Increasing the duration of the session in config/sessions.php ? (default : 120 min)
L5 - Properly handle TokenMismatchException when logging in
Clients are complaining about TokenMismatchExceptions when trying to log in. Many use the sites I develop on workstations that always keep the site's login form in view, so this happens very frequently.
The error is thrown here (in /vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php) -
public function handle($request, Closure $next)
{
if ($this->isReading($request) || $this->tokensMatch($request))
{
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
I searched around and could not find a 100% definitive solution to this. I was able to find this bit of code but it does not seem to make any difference:
App\Exceptions\Handler.php
public function render($request, Exception $e)
{
if ($e instanceof TokenMismatchException){
//Redirect to login form if session expires
return redirect($request->fullUrl())->with('errors',"The login form has expired, please try again. In the future, reload the login page if it has been open for several hours.");
}
return parent::render($request, $e);
}
What is the proper way to handle this without displaying an error screen to the user? Is there any way to dynamically refresh the token from the login form to prevent this altogether? Clients do not see this as a security feature, they see it as a bug in my code. I understand the purpose of CSRF protection but it does not translate for them.
Thanks!
Please or to participate in this conversation.