Redirect to login in Laravel 5.4 if the token fails


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 в 16:43

1 answer


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()
         ->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 / 17:06