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

saurabhji's avatar

Laravel token

Laravel Token is showing : TokenMismatchException : When i get logout from my app. May be there is an issue in Post Logout link(Form).

Tokens functionality tweaks a bit.

0 likes
9 replies
saurabhji's avatar

I have generated simple Auth functonality by : php artisan make:auth I don't know why its showing token mismatch if i login and logout randomly.

StefanVoinea's avatar

This error occurs when you don't have a csrf_field in your form like this {{ csrf_field() }}.

saurabhji's avatar

Thanks, but i am having that field in my logout form.

jamiesefton's avatar

Try adding this in the head tag of your views

<meta name="csrf-token" content="{{ csrf_token() }}">
clay's avatar

If you try to logout or login after the session has timed out, you'll get the exception.

DarkRoast's avatar

Which version of Laravel are you using? Some older versions required you to add the 'web' middleware group.

clay's avatar

Using the auth scaffolding in Laravel 5.3, 'logout' is a post request. If you make post requests after the session has expired, and you're using the csrftoken middleware, then you will, by default, throw a tokenmismatchexception. So if you sit idle on the login page for longer than the session timeout or if you sit idle and the session expires, then you try to logout, you'll get the exception.

To avoid this on logout, either make 'logout' a GET request, like in 5.2 and earlier versions, or add 'logout' route to the 'except' property of the verifycsrfmiddleware, or handle it in your 'handler' class, or just have a custom error page.

I handle this in the handler.php by checking for a tokenmismatchexception then checking the requestUri for 'login' or 'logout', then redirecting to 'login' and showing a flash message that the session has expired, asking the user to login.

saurabhji's avatar

I was using laravel 5.3. But it may the token exception.

Please or to participate in this conversation.