Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

felipesmendes's avatar

How to handle session expired on ajax request (302 Found) Laravel 8

Hello my dears,

Laravel Framework 8.83.16

I have an application that controls the sessions by file.

And when a session ends and the user is still on the page and makes an ajax request, in the return on the ajax request I get a 302 Found with redirection to the login screen, but the browser is not redirecting automatically, it returns another ajax request with the login screen.

For me this behavior is useless, I need that if you receive the 302 effectively redirect to the login screen.

I already tried this through middle that I keep getting the same problem with 302 and I already tried to do it with setInterval checking the session that also returned the 302.

How can I adjust this so that any ajax requests after the session expired are effectively redirected to the login screen?

0 likes
2 replies
Sinnbeck's avatar

Hard to say without seeing your code. My guess is to have a method that handles all ajax calls and redirects if it gets an error that the user is signed out

felipesmendes's avatar

@Sinnbeck Thank you for reply, I can show part of the code, I need you to tell me which one would be interesting to analyze, for example, the ajax request, or a controller specified, or a specific method.

Second point is that I have already tried to use a middleware so that before completing the request I can analyze the ajax request and redirect it, but the problem is that this request is already ajax so when I receive the 302 redirect again in the browser it will not take effect because today I I already receive and the browser does not redirect.

Please or to participate in this conversation.