#Getting CSRF Token mismatch only on my first load after login in my Laravel 8 application

1 messages · Page 1 of 1 (latest)

sage nimbus
#

I've got a form that has some fields that their values are loaded in by an ajax call to a controller after the first input is filled.

I have added the token as an input to my form with "@csrf" in my blade file.

To make the Ajax call I'm sending the CSRF token in the data section.

The first time I log in my application and load the page where the form is located I get the "CSRF Token Mismatch" error with status code 419 when doing the Ajax call. If I reload the page normally with F5 key or just clicking the reload button on my browser, it works flawlessly.

What is producing this behaviour in my application and how can I fix it?

I know we can bypass this issue by disabling the token protection for this route, but that's the last thing I want to do.

muted robin
sage nimbus
#

Hi, thanks for your response. What could I do if i'm getting the head section of my page from another blade file? Can I add new meta tags to the head from my current blade file? @muted robin

woven ocean
#

if that's layout yes, you can use stack / push mechanism to do so

sage nimbus
#

Thank you, will give it a go

sage nimbus
#

I just tried and had no luck with it.

#

What happens is that the first time I log in and make an ajax call I will receive error 419 and Token Mismatch. If I reload the page the token we are sending will change and it will work ok

#

If I go into private browsing mode I will have a different token than the last time I tried and I will have same error. If I reload right there it will work without problems. And like this every time

#

It is very weird and my teammates and I don't know how to solve this