9

I have a Laravel 4 app in which I have set up one user. In my login route I'm calling Auth::attempt with the email and password but it always comes back as false. I definitely have the password correct and the correct hash in the database as Hash::check returns true.

I think it may be due to using email as the login field instead of username, but I can't see any setting for that. This question implied you could add an option to config/auth.php but it didn't work. This question says to use username as the array key, but then I get SQL error because it tries to select on a username field in the database.

Do I need to add something to the User model to specify the username field? Here is my login route:

Route::post('login', function() {

    // data from login form
    $credentials = array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
    );

    $auth = Hash::check(Input::get('password'), Hash::make('mypass'));
    var_dump($auth); // this is TRUE

    // login was good
    $auth = Auth::attempt($credentials);
    var_dump($auth); // this is FALSE
});
4

3 回答 3

11

我发现了问题。正如 Jason 在上面的评论中所建议的那样,我已经修改了models/User.php文件并删除了一些我没有意识到是必要的功能。

和方法必须包含在 User 模型中才能进行身份验证getAuthIdentifier()getAuthPassword()

于 2013-04-24T09:57:25.150 回答
6

在 app/config/app.php 中确保您设置了“密钥”。这让我把头发拉了出来。一切都会正常工作,密码似乎在数据库中散列,但它总是会返回 false,直到您设置此密钥并将密码重新散列到数据库中。

“php 工匠密钥:生成”

于 2013-04-24T03:10:21.900 回答
1

有同样的问题,让我汗流浃背几个小时。明确检查您的User.php模型并确保您没有覆盖默认模型。谢谢杰森!

于 2013-05-18T18:05:26.623 回答