Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel not setting the XSRF-Token cookie on the live server

I created an app with Laravel 5.7.16 and Angular2 and everything works fine in my localhost.

However, on the live server, I got a "419 error" when submitting post requests. After some research, I concluded that this would likely be due to an issue with the XSRF-TOKEN. Then, when comparing what was going on in my localhost and on the live server, I noticed that the live server is not setting the XSRF-TOKEN cookie on the initial request.

I am not explicitly setting the XSRF-TOKEN on my Angular2 HTTP post request, but from what I understood, Angular2 does this automatic, as long as a XSRF-TOKEN cookie is initially setting by the server side application.

Does anyone have any ideas on how I can solve this?

Edit: I have just tried adding one of my application post routes to the $except array of VerifyCsrfToken, and that works. However, this is more of a workaround, rather than the actual solution. What is interesting is that, after executing the "allowed" route, the XSRF-TOKEN is returned, and all other routes work. I think this reinforces the idea that the issue is that the XSRF-TOKEN is not being returned in the initial application load. Why would that be?

Thank you.

like image 382
vjoe Avatar asked Nov 17 '25 16:11

vjoe


2 Answers

Hello I had the same problem on my local machine. XSRF-TOKEN cookie was not getting set on browser. I tried the following steps and problem was solved.

  • Give permission to folders. sudo chmod -R 777 storage bootstrap/cache
  • Clear config and application cache. php artisan cache:clear && php artisan config:clear
like image 123
Iqbal Hussain Avatar answered Nov 20 '25 05:11

Iqbal Hussain


Try clearing your Laravel caches

php artisan cache:clear

And also clear your browser cookies to make sure there isn't a session stored already?

like image 38
Kiksy Avatar answered Nov 20 '25 06:11

Kiksy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!