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

Swaz's avatar
Level 20

[L5] Check if session expired

Is there a way I can check if a users session has expired? Then redirect to the login page.

If I stay logged in overnight and refresh the page, I get a TokenMismatchException but I don't want to catch that, because that is the same error you get when posting a form with a bad token.

0 likes
19 replies
bashy's avatar

Not sure why that would throw the same exception... So that's session expire yeah?

bashy's avatar

Not sure if that's entirely related...

bobbybouwmann's avatar

That's why I said if I wasn't sure if it would help him.. I can't find anything else on this subject and I can't recreate it..

bashy's avatar

Sounds a bit weird... session expire would hit the middleware and redirect to login anyway...right?

1 like
Swaz's avatar
Level 20

@bashy That is the behaviour I get when I set the session to expire after 1 minute, and then refresh. I will watch carefully for it to happen again and take note of any additional information I can provide.

This is happening on an iPad (mobile safari) btw. I log in, and go to whatever page, then I charge the iPad over night, and when I go back to the site in the morning, I sometimes get the error. It kind of looks like the page is automatically refreshing when the browser opens up.

bashy's avatar

@Swaz When ever I do that, it redirects me to the login page. Does the area require login/auth? I can't see how the TokenMismatch exception would be thrown if it's just session. You shouldn't need the token just to view a page.

Swaz's avatar
Level 20

@bashy Yea, it's a page that requires you to be authenticated.

alenabdula's avatar

Just a wild guess, maybe do composer dump-autoload and/or clear any application cache. Also clear the device cache.

Snapey's avatar

I think I'm having the same issue. If you guys think not, then I will create a new thread.

There is possibly a situation where the session has not expired but the token has?

So, the user has logged in and is looking at a page with a form. Imagine this form is to create a new topic for instance. Several hours pass and they still have that form on the screen. Now they come back and fill in the form and press submit. The application then throws a TokenMismatchException.

How can this be handled gracefully?

Or, if the session has also expired... the route for the page uses Route::group(array('middleware' => 'auth'). Is this run after csrf validation?

bashy's avatar

@Snapey I'm not sure in what order they're executed but I would think it's top to bottom in the middleware array list? That is excepted behaviour by the way. Token only has a small lifetime so it's not used for too long.

Swaz's avatar
Level 20

This is happening to me without hitting submit. It's when I'm signed in, and the mobile browser opens up after a few hours of being closed. It happened again this morning, the error is:

ErrorException in RouteServiceProvider.php line 33:
Trying to get property on non-object
Swaz's avatar
Level 20

Yes, that file. I have a route model binding on that line:

// app/Providers/RouteServiceProvider.php
$router->bind('project', function($id)
{   
    return Auth::user()->company->projects()->findOrFail($id);
});
SachinAgarwal's avatar

If i'm not wrong, it should be like this

return Auth::user()->company()->projects()->findOrFail($id);
SachinAgarwal's avatar
Level 21

@Swaz Ok so you are getting this error because you dont have any loggedin user. Try this

$router->bind('project', function($id)
{  
    if(Auth::check()) { 
            return Auth::user()->company->projects()->findOrFail($id);
    }
    return redirect('/');
});
Swaz's avatar
Level 20

@SachinAgarwal Shouldn't the middleware be detecting if I am authenticated? Or does RouteServiceProvider get hit before the middleware?

SachinAgarwal's avatar

Logically Middleware is suppose to hit first.
Once check if you middleware is registered in http\kernal.php
And even check if you applied correct middleware and at correct place.(Case sensitivity matters)
IF eveything is fine, then try that logic once.

Please or to participate in this conversation.