LARAVEL: How to redirect users on timeout

LARAVEL: How to redirect users on timeoutGabriel GuerraBlockedUnblockFollowFollowingMar 9Sometimes web frameworks leads us to weird scenarios.

For a protected area, it's common and expected that every session have an inactivity period set and, after that, the user is forced to log in again.

Well, Laravel doesn't have a default redirect url for this situation except the infamous screen message:We can handle that :)Redirecting users when timeout occurs is easy.

Timeout triggers an Exception which can be caught and treated accordingly our needs.

Photo credits & licensingLaravel has a Handler with this sole purpose.

What we're going to do is just edit the Exceptions handler in App/Exceptions/Handler.

phpIn the render method add the following:if ($exception instanceof AuthenticationException) { return redirect('/');}The whole method will become like this:public function render($request, Exception $exception){ if ($exception instanceof AuthenticationException) { return redirect('/'); } return parent::render($request, $exception);}That’s it.

Happy coding!.

. More details

Leave a Reply