Handling expired token in Laravel

后端 未结 13 2777
情话喂你
情话喂你 2020-11-29 00:30

What is the best way to handle expired tokens in laravel 5.

I mean I have a page and it has some links which perform ajax requests. They work fine when the page is

13条回答
  •  长情又很酷
    2020-11-29 01:19

    Best way to handle this Exception is with App\Exceptions\Handler.php.

    public function render($request, Exception $e) {
    
            if ($e instanceof \Illuminate\Session\TokenMismatchException) {            
                return Redirect::back()->withErrors(['session' => 'Désolé, votre session semble avoir expiré. Veuillez réessayer.']);
            }
    
            return parent::render($request, $e);
        }
    
    
    

    and where ever you wanna show this message (in all your pages that contains csrf_token), add this piece:

    @if(count($errors)>0) @foreach($errors->all() as $error)
    • {{$error}}
    @endforeach @endif

提交回复
热议问题