How To Avoid TokenMismatchException On Logout In Laravel

In this example we will see how to avoid TokenMismatchException on logout in laravel. Many time we faced TokenMismatchException error in laravel. this error occured If you stay too long time on one form or if your system stay on idle or you are not active on your computer, and then again try to fill this form.

At that time you may get a TokenMismatchException error, because the CSRF token won’t be the same. recently many time we found this problems in logout time.

So, In this example I will show you to how to solved TokenMismatchException error in laravel.

Normally, if you are not active for long time in your system then you will get error like below screen print.

how_to_avoid_tokenmismatchexception_on_logout.png

To avoid TokenMismatchException error, we may add exceptions for the URLs that we don’t want to have CSRF protection. There are special array for that – in App/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}

So what we need do, just add logout into this array.

protected $except = [
    '/logout'
];

If you want to add more URLs then you can add here, but CSRF protection is also important.


You might also like :