我正在尝试使用 laravel 5.2 内置的身份验证系统。登录似乎工作正常,如果我用 Auth::check() 替换 return 语句,它会返回 true。但是当我重定向到“/”时,Auth::check() 突然在我的 Auth 中间件中返回 false。
会话创建方法:
public function create(Request $request)
{
$email = $request->email;
$password = $request->password;
if(Auth::attempt(['email' => $email, 'password' => $password])) {
return redirect()->intended('/'); // returns true when replaced with Auth::check();
}
return redirect("login");
}
认证中间件:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return var_dump(Auth::check()); // returns false
}
}
return $next($request);
}
路由文件:
Route::post('/create-session', 'SessionController@create');
Route::get('/logout', 'SessionController@logout');
Route::get('/login', function() {
return view('login');
});
Route::group(['middleware' => ['web', 'auth']], function(){
Route::get('/', 'HomeController@getIndex');
});