I'm trying to use the authentication system that comes built in with laravel 5.2. The login seems to be working correctly, if I replace the return statement with Auth::check(), it returns true. But when I redirect to '/', Auth::check() suddenly returns false in my Auth middleware.
Sessions Create method:
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");
}
Auth Middleware:
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);
}
Routes file:
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');
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…