Laravel Auth::attempt() 总是假的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18006597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:13:06  来源:igfitidea点击:

Laravel Auth::attempt() always false?

phpauthenticationlaravel

提问by xDranik

I recently started learning Laravel (PHP MVC framework) and I've been having a lot of issues with Authenticating a user with: Auth::attempt($credentials).

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

I'll put my code snippets below. In my mySQL database I have a record with the following key=>values: id=>1, username=>'admin', password=>'admin', created_at=>0000-00-00 00:00:00, updated_at=>0000-00-00 00:00:00

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

The Validator passes, but the Auth:attempt always fails.

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

If the code below and my little explanation isn't enough, please let me know! :-)

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

Routes.php:

路线.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'));
}

回答by madshvero

I believe the attempt() method will hash the password that is sent in. Seeing as your DB entry's password is 'admin' in plaintext, that would fail. Save your password with Hash::make($password); and i believe your problem should be solved.

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