Laravel Auth ::尝试失败每次 [英] Laravel Auth::attempt failing each time

查看:62
本文介绍了Laravel Auth ::尝试失败每次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在laravel应用程序上创建了一个测试用户.详细信息是

Hi guys I've created a test user on my laravel app. The details are

用户:joe@gmail.com密码:123456

user: joe@gmail.com pass: 123456

当我完成注册过程时,一切都会按预期进行,并且在database

When I go through the registration process everything works as expected and an entry is made into the users table of the database

完成后,我将user重定向到仪表板.

Once this is finished I redirect the user to the dashboard.

public function postCreate(){
        //Rules
        $rules = array(
        'fname'=>'required|alpha|min:2',
        'lname'=>'required|alpha|min:2',
        'email'=>'required|email|unique:users',
        'password'=>'required|alpha_num|between:6,12|confirmed',
        'password_confirmation'=>'required|alpha_num|between:6,12'
        );

        $validator = Validator::make(Input::all(), $rules);
        if($validator->passes()){
            //Save in DB - Success
            $user = new User;
            $user->fname = Input::get('fname'); //Get the details of form
            $user->lname = Input::get('lname');
            $user->email = Input::get('email');
            $user->password = Hash::make(Input::get('password'));//Encrypt the password
            $user->save();
            return Redirect::to('/books')->with('Thank you for Registering!');
        }else{
            //Display error - Failed
            return Redirect::to('/')->with('message', 'The Following Errors occurred')->withErrors($validator)->withInput();
        }
    }

然后,我导航回到登录页面并尝试使用上面的凭据登录,并且不断被告知Auth::attempt()失败,因此我的用户无法登录该应用程序.

I then navigate back to the landing page and attempt to log in using the credentials above and I keep getting told that Auth::attempt() is failing hence my user cannot log into the application.

public function login(){
        if(Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))){
            //Login Success
            echo "Success"; die();
            return Redirect::to('/books');
        }else{
            //Login failed
            echo "Fail"; die();
            return Redirect::to('/')->with('message', 'Your username/password combination was incorrect')->withInput();
        }
    }

有人知道为什么会这样吗?这是我的users表的Schema:

Does anyone know why this is happening? This is the Schema for my users table:

Schema::create('users', function($table){ 
            $table->increments('id'); 
            $table->integer('type')->unsigned(); 
            $table->string('fname', 255); 
            $table->string('lname', 255); 
            $table->string('email')->unique(); 
            $table->string('password', 60); 
            $table->string('school', 255); 
            $table->string('address_1', 255); 
            $table->string('address_2', 255); 
            $table->string('address_3', 255); 
            $table->string('address_4', 255);
            $table->string('remember_token', 100);
            $table->timestamps(); 
        });

非常感谢您的帮助.

查看登录信息":

<div class="page-header">
    <h1>Home page</h1>
</div>

<!-- Register Form -->
<form   action="{{ action('UsersController@postCreate') }}" method="post" role="form">
    <h2 class="form-signup-heading">Register</h2>
    <!-- Display Errors -->
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>

    <!-- First Name -->
    <div class="form-group">
        <label>First Name</label>
        <input type="text" class="form-control" name="fname" /> 
    </div>
    <!-- Last Name -->
    <div class="form-group">
        <label>Last Name</label>
        <input type="text" class="form-control" name="lname" /> 
    </div>
    <!-- Email -->
    <div class="form-group">
        <label>Email</label>
        <input type="text" class="form-control" name="email" /> 
    </div>
    <!-- Password-->
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password" />  
    </div>
    <!-- Confirm Password -->
    <div class="form-group">
        <label>Confirm Password</label>
        <input type="password" class="form-control" name="password_confirmation" /> 
    </div>
    <input type="submit" value="Register" class="btn btn-primary"/>
</form>

<!-- Login Form -->
<form   action="{{ action('UsersController@login') }}" method="post" role="form">
    <h2 class="form-signup-heading">Login</h2>
    <!-- Email -->
    <div class="form-group">
        <label>Email</label>
        <input type="text" class="form-control" name="email" /> 
    </div>
    <!-- Password-->
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password" />  
    </div>
    <input type="submit" value="Login" class="btn btn-primary"/>
</form>

推荐答案

能否在下面运行此功能-并告诉我错误发生在哪里?它将诊断问题:

Can you run this function below - and tell me where the error occurs? It will diagnose the problem:

public function testLogin()
{
     $user = new User;
     $user->fname = 'joe';
     $user->lname = 'joe';
     $user->email = 'joe@gmail.com';
     $user->password = Hash::make('123456');

     if ( ! ($user->save()))
     {
         dd('user is not being saved to database properly - this is the problem');          
     }

     if ( ! (Hash::check('123456', Hash::make('123456'))))
     {
         dd('hashing of password is not working correctly - this is the problem');          
     }

     if ( ! (Auth::attempt(array('email' => 'joe@gmail.com', 'password' => '123456'))))
     {
         dd('storage of user password is not working correctly - this is the problem');          
     }

     else
     {
         dd('everything is working when the correct data is supplied - so the problem is related to your forms and the data being passed to the function');
     }
}

一个想法-您确定用户正确地保存到数据库中了吗?您是否尝试过清空/删除"数据库并重试代码?在您当前的代码中,如果您继续在joe@gmail.com上注册,它将失败-因为它是唯一的.但是您不会在任何地方捕获错误.因此,清空数据库,然后重试...

one thought - are you sure the user is being correctly saved in the database? Have you tried to 'empty/delete' your database and try your code again? In your current code, it will fail if you keep registering with joe@gmail.com - because it is unique. But you dont catch the error anywhere. So empty the database and try again...

我发现了您发布的另一个具有相同问题的问题 -在其中您提到以下代码是您的用户模型?

Edit 2: I found another question you posted with the same problem - and in there you mentioned that the following code is your user model?

use Illuminate\Auth\UserTrait; 
use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableTrait; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class User extends Eloquent implements UserInterface, RemindableInterface { 

use UserTrait, RemindableTrait; 

/** 
* The database table used by the model. 
* 
* @var string 
*/ 
protected $table = 'users'; 

/** 
* The attributes excluded from the model's JSON form. 
* 
* @var array 
*/ 
protected $hidden = array('password'); 

public function getAuthIdentifier() { 

} 

public function getAuthPassword() { 
} 

public function getRememberToken() { 

} 

public function getRememberTokenName() { 

} 

public function getReminderEmail() { 

} 

public function setRememberToken($value) { 

} 
}

那是您当前的用户模型吗?因为如果是这样-这是错误的-这些功能都不应该为空.

Is that EXACTLY your current user model? Because if so - it is wrong - none of those functions should be blank.

这是Laravel 4.2的正确用户模型的样子

This is what a CORRECT user model should look like for Laravel 4.2

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

}

这篇关于Laravel Auth ::尝试失败每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆