4

Well this is the problem I am facing from yesterday. It is always giving me TokenMismatchException and when I digged in and compared a few things, I found that on my local server, the _token field never changes. But on my production, it does. And that's the reason it kept giving me TokenMismatchException. Does anyone know how to fix this error.

I have

  1. seen this question
  2. Went through documentation.
  3. Wrote several codeception tests.
  4. <input id="token" type="hidden" value="{{ csrf_token() }}"> this already in my code.
1
  • [Here is the code](view-source:cornchat.ga/auth/register). Check line number 132. It's always changing. Commented May 28, 2015 at 7:04

2 Answers 2

7

Check if you have domain in the config/session.php setup to the right path. Even I had got the same problem. And resolved it just by changing that path.

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

Comments

0

May be usefull.

Html:

<meta name="_token" content="{{ csrf_token() }}">

Js:

var network = {
    post: function(path, params, cb, type){
        $.ajax({
            url: path,
            type: 'post',
            data: params,
            headers: { "X-CSRF-TOKEN" : $('meta[name="_token"]').attr('content') },
            dataType: type,
            success: function (response, status) {
                if (status == "success") {
                    if (response.reason == "token_timeout") {
                        var new_token = response.new_token;
                        $('meta[name="_token"]').attr('content', new_token);
                        network.post(path, params, cb, type);
                    }else{
                        cb(response);
                    }
                }
            }
        });
    }
};

network.post('path to handler...', { key: value... }, function(response){
   if(response.status == 'success'){
       // to do
   }
}, "json");

/app/Exceptions/Handler.php:

    public function render($request, Exception $exception) {

        if ($exception instanceof \Illuminate\Session\TokenMismatchException) {
            return response()->json(['reason' => 'token_timeout', 'new_token' => csrf_token()], 200);
        }

        return parent::render($request, $exception);
    }

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.