2

I have been suffering this TokenMismatchException. So I was figuring what causes the error. I found out that If I try to idle the landing page for 15minutes then login. I got the TokenMismatchException. I have a {!! csrf_field() !!} in the loginform.

My config/session.php lifetime is set to 15 minutes. So I've waited 15minutes to catch the TokenMismatchException error.

So I have tried these several solutions

  1. installed laravel-caffeine
  2. <meta name="csrf-token" content="{{ csrf_token() }}" />
  3. Edited the Handler.php

    public function render($request, Exception $e) { if ($e instanceof TokenMismatchException) { return redirect('/login')->with('message', 'Security token expired. Please, login back.'); } if ($e instanceof \Illuminate\Session\TokenMismatchException) { return redirect('/login')->with('message', 'Security token expired. Please, login back.'); } return parent::render($request, $e); }

But nothing seems working.

Vahn Marty
  • 1,428
  • 3
  • 14
  • 28

2 Answers2

0

You were on the right track. You could edit App\Exceptions\Handler like this

public function render($request, Exception $e)
{

    if($this->isTokenMismatchException($e)) {

        if(!(Auth::check()))  return $this->redirectGuestToLogin();

    } 

    return parent::render($request, $e);
}

protected function redirectGuestToLogin(){

    return redirect()->guest('auth/login');

}

protected function isTokenMismatchException(Exception $e){

      return $e instanceof TokenMismatchException;

}
Wistar
  • 3,770
  • 4
  • 45
  • 70
0

My code in Handler was right. But my mistake is that I was using the older version of the project in the browser. That is why I am still getting the error. Now it's totally working.

Vahn Marty
  • 1,428
  • 3
  • 14
  • 28
  • I didn't understand the error about `using the older version of the project in the browser`. Could you please explain it in more detail? – Pathros Jun 30 '17 at 04:06
  • the actual project is supposed to be "http://localhost/myrcf-version-3" and I was running the "http://localhost/myrcf/" the whole time. – Vahn Marty Jul 07 '17 at 20:59