Sometimes we get the 419 session expired error. Simple ways to handle the TokenMismatchException , is we put it on Exception Handler (global):
bootstrap/app.php (Laravel 11)
-> withExceptions ( function ( Exceptions $exceptions ) { $exceptions -> render ( function ( \Illuminate\Session\TokenMismatchException $e , Request $request ) { return redirect () -> route ( 'login' ) -> withErrors ([ 'username' => 'Your session expired. Please log in again.' ]); }); })
Enter fullscreen mode
Exit fullscreen mode
app/Exceptions/Handler.php (Laravel 10)
use Illuminate\Session\TokenMismatchException ;
public function render ( $request , Throwable $exception ) { if ( $exception instanceof TokenMismatchException ) { return redirect () -> route ( 'login' ) -> withErrors ([ 'username' => 'Your session expired. Please log in again.' ]); }
return parent :: render ( $request , $exception ); }
Enter fullscreen mode
Exit fullscreen mode
And just refresh the page.…