LARAVEL: How to redirect users on timeout

Gabriel Guerra
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.

Photo credits & licensing

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:

The whole method will become like this:

That’s it. Happy coding!