Laravel check CSRF manually in controller

Viewed 1537

I'm using Laravel 5.5. When I send POST request from laravel blade. Then everything is ok:

$.post({
    url: 'http://localhost/check',
    data: {
        _token: CSRF_TOKEN,  // I have CSRF token variable above.
        test: "it works"
    }
})

But I want to send that request with socket.io > node.js > laravel :

socket.emit('checkPost', { token: CSRF_TOKEN });

index.js

socket.on('checkPost', function (csrf) {
    request.post({
        url: 'http://localhost/check',
        form: {
            _token: csrf.token,
            test: "it works"
        }
    });
});

With ajax everything is ok. Here I got token missmatch. Why?

When I disable token verification in VerifyCsrfToken for /check route then node request works too.

How can I fix that? Maybe there is controller csrf check method? Then I'll check post csrf in controller.. Thanks in advance and sorry for my bad English.

1 Answers

You missed the underline before the token variable name. Laravel's token input name is "_token" not "token". you must write it this way:

socket.emit('checkPost', { _token: CSRF_TOKEN });

It's just a typo.

Hope this would help.

Related