为什么Auth :: attempt总是在Laravel 5.1中返回false

时间:2015-09-02 09:31:27

标签: php laravel-5 laravel-5.1

我有这段代码:

 public function postLogin(Request $request) {


    if(Auth::attempt(['username' => $request->username, 'password' => $request->password]))
    {
        return redirect()->intended('users/dashboards');
    }else{
        return redirect('/')->withInput()->with('message', 'Login Failed');
    }
}

这是我的控制器:

  Route::get('/', ['as' => 'home', 'uses' => 'HomeController@Home']);
  Route::post('post-login', ['as' => 'post_login', 'uses' => 'HomeController@postLogin']);

  Route::group(['middleware' => 'auth', 'prefix' => 'user'], function(){
    Route::get('dashboards', ['as' => 'dashboards', 'uses' => 'HomeController@getDashBoards']);
  });

我不知道为什么它总是返回假。我正在测试用户是否经过身份验证。如果我使用Auth::check(),则返回false,但如果我使用Auth::guests则会返回true。我对中间件感到困惑。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

实际上,错误发生在framework/src/Illuminate/Auth/Guard.php

的hasValidCredentials函数中
protected function hasValidCredentials($user, $credentials) {
    return ! is_null($user) && $this->provider->validateCredentials($user, $credentials);
}

所以只需用

替换return语句
return ! is_null($user) ||
$this->provider->validateCredentials($user, $credentials);