Hello,
I would like to know if there is a way to detect if the user's session has expired. I did a few tests with a session lifetime of 1 minute, and tried to see what kind of error is thrown when the user tries to navigate after the session expired.
I went to app\Exceptions\Handler.php and printed the errors in the 'render' method, and it seems to be a 404 NotFoundHttpException, which doesn't help at all as I would like to be able to tell the user that his session expired and not that the page he's looking for doesn't exist.
I also tried to dig a bit deeper and printed errors in the Laravel files (Foundation\Exceptions\Handler.php for example) : in the 'report' method, the exception is a TokenMismatchException while in the 'render' method, it's another NotFoundHttpException. I don't get why those 2 methods would print 2 different exceptions.
Is there a way to tell the user that his session expired?
Any help is appreciated,
Thanks
Session expiration probably means that the user is logged out. In this case you can specify middleware on your routes that will redirect users if the route requires you to be logged in. That will redirect them to auth/ pages but user will not see any notification. You can of course edit your app/Http/Middleware/Authenticate file, @handle method and use
Session::put('message', 'You need to be logged in');
And on your auth/login page use
echo Session::get('message');
Hey, thank you for your answer. I added this at the beginning of the @handle method in the Authenticate middleware :
if (!Auth::check()) {
Session::flash('message', trans('errors.session_label'));
Session::flash('type', 'warning');
return redirect()->route('home');
}
Don't know if there's a better way, but it works, thanks !
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community