I am developing a single page application using Extjs 5 Javascript framework using Laravel 5 as an API. The problem is every time I make a POST request I get TokenMismatchException because of the 'VerifyCsrfToken' middleware in Kernel.php. Is there any way to include the csrf token from my javascript app.
Thanks @bestmomo, @Iss: They are indeed good articles. But I am doing a cross origin request from a separate extjs 5 app to a laravel api hosted on another server. Do you have any clue on that please?
@bestmomo Yes I am using that package from barryvdh and it works. The problem is when I make a POST request to the api that i get a tokenmismatch exception.
Have a separate route that authenticates the user via an api key and in response you give the csrf_token which it will store on the requesting/posting server
You have example.com POSTing to api.com and it requires a csrf token
Have a route on api.com, /auth that will either authenticate the user and return csrf_token() or just return the token on a GET request and have example.com add the result as a X-CSRF-TOKEN header when POSTing
@binalfew does the response.responseText contain an encrypted version of the token?
Try logging in the route you're posting the token you're sending and the token it's trying to compare against (do it in the middleware) see if they match (my guess is they don't since you're still having the issue).
My guess the laravel app you're posting to is registering 2 separate sessions for the get and the post.
Try the second approach and alter the middleware. See how that goes
@Iss The token I get from response.responseText is not encrypted. I have been debugging in the middleware; it fails while comparing the token I submit with the form with the one stored in Session->token(); But the last point you mentioned, is it possible for the laravel app to register two separate sessions for GET and POST? If so, how can I access the one registered for POST.