Friday, June 17, 2016

Token Mismatch error on login page Laravel 5

Token Mismatch error on login page Laravel 5:

Sometimes we noticed on the login page of Laravel 5, when we leave this page for sometimes and then click on login button, then it shows token mismatch error.

This is because session time of laravel has been expired and current token for that page do not match the actual page.

For prevent this kind of error, we have to handle it.

Goto this file :  app/Exceptions/Handler.php

and add this code in render function.

    public function render($request, Exception $e)
    {
        if ($e instanceof \Illuminate\Session\TokenMismatchException) {
            return redirect()->guest('auth/login');
        }
        return parent::render($request, $e);
    }


Thanks

1 comment:

  1. Well, it works, meaning it does not throw an error.

    But don't you think, that it isn't desired behaviour? If there is no session on login, why on earth can't laravel create a new one? Any other page is OK - redirect to login page. But on login page itself?

    ReplyDelete