Change the redirectTo method in /app/Http/Middleware/Authenticate.php
Session expired -> Redirect to / instead of /login
Hello everybody,
I am just wondering, why Laravel is redirecting a user to /login by default if a user's session expires.
Could it be possible to redirect to / instead?
Maybe by changing the ExceptionHandler?
What's best practice here?
Hello @ahoi . If user's session is expired then user becomes unauthenticated user.
And auth middleware comes to play. It checks whether user is authenticated and if not - it throws exception and redirects to login route. Quick solution - to name / end point as login: Route::get('/', '{Controller}@{method}')->name('login')
But I would recommend you to override unauthenticated method in your App\Exceptions\Handler class:
use Illuminate\Auth\AuthenticationException;
...
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('/'));
}
Please or to participate in this conversation.