Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kirchevsky's avatar

Can't solve TokenMismatchException in VerifyCsrfToken.php line 53

After some tries to solve this problem i'm decided to post it there.

What we have...

  1. Laravel default app + Auth + user roles based on Bican Roles.

Not sure what exactly was the reason but after composer update i got the error:

[2015-09-12 23:56:54] local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:53
Stack trace:
#0 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#2 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(54): 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 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#5 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): 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 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#8 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): 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 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#11 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): 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 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#14 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#17 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#18 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#19 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#20 /Users/kirchevsky/web/laravel/tg/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#21 /Users/kirchevsky/web/laravel/tg/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#22 {main}  

Tried all known solutions....

  1. Using form generator so token is generating for the login form automatically.
  2. Disabling CSRF in App\Http\Kernel.php or changing VerifyCsrfToken.php directly can't solve the problem - during login attempt it's redirect to login page (so i can't login)

Maybe somebody had something like this and solved this.

0 likes
35 replies
jrean's avatar

@kirchevsky check your session config path:

'files' => storage_path().'/framework/sessions',

Verify now in project folder structure it is the same.

Remove everything inside the session folder.

Try again.

kirchevsky's avatar

Tried to clear sessions folder, checked config, tried to move sessions to database. The situation the same.

kirchevsky's avatar

When we try to disable csrf protection it's always redirects us to login page. I think that the problem inside Auth middleware but can`t locate it a few days.

P.S. I`m a beginner, so sorry for some stupid questions :)

thomaskim's avatar

@kirchevsky What version of PHP are you running? With Laravel 5.1, you need to be running PHP >= 5.5.9.

kirchevsky's avatar

PHP - 5.6.2 Browser accept cookies - tested with latest Safari and Chrome.

sid405's avatar

@kirchevsky Okay, one matter at at time. Unless you've modified VerifyCsrfToken, or the sessions file in a drastic way, no point looking in there. config/app.php does nothing also in this realm.

You say you disable the csrf protection and you get redirected to the login?

  • where are you disabling it and how? (code, please)
  • which url are you hitting that redirects you to login once you disable csrf?
  • when do you get the original error? Is that during login?
  • "Using form generator so token is generating for the login form automatically." - What does that mean?
  • Have you tried to dump what's being posted during the login? -- IF not place the following in your routes.php instead of
Route::post('auth/login', 'Auth\AuthController@postLogin');

Replace with:

Route::post('auth/login', function(Illuminate\Http\Request $request){
    dd($request);
});

Share your findings then.

Best, sid

kirchevsky's avatar

sif405, thx for your answer.

VerifyCsrfToken is original now, all modifications i made is commented and that was just a try.

I disabled the csrf security in App\Http\Kernel.php by commenting the line:

//        \App\Http\Middleware\VerifyCsrfToken::class,

The '/home' route redirects me to the login page. Yes, the error is just after we pressing button Login. I mean that i don't put token to the form manually, it present in login form and generated by system. Dump - http://pastebin.com/amQiUmRs

sid405's avatar

@kirchevsky It makes sense that you'd be directed to the auth if you hit home. You got the following in your routes file

Route::get('/home',[
    'middleware' => 'auth',
    'uses' => 'MainController@index'
]);

Also now let's try to dump

Route::post('auth/login', function(Illuminate\Http\Request $request){
    dd($request->input());
});

Can i see the login view?

kirchevsky's avatar

sid405, that what we have on dd($request->input()

array:3 [▼
  "_token" => "Z8DpsoKKc6SRAPYnvD0aIEQTn8GfTwvwJUcBWUFC"
  "email" => "xxx@xxx.com"
  "password" => "password"
]

The real data (login+password) replaced with sample data of course.

kirchevsky's avatar

Login view:

@extends ('cleanlogout')

@section('content')
<div class="text-center">
                        <h3 class="page-header">Login</h3>
                    </div>
<div class="text-center">
<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div class="form-group">
        <label class="control-label">E-mail</label>
        <input class="form-control" type="email" name="email" value="{{ old('email') }}">
    </div>

    <div class="form-group">
        <label class="control-label">Password</label>
        <input class="form-control" type="password" name="password" id="password">
    </div>

    <div class="form-group">
        <input class="form-inline" type="checkbox" name="remember"> Remember Me
    </div>

    <div class="form-group">
        <button class="form-control" type="submit">Login</button>
    </div>
</form>
</div>
@stop
sid405's avatar

@kirchevsky And even after you comment out Csrf in Kernel.php you get the same?

php artisan clear-compiled
composer dump-autoload

Other than that idk, i'd have to fiddle with the code myself.

Have you had a search round the forum for a similar issue?

kirchevsky's avatar

The login form from browser:

<form method="POST" action="/auth/login">
    <input type="hidden" name="_token" value="Z8DpsoKKc6SRAPYnvD0aIEQTn8GfTwvwJUcBWUFC">

    <div class="form-group">
        <label class="control-label">E-mail</label>
        <input class="form-control" type="email" name="email" value="">
    </div>

    <div class="form-group">
        <label class="control-label">Password</label>
        <input class="form-control" type="password" name="password" id="password">
    </div>

    <div class="form-group">
        <input class="form-inline" type="checkbox" name="remember"> Remember Me
    </div>

    <div class="form-group">
        <button class="form-control" type="submit">Login</button>
    </div>
</form>
kirchevsky's avatar

I've searched via Google all similar issues, tried all solutions founded on every website and nothing. The forum topic - it's a last thing i've tried :)

sid405's avatar

@kirchevsky OOOOOOKay. Solved. This was an interesting dive down the rabbit hole.

  1. in config/roles.php, remove the white spaces before the <?php tag on the first line. That was preventing your session from being stored and caused that nasty 'Redirecting to ...' screen.

  2. In MainController.php remove the guest middleware

// $this->middleware('guest');

You have 'auth' middleware on the route and guest on the controller. This causes a redirect loop and makes your app unusable

  1. Enable \App\Http\Middleware\VerifyCsrfToken::class in Kernel.php

  2. In config/sessions.php set domain

'domain' => null,

The problem was the cookie stored in the Session was not the one in the form.

  1. in views/auth/login
{ {  csrf_field() } } instead of {!! csrf_field() !!}

That's it. Your app works just fine on my end.

Registers and logs in users with csrf protection as per usual. Now you can continue to implement your ACL

Best. sid

6 likes
kirchevsky's avatar

Will try :) And... you are my hero )))) Thx a lot!

kirchevsky's avatar

Everything works fine. Thank you a lot. Sorry for stupid situation.

mario_ene's avatar

This solution worked for me:

Add {{ csrf_field() }} anywhere in the form.

1 like
HireMedia's avatar

i Have:

-   'files' => storage_path('/framework/sessions'),

-   <form method="POST" action="/auth/register">
        {!! csrf_field() !!}

I Do:

-   domain => 'domain.com' 

-   perm. to 777 on the folder -> framework/sessions

it works.

DougHubbard's avatar

I know this is fixed but I just discovered another "gotcha" that can cause this (here for posterity). A site I manage has 2 laravel projects, both 5.1 with different keys, when the cookie name in config/session.php was the same I could not solve this issue for anything. Updated one of them to a different cookie name and NOW it works.

Hephzebah's avatar

After trying about everything i found on the web, kept skipping one part what was the solution ofcourse.. replaced {{ csrf_field() }} with {!! csrf_field() !!} inside the default generated make:auth login/register form.

Snapey's avatar

@dragonberr

Problem is with new users, not the functionality that has worked perfectly for 5 years.

Perhaps start a new question so that you can relate your specific situation.

dragonberr's avatar

@snapey

well, we'll see! Default package with no edits! will also post as new topic.

PROBLEM:

  1. Register page on submit was going to 419 Page Expired option 1 = (experimented by adding it as an exception to VerifyCsrfToken.php, obviously, once that is done the register page works)

**Note 1: register.blade.php

Already has the token.

@csrf

**Note 2: folder permissions

storage > framework > sessions, etc. all have read & write permission

**Note 3: database

Is working (created an user, etc. with the VerifyCsrf exception)

Next

Please or to participate in this conversation.