简体   繁体   中英

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.

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM