8

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

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

Validator 通过,但 Auth:attempt 总是失败。

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

路线.php:

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

授权控制器:

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

1 回答 1

22

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

于 2013-08-02T00:10:28.633 回答