LARAVEL: How to redirect users on timeout
Mar 9 · 2 min read
Sometimes 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.

Laravel has a Handler with this sole purpose.
What we're going to do is just edit the Exceptions handler in App/Exceptions/Handler.php
In 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!
Other Laravel useful quick tips
How to make multiple inserts from inputs
How to rename a table with migrations preserving data