0

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>
2
  • you need to post more on how you submit the form (js snippet), the html form looks ok. Commented Aug 14, 2015 at 16:56
  • I know this post is getting old, but it came up first in my Google search. I had a problem where the token was invalid and I couldn't figure out why. The issue for me was that there was javascript code that was setting form inputs to disabled. The js script was also marking the _token field as disabled so it wasn't getting sent along with the POST. Hope this is useful to someone else. Commented Oct 21, 2015 at 18:13

6 Answers 6

1

Just add the csrf token as follows in the form :

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

it worked for me.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Execute:

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

Comments

0

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')
    }
});

1 Comment

no I don't submit it via ajax but about <?php opening tag can you explain more please?
0

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

Comments

0

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() !!}

Comments

0

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);
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.