Friday, September 23, 2016

Token Mismatch error in Laravel

Hi Everyone

Today we are going to learn, how to solve token mismatch error on login page.

Sometimes we noticed that, 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.

To prevent this kind of error, we have to write below code

Open the file :  app/Exceptions/Handler.php

and add below 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);  
   }  

Now instead of showing token mismatch error, it will redirect to login page and refresh the token.

Thanks

No comments:

Post a Comment