Redirect to login in Laravel 5.4 if the token fails

3

When I'm on the login page for a while without activity and I try to login, I get the error TokenMismatchException and I know it has to do with the token that is in the access form and that being so long without activity the token becomes invalid, but as I avoid that instead of showing me the error, I simply redirect to the login .

Error information:

  

TokenMismatchException   in VerifyCsrfToken.php (line 68)

    
asked by Edwin Galeano 04.05.2018 в 14:43
source

1 answer

3

You have to manage the error caused by the fact that the token is "expired". You can do this in app\Exceptions\Handler.php . In the class Handler , within the function render you have to put the following code:

if ($exception instanceof TokenMismatchException)
   {
      return response()
         ->redirectTo('login')
         ->with('flash_error', 'Mensaje de error que quieras mostrar'));
   }

Contribution of Edwin Galeano

Must be added at the top use Illuminate\Session\TokenMismatchException; otherwise $exception instanceof TokenMismatchException will be false

    
answered by 04.05.2018 / 15:06
source