Menu

419 Session Expired Error in Laravel
🖼️

419 Session Expired Error in Laravel

DEV Community·Rafli Zocky·6 months ago
#Hn4VdNVV
#csrf#webdev#php#security#session
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.