Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel "The page has expired due to inactivity"

The Error

Enter image description here

My Scenario

Firstly I know that this error message is a CSRF / session error message and that's fine; in fact it's behaving as expected. For reasons specific to my application I've made it so that to access an account I send a post request and build out the page depending on that response data.

The problem is that when the session times out on that page, my application does nothing, but throw this error message. I then physically have to type in a URL to get redirected to the login page which is not ideal for clients.

My Question

How do I control the behaviour of my application so I can modify what happens when this CSRF error occurs, for example if I wanted to make a custom page or use a controller to perform a redirect, etc.?

The difference is that I know what this error is. I want a way to redirect while this error is present, I'm not trying to stop the error from showing, and I'm trying to redirect from it. In my case it's not an error; it's a behaviour that I expect!

like image 937
Kenziiee Flavius Avatar asked Mar 22 '18 07:03

Kenziiee Flavius


1 Answers

In your app/Exceptions/Handler.php in render function add the lines:

    if ($e instanceof \Illuminate\Session\TokenMismatchException) {

        return redirect('/login')->with('message', 'Sorry, your session seems to have expired. Please login again.');

   }

before the line :

return parent::render($request, $e);

This should redirect to login on a Token mismatch.

Link with further explanation: https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e

like image 145
Mehravish Temkar Avatar answered Oct 26 '22 09:10

Mehravish Temkar