laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.24k stars 10.92k forks source link

[5.3] "TokenMismatchException in VerifyCsrfToken.php" in Laravel's auth form #15040

Closed MountainDev closed 8 years ago

MountainDev commented 8 years ago

I use fresh installation of Laravel 5.3. I did the following steps in my Homestead:

laravel new blog php artisan make:auth entered proper database configuration in .env php artisan migrate

That's all I did. Everything wen smoothly but when I submit register form I get: TokenMismatchException in VerifyCsrfToken.php line 67:

I tried to clean cache and cookies, use different browsers and install Laravel again (also via composer). Some people from Laravel's IRC Chat also confirm that bug too.

subin7 commented 7 years ago

This is not a bug. Just clear localhost cookies on your browser every time you need to switch to other laravel projects.

digitlimit commented 7 years ago

I encountered same problem but I was able to resolve it by ensuring the following keys in .env are correct

  APP_URL=http://mylocalsite.dev
  SESSION_DOMAIN=mylocalsite.dev
digitalhuman commented 7 years ago

@digitlimit Exactly. Good point. Added 'session' part to the list above.

et4m1r commented 7 years ago

same problem here. why closed this issue. i tried all default installation. but still showing "TokenMismatchException in VerifyCsrfToken.php line 68:"

ttimot24 commented 7 years ago

Okey suddenly I got this error too. But only in one route. I'm trieing to solve this about 3 days. I googled everything and tried what others wrote but nothing works. Laravel creates a new session everytime I load the page and the datas I stored in session are lost. Any suggestion?

digitalhuman commented 7 years ago

Did you checked all options that I put in that post?

Get Outlook for iOShttps://aka.ms/o0ukef

On Mon, Nov 28, 2016 at 10:04 PM +0100, "Timot Tarjani" notifications@github.com<mailto:notifications@github.com> wrote:

Okey suddenly I got this error too. But only in one route. I'm trieing to solve this about 3 days. I googled everything and tried what others wrote but nothing works. Laravel creates a new session everytime I load the page and the datas I stored in session are lost. Any suggestion? Unfortunately it makes my app unusable after weeks of working and I can't finish the projects to my clients ... :(

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/laravel/framework/issues/15040#issuecomment-263393717, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAvqMLGltdmSZYoiX9LbjRbNK1KmKXp2ks5rC0HzgaJpZM4Js_eC.

ttimot24 commented 7 years ago

Permissions are OK. Encrypt enabled. app_url and session_domain are the same. Session is generated. But the session token and the token that the form sends not the same.

ghost commented 7 years ago

@ttimot24 What is your Environment like? Are you using Shared hosting, dev'ing locally? Are you using Vagrant or Virtual Box? I'm on the side that, moving files from local desktop environments to github and back etc might be the issue.

digitalhuman commented 7 years ago

@ttimot24 Yeah that is exactly what I expect it to do. I really would like to know what your environment is like and where you develop on. Did you try settings session to 'file' and see if the form works after that? Another option is to check if that /url/ is begin cached or not. I had some unexpected behaviour with that as well.

ttimot24 commented 7 years ago

I found the problem, there was a Session:flush() in one of my middleware. Thanks for the help! :)

mairesweb commented 7 years ago

I found the solution by giving permission on the storage folder.

keebeegee commented 7 years ago

switched to db session management as described here: http://stackoverflow.com/questions/30338518/persisting-sessions-across-subdomains-in-laravel-5/39741256#39741256

I suppose in my case the issue was related to file permissions.

NatLuder commented 7 years ago

Hello everyone,

I am trying to figure this issue out aswell, but I am experiencing some troubles... So far I have tried:

Login and register forms work without any problems. It's just my custom form (which only can be accessed when logged in) which won't work (with and without csrf token). TokenMismatchException in VerifyCsrfToken.php line 68:

How can I check the other points about the session? I am still very new to Laravel 5.3, so I am sorry if I don't know how to check the session values.

My workstation: I am working on a Mac OS Sierra 10.12.2 with PHPStorm and Laravel 5.3, VueJS and Bulma (no Bootstrap). My local server works with MAMP.

Zedonboy commented 7 years ago

Hey happy new year everyone. have got same problem too Its funny that i cant see any permissions, encryption property, SESSION_DOMAIN in .env file

i use laravel 5.3.22 with xampp

Zedonboy commented 7 years ago

i dont know my. env file is different or what?

Zedonboy commented 7 years ago

@Natluder i have solved mine.. ........ sometimes when we code we make silly mistakes........Go to your html form that is either the sign up or register form in the form tag add {{csrf_field()}} in it

Zedonboy commented 7 years ago

i pray this helps you

NatLuder commented 7 years ago

@Zedonboy Happy new Year to you too!

As I said before, register and login work. I am using this form of linking my CSRF token anyway, so I already have this. But I also tried it the other way with a hard coded CSRF token in a hidden input.

Zedonboy commented 7 years ago

@Natluder are you using scalfolded auth in laravel for your login and Registration?. ..... if not i would love to see your code

Zedonboy commented 7 years ago

honestly most of the time such errors comes from the form itself.. ....... for me

NatLuder commented 7 years ago

@Zedonboy I have used php artisan make:auth as always...

My custom form for creating a project looks like this:

<form role="form" method="POST" action="http://dev.project/create" class="control is-horizontal"> {{ csrf_field() }} <div class="addmenu"> <div class="control is-horizontal"> <div class="control-label"> <label class="label">Name</label> </div> <div class="control is-fullwidth"> <input id="name" type="text" name="name" required="required" autofocus="autofocus" class="input"> </div> </div> <div class="control is-horizontal"> <div class="control-label"> <label class="label">Description</label> </div> <div class="control is-fullwidth"> <textarea id="description" name="description" rows="4" class="textarea"></textarea> </div> </div> </div> <div class="control is-horizontal btn-pull-right"> <button type="submit" class="button is-primary"> <span class="icon"><i class="fa fa-plus-square"></i></span> <span>Create</span> </button> </div> </div> </form>

Usually I copy & paste forms from project to project (the base tags). So I don't really get why this should be wrong while all others work...

The web.php Route is as follows: Route::post('/add', 'ProjectController@create');

And the Controller function ProjectController create:

public function create(Request $request) { if ($request->input('name') !== null && $request->input('description') !== null) { $event = new Event(); $event->name = $request->input('name'); $event->description = $request->input('description'); $user = Auth::user(); if($user) $event->user_id = $user->id; $event->save(); } return self::index(); }

Zedonboy commented 7 years ago

well have printed your request in json format and _token is the same. everything seems ok in my machine .

Zedonboy commented 7 years ago

What sort of bug is this?

Zedonboy commented 7 years ago

@Natluder remove the X-CSRF-TOKEN at the hearder

Zedonboy commented 7 years ago

coz am trying to read the code of VerifyCsrfToken. php@handle function.... check whether your app match to 4 conditions...............#NOTHING WAS EASY EVEN PROGRAMMING

Zedonboy commented 7 years ago

@Natluder from what am analyzing here.....VerifyCsrfToken@tokenmatch function, $sessionToken and $token.. ......may not be equal

Zedonboy commented 7 years ago

Logically i suggest do something to the $sessionToken by.....maybe flush your sessions, cache , refresh your browser.. .....to initialize the $sessionToken.. ..

NatLuder commented 7 years ago

@Zedonboy, It's getting weirder... I flush my session now at logout (via "/logout" route). I cleared my cache via php artisan cache:clear. Now when I login, go to a different page I directly get logged out again. I think something is completely wrong here, but I do not get what it is. Also I created three times a new project, php artisan make:auth, copied parts of my project (very basics like welcome.blade.php and style.css) and it's the same...

Zedonboy commented 7 years ago

@Natluder this is really a fluke not a bug for the fact you have tried in many fresh project...... i guess its from you.. .. Aiit lets try unconventional means

  1. try changing your form action to just "create" dont put any http//dev.project
  2. try the using the most simplest form no css just
    {{csrf_token()}} .......to check the bug 3.disable the CSRFTOKEN middleware for the meantime

try each step before proceeding to the next

artus9033 commented 7 years ago

Sometimes getting same error. Tried changing the session driver to database, but didn't help. So, finally, is there any fix?

gmenti commented 7 years ago

Possible solution: (WORKED FOR ME)

artus9033 commented 7 years ago

Ok, for me, changing the session driver to database in config almost fixed the problem, sometimes the error appears again, but it happens only after refreshing a page and editing the source; then, you just need to clean the browser's cache

maurocasas commented 7 years ago

Check your .env file for APP_URL and SESSION_DOMAIN; these two needs to match exactly.

wynsto commented 7 years ago

I donot like this error.

wynsto commented 7 years ago

Why you guys wont fix it before release

M0H3N commented 7 years ago

I have this problem also . when I want to test project in new computer this error happened and I must to delete all browser sessions to fix the problem

frdteknikelektro commented 7 years ago

Mine is happen only when i check remember me when login.

I hope it can be fixed *But currently using sqlite

ivan-grozni commented 7 years ago

I also have this issue but only in Chrome. Both in Ubuntu 14 and Windows 10. PHP7.

ellisio commented 7 years ago

Just a little interesting fact: This error stopped occurring once I stopped using Vagrant and switched to Docker. :)

jerauf commented 7 years ago

I have no idea how to fix this. I've done everything on this thread, I've done fresh installations of Laravel. And it's not resolving.

Any other ideas?

wynsto commented 7 years ago

hey guys, check the Session domain in your .env or config files. It should be same with the host name when you access it in your brower.

jerauf commented 7 years ago

There's no session domain in the env file by default. There's an app domain and mine is set correctly.

wynsto commented 7 years ago

I fixed this error by add SESSION_DOMAIN= somedomain in my .env file

jerauf commented 7 years ago

Didn't work.

wynsto commented 7 years ago

is there a 'domain' => env('SESSION_DOMAIN'),

In your config session file?

jerauf commented 7 years ago

Yes. And it matches.

ivan-grozni commented 7 years ago

Didn't work for me either :/

On Sun, Feb 12, 2017 at 9:06 AM, Jeremy Aufderheide < notifications@github.com> wrote:

Yes. And it matches.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/laravel/framework/issues/15040#issuecomment-279232211, or mute the thread https://github.com/notifications/unsubscribe-auth/AFMEiVpm6dVj6TImrk2az7c-JCWeDDHnks5rbzwOgaJpZM4Js_eC .

ellisio commented 7 years ago

Those having problems. Are you making calls over AJAX? If so, have you ensured that you're passing the XSRF-TOKEN header? If not, your application is probably generating a new one every request causing this error. We also noticed this as we had a broken Interceptor on vue-resource.

https://laravel.com/docs/5.3/csrf#csrf-x-csrf-token

CF-FullStackDev commented 7 years ago

If you are using AJAX add this command: $.ajax({ headers : { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); Working in resources controller...

Hope it Helps!

ivan-grozni commented 7 years ago

That fixed it for me, thanks!