A
A
Alexander2019-07-03 19:47:20
Laravel
Alexander, 2019-07-03 19:47:20

How to solve Laravel TokenMismatchException issue?

The essence of the problem:
When sending an Ajax request with an expired csrf_token, an error is written to laravel.log.

Mistake

[2019-07-03 19:35:48] local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /site.ru/public_html/bootstrap/cache/compiled.php:2710
Stack trace:
#0 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /site.ru/public_html/bootstrap/cache/compiled.php(9188): call_user_func_array(Array, Array)
#2 /site.ru/public_html/bootstrap/cache/compiled.php(12456): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /site.ru/public_html/bootstrap/cache/compiled.php(9188): call_user_func_array(Array, Array)
#5 /site.ru/public_html/bootstrap/cache/compiled.php(11104): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#6 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#7 /site.ru/public_html/bootstrap/cache/compiled.php(9188): call_user_func_array(Array, Array)
#8 /site.ru/public_html/bootstrap/cache/compiled.php(12193): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /site.ru/public_html/bootstrap/cache/compiled.php(9188): call_user_func_array(Array, Array)
#11 /site.ru/public_html/bootstrap/cache/compiled.php(12132): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#12 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 /site.ru/public_html/bootstrap/cache/compiled.php(9188): call_user_func_array(Array, Array)
#14 /site.ru/public_html/app/Http/Middleware/CheckRkn.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 [internal function]: App\Http\Middleware\CheckRkn->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 /site.ru/public_html/bootstrap/cache/compiled.php(9188): call_user_func_array(Array, Array)
#17 /site.ru/public_html/app/Http/Middleware/CheckForMaintenanceMode.php(23): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#18 [internal function]: App\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#19 /site.ru/public_html/bootstrap/cache/compiled.php(9188): call_user_func_array(Array, Array)
#20 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 /site.ru/public_html/bootstrap/cache/compiled.php(9178): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#22 /site.ru/public_html/bootstrap/cache/compiled.php(2035): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#23 /site.ru/public_html/bootstrap/cache/compiled.php(2018): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#24 /site.ru/public_html/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#25 {main}

Added a check to /app/Exceptions/Handler.php, in case of an error, the corresponding error and a new csrf_token are returned to the user.
But the error continues to be written to the logs, I suspect that the problem is in order in the order of connecting $middleware "Kernel.php", but I cannot solve it myself.
Kernel.php

protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
];

Tell me how to fix this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexandr, 2019-07-03
@QcfgAlexandr

And here is the solution:
You need to add a check in the "report" function and not in the "render" function in /app/Exceptions/Handler.php
The check itself:

if ($e instanceof \Illuminate\Session\TokenMismatchException) {
  return response()->json(['msg'=> 'session expired', 'token'=> csrf_token()], 401);
}

K
Konstantin B., 2019-07-03
@Kostik_1993

The problem is that the first request is still with the wrong token.
If you want to solve your problem, you need to create a separate route, when accessed, a new token will be given. It can be made using the get method.
Well, on the frontend, by timer, request a new one and update

W
WebDev, 2019-07-03
@kirill-93

You can disable token verification for Ajax requests or, as mentioned above, request a token by timeout.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question