Laravel Auth ::每次都尝试失败

时间:2014-08-15 15:44:28

标签: php laravel laravel-4

大家好我已在我的laravel应用中创建了测试用户。细节是

用户:joe@gmail.com传递:123456

当我完成注册过程时,一切都按预期工作,并在users table的{​​{1}}中输入

完成此操作后,我将database重定向到信息中心。

user

然后我导航回登录页面并尝试使用上面的凭据登录,我不断被告知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()

有谁知道为什么会这样?这是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(); } } 表的Schema

users

非常感谢任何帮助。

'查看登录':

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(); 
        });

4 个答案:

答案 0 :(得分:9)

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

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注册它将会失败 - 因为它是唯一的。但你不会在任何地方发现错误。所以清空数据库并再试一次......

编辑2:I found another question you posted with the same problem - 在那里你提到以下代码是你的用户模型?

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) { 

} 
}

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

对于Laravel 4.2,这是一个CORRECT用户模型的样子

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');

}

答案 1 :(得分:2)

你会确定:

  • 你的模特:
我看起来像是:

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

class User extends Eloquent implements UserInterface, RemindableInterface {

        protected $table = 'users';

        protected $hidden = array('password');

        public function getAuthIdentifier()
        {
            Return $this->getKey ();
        }
        public function getAuthPassword()
        {
            return $this->password;
        }
    }
  • 确保您的app / config / auth.php配置正确
  • 确保app / config / app.php有服务提供商
  

'照亮\验证\ AuthServiceProvider',

  • 确保您的控制器类具有身份验证。在写课之前你已经使用过Auth(我的意思是包括Auth类)

这一切都可能使Auth无法正常工作

答案 2 :(得分:0)

strlen(Hash::make(Input::get('password')))的价值是多少?如果它大于60,那么每次都会导致身份验证失败,因为存储的密码不是完整的哈希值。

答案 3 :(得分:0)

启用密码哈希处理后,User模型必须覆盖以下方法:

public function getAuthIdentifierName()
{
    return 'email';
}

public function getAuthIdentifier()
{
    return request()->get('email');
}

public function getAuthPassword()
{
    return Hash::make(request()->get('password'));
}