【问题标题】:Laravel Auth::attempt() always false?Laravel Auth::attempt() 总是假的?
【发布时间】:2013-08-03 02:47:33
【问题描述】:

我最近开始学习 Laravel(PHP MVC 框架),并且在使用以下方法对用户进行身份验证时遇到了很多问题:Auth::attempt($credentials)。

我将把我的代码 sn-ps 放在下面。在我的 mySQL 数据库中,我有一个包含以下键 => 值的记录: id=>1, username=>'admin', password=>'admin', created_at=>0000-00-00 00:00:00, updated_at=>0000-00-00 00:00:00

验证器通过,但 Auth:attempt 总是失败。

如果下面的代码和我的小解释还不够,请告诉我! :-)

Routes.php:

Route::get('login', 'AuthController@showLogin');
Route::post('login', 'AuthController@postLogin');

AuthController:

public function showLogin(){

    if(Auth::check()){
        //Redirect to their home page
    }
    //Not logged in
    return View::make('auth/login');
}

public function postLogin(){

    $userData = array(
        'username' => Input::get('username'),
        'password' => Input::get('password')
    );

    $reqs = array(
        'username' => 'Required',
        'password' => 'Required'
    );

    $validator = Validator::make($userData, $reqs);

    if($validator->passes() && Auth::attempt($userData)){
        return Redirect::to('home');
    }
    return Redirect::to('login')->withInput(Input::except('password'));
}

【问题讨论】:

    标签: php authentication laravel


    【解决方案1】:

    我相信尝试()方法会对发送的密码进行哈希处理。如果您的数据库条目的密码是明文的“admin”,那将失败。使用 Hash::make($password); 保存您的密码;我相信你的问题应该得到解决。

    【讨论】:

    • 成功了!我回滚了所有迁移,对所有密码进行了 Hash::make 更改,然后运行了迁移。你是救生员! :D
    • 甜蜜,很高兴我能帮上忙! :) 如果您可以选择正确的答案,那就太好了,这样我就能获得一些声誉:D
    • 你去!再次感谢:)
    • 这确实应该在 Laravel 文档中明确说明,因为唯一的提示就是说明密码字段必须是 60 个字符。它没有说明 Auth::attempt 方法(理所当然地)对密码进行哈希处理。
    • @MarkW 你救了我,我错过了文档中关于密码字段为 60 个字符的部分,因此我的尝试总是失败......谢谢!
    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 2016-09-12
    • 2017-05-24
    • 2013-06-11
    • 2015-12-10
    • 2014-01-17
    • 2019-04-14
    相关资源
    最近更新 更多