70531

laravel 5.1 auth csrf token mismatch

Question:

before make any judgment I read all the related questions related to my problem but none of them fixed it. so here's my problem when I use the authentication facility of laravel 5.1 and want to register a user the csrf token generate twice one when I requesting to show my register form and one when I post the form data to auth/register post route and this cause my to receive a csrf token mismatch exception. here's my register form markup

<form method="POST" action="/auth/register" class="ui large form"> {!! csrf_field() !!} <div class="two fields dirright alignright"> <div class="field" > <div class="ui right icon input"> <i class="user icon"></i> {!! Form::text( 'first_name', Input::old('first_name'), array( 'class' => 'dirright alignright fontfamily', 'placeholder' => 'نام' ) ) !!} </div> </div> <div class="field" > <div class="ui right icon input"> <i class="user icon"></i> {!! Form::text( 'last_name', Input::old('last_name'), array( 'class' => 'dirright alignright fontfamily', 'placeholder' => 'نام خانوادگی' ) ) !!} </div> </div> </div> <div class="field"> <div class="ui left icon input latintext"> <i class="mail icon"></i> {!! Form::email( 'email', Input::old('email'), array( 'class' => 'latintext', 'placeholder' => 'E-mail address' ) ) !!} </div> </div> <div class="field"> <div class="ui left icon input latintext"> <i class="lock icon"></i> {!! Form::password( 'password', Input::old('password'), array( 'class' => 'latintext', 'placeholder' => 'Password' ) ) !!} </div> </div> <div class="ui fluid large primary submit button">ثبت نام</div> <div class="ui error message alignright"></div> </form>

Answer1:

Just add the csrf token as follows in the form :

<input type="hidden" name="_token" value="{{csrf_token()}}"/>

it worked for me.

Answer2:

Assume that your web server has already write access to session directory, in my case <strong><em>'app/storage/framework/sessions/'</em></strong>.

Execute:

$ rm -f {your_web_app}/storage/framework/sessions/*

Answer3:

There are several possibilities...

1) If you have any spaces at all in front of your opening <?php tag, it can cause this error (especially if you're using AJAX). So just double-check to make sure that there's nothing before <?php in your files.

2) If you're trying to submit this form data via AJAX, the docs suggest passing the CSRF token like so:

Add this meta tag to your <head>:

<meta name="csrf-token" content="{{ csrf_token() }}">

And then do this in the AJAX call:

$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });

Answer4:

If your using laravel 5.1 simply adding {{ csrf_field() }} would do the trick

Answer5:

The csrf token will be added automatically if you use the open and close tags for Form

{!! Form::open(['action' => '/auth/register', 'class' => 'ui large form']) !!} -- Form stuff here -- {!! Form::close() !!}

Answer6:

i hope this will help

set meta-tag like follows

<meta name="csrf-token" content="{{ csrf_token() }}">

then request like follows

$.ajax({ data: {data1:'data1',data2:'data2'}, url: '/your/url/goes/here', type: 'POST', beforeSend: function (request) { return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content')); }, success: function(response){ console.log(response); } })

Recommend

  • WPF menu tab navigation
  • Sort a list from Arrays.asList() changes also the origin array?
  • How to keep a date current in Markdown files
  • Primefaces: Ajax status stops on redirection to new page
  • ASP.NET ApiController inside a webform can't reach methods
  • Will HttpClient Async methods run in new threads
  • rvest: extract tables with url's instead of text
  • Add class to parent element
  • Ioniv-v4 : ionic cordova plugin add cordova-plugin-googleplus Not working
  • What happens when one of the Kafka replicas is down
  • Cron and nightmarejs
  • Override all shipping costs for a specific shipping class in Woocommerce
  • Exporting Records from Acumatica via REST Contract-Based API
  • Run git bash script in Windows 7/64bit
  • Error processing job: Project has not enabled BigQuery
  • Videos won't upload
  • How to get a time and Date Separately?
  • Example of using Service Exists MSBuild task in Microsoft.Sdc.Tasks?
  • In metro, get all inherited classes of an (abstract) class?
  • How do I set the logging properties in a spring java configuration?
  • Defer unused CSS
  • Modifying native query cannot have named parameter bindings?
  • Allowing audio files in Spring MVC 3.0?
  • What does “T extends Junk” mean in a generic class in Java?
  • How to use Flask's render_template from an ajax POST form submit
  • Silverlight Event Log in Isolated Storage
  • How do I use libcurl to printf a remote FTP directory listing?
  • Year over Year Stats from a Crossfilter Dataset
  • Google Spreadsheet Script to Blink a range of Cells
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • How to clear a browser cache in Protractor
  • How to turn off notice reporting in xampp?
  • Cloud Code: Creating a Parse.File from URL
  • Android: Unable to detect vertical plane
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to warp text around image in iOS?
  • Google App Engine backend servlet not responding
  • How to get rgb from transparent pixel in js
  • WPF custom control and direct content support
  • ReferenceError: TextEncoder is not defined