Why Laravel Sanctum Sessions Break in Nuxt SSR and How to Fix It
When using Laravel Sanctum's SPA cookie authentication with a Nuxt frontend, authenticated requests succeed in the browser but return 401 errors during server-side rendering. This happens because Nuxt's SSR runs inside a Node/Nitro server process that has no cookie jar, no Origin header, and no browser context to attach session cookies automatically. The fix requires manually forwarding the incoming request's cookie and origin headers to outbound server-side API calls using Nuxt's useRequestHeaders utility. Additionally, the API base URL must differ by environment, since internal server processes often cannot reach public-facing addresses like localhost or external domains. State-changing requests also require proper CSRF handling by reading the XSRF-TOKEN cookie and echoing it in the X-XSRF-TOKEN request header.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in