This answer applies to version 5.4, possibly to previous versions, but I have not tested them.
The root of the problem is that the CSRF token has expired on the client side, which makes POST on the server with this token.
IF you use AJAX, you can use API routes that do not perform CSRF validation by default.
You can disable CSRF checking for specific URIs. In this case, I will disable the CSRF check for /logout. This approach works well if you really want to exclude specific URIs from validation.
app / Http / Middleware / VerifyCsrfToken.php
/ **
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'/logout'
];
, CSRF, , . , .
app/Exceptions/Handler.php
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if($exception instanceof \Illuminate\Session\TokenMismatchException){
// token mismatch is a security concern, ensure logout.
Auth::logout();
// Tell the user what happened.
session()->flash('alert-warning','Your session expired. Please login to continue.');
// Go to login.
return redirect()->route('login');
}
return parent::render($request, $exception);
}
, , . 1 . , ( 120). , , , POST.
config/session.php
/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/
'lifetime' => 1,