-
Hi there, I am trying to get the login flow of my web app working. The web app is written using Nuxt 3 for the frontend (SPA) running on Laravel specific info:
Because Nuxt 3 is using the fetch API and axios is currently not available for Nuxt 3 I am trying to get the login flow with Laravel Sanctum and the fetch API to work. I am calling the Laravel Backend using a composable function like this:
Then I call it in a LoginForm-Component:
The problem is that I get 419 errors (csrf mismatch) back from the login route although the X-XSRF-Token is set in the request headers (like described in the Laravel Sanctum docs): Is it possible that Laravel Sanctum handles the Header name case-sensitive and so it can't find the token? As far as I know the fetch API always sends header names in lowercase and there is no way to change this behaviour. The cookie is set correctly by the Laravel Backend: The cookie data seems to be OK, too (response from a request I made some minutes ago): I also tried setting the correct timezone in These are the Sanctum values inside my
I don't know how to investigate any further. I already wrote the problem details over at stackoverflow - so sorry for cross-posting but I did not get a working answer yet and so I give it another try over here. Because mostly all examples use axios or API tokens I am stuck with the problem for days now. I'd be happy for any help! Update
For me this looks like Fetch API is the problem here, because the request seems to be blocked by Laravel Sanctum anytime with 419 (csrf token mismtach). Anyone has any clue how to fix this? Or is it a bug? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
have you tried removing the |
Beta Was this translation helpful? Give feedback.
-
I just got this working today and had the same scenario. I believe the missing piece is you need to reference your X-XSRF-TOKEN as: My Stack is:
ENV
API Users Controller
Registration (Nuxt 3)
Login Nuxt 3
User Request
Other thing to note, make sure in your Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hey there, So this example token is sent by Fetch API: And this example token is sent by Axios: There is a very small difference at the end of the token string (= vs. %3D). Laravel itself sends the Token value URIEncoded (with %3D) but seems to expect the token with = at the end. There is nothing about that in the docs but this issue kept me searching for days and was not that obvious. I changed my code to:
And now the login is working using the standard Fetch API. I hope this helps someone else debugging their login flow. Greetings. |
Beta Was this translation helpful? Give feedback.
Hey there,
I misinterpreted my error logs and found that there is another small difference using Axios and Fetch API:
Axios seems to decode the X-XSRF-TOKEN whereas Fetch API does not.
So this example token is sent by Fetch API:
eyJpdiI6IkRScW9GNGtIbndWWFdKbzRiN0VWVkE9PSIsInZhbHVlIjoiNUNuQStiZGt0Y3l0cXhReThHSEJxbCsxRmZvbFFHKzhQV3ArSlg0cjVQVm5qSDZoQ3ZsSERnUTREUXRvczdMOHhYSHFqbm5FUjd2dFpGMlN4bU81NWJ5SWpmem5BQi9vRmJBRFZZWmFSZFlqcHNxbTZ5N1Z0cGJzSmMwcUFRaXUiLCJtYWMiOiI4MjM1MTQ2ODExNzhlY2ExNDk1NDhhOWEwNzE0OWJlMzViOGQxNDJhMTY0YTI2NzYwMThjMzQ5ODVmMDYwMjk1IiwidGFnIjoiIn0%3D
And this example token is sent by Axios:
eyJpdiI6IisydDZOOFJoREp5ZnZudUtjRk1teXc9PSIsInZhbHVlIjoiV1dQdjJDZlNrcW43Zlg4TW1yRFdj…