katedev29 Jul 2025 05:42

Spent three days debugging Sanctum SPA auth issues on a Vue + Laravel setup. Documenting the mistakes for others.

The CSRF cookie flow is conceptually simple but breaks in non-obvious ways.

Replies (6)
ivan_morozov29 Jul 2025 05:53

Most common mistake: calling /sanctum/csrf-cookie before the login request, then making the login request without including the XSRF-TOKEN header. Axios sends it automatically if you have withCredentials: true and the cookie is present. Check browser dev tools, Network tab.

0
alex_petrov29 Jul 2025 07:24

CORS is the second common issue. SESSION_DOMAIN in .env needs to match the subdomain relationship between API and SPA. If SPA is on app.example.com and API on api.example.com, SESSION_DOMAIN=.example.com with the leading dot.

0
sergey_web29 Jul 2025 08:29

stateful_domains in sanctum.php config must include the domain where the SPA is served. This determines which requests use cookie-based auth vs token auth.

0
vova29 Jul 2025 10:01

Running SPA on localhost:3000 and API on localhost:8000 breaks same-origin cookie rules. You need to proxy /api through the SPA dev server (Vite proxy config) to make them appear same-origin.

0
katedev29 Jul 2025 11:55

In production, session cookies need Secure and SameSite=Lax. SameSite=None requires Secure (HTTPS). Getting this wrong means cookies are silently dropped by modern browsers.

0
dmitry_kv29 Jul 2025 13:53

For mobile apps or non-browser clients, use Sanctum tokens (not cookie auth). SPA cookie auth is specifically for browser same-site scenarios. Mixing the two approaches on the same client causes subtle bugs.

0
Write a reply
Markdown. ```php blocks are runnable.