Quick Answer
A Laravel Sanctum 419 on SPA cookie auth often means the CSRF token never matched — not that your password is wrong. If you excluded XSRF-TOKEN from cookie encryption so the frontend could “read it,” Laravel 11/12 still decrypts the X-XSRF-TOKEN header. Decrypt fails, the token becomes empty, and every stateful POST dies with CSRF token mismatch.
Quick Facts
| Item | Details |
|---|---|
| Topic | Laravel Sanctum 419 / XSRF-TOKEN encryption |
| Category | Laravel / Sanctum / SPA Auth |
What Is a Laravel Sanctum 419?
It’s not “Sanctum is broken.” HTTP 419 here means Laravel’s CSRF middleware rejected the request before your login controller ran. For first-party SPAs, Sanctum uses session cookies: you hit /sanctum/csrf-cookie, then send mutating requests with the session cookie plus an X-XSRF-TOKEN header copied from the XSRF-TOKEN cookie.
If that header doesn’t match the session’s _token, you get {"message":"CSRF token mismatch."}. Bad credentials would be 401 from the controller. 419 means you never got that far.
Why It Matters
- Total write outage — login, logout, and every cookie-session POST can fail at once.
- Misleading debugging — teams chase CORS, Redis, Cloudflare, and “cookie not set” for hours.
- False “fixes” — excluding
XSRF-TOKENfrom encryption looks clever and breaks Laravel’s own CSRF path.
How It Works
Three pieces have to agree on one token value.
- Session — Laravel stores
_tokenin the session (often Redis). - Cookie —
XSRF-TOKENis set for the SPA to echo. With normalEncryptCookies, that cookie is encrypted on the way out. - Header — the SPA sends
X-XSRF-TOKEN(usually URL-decoded cookie value).VerifyCsrfToken::getTokenFromRequest()decrypts that header, then compares it to the session token.
If the cookie was left plaintext because you called something like encryptCookies(['XSRF-TOKEN']) as an exclusion, decrypt() throws, Laravel sets the token to '', and tokensMatch() fails → 419 every time.
The SPA does not need to understand the cookie bytes. It only needs to copy them back. Encryption on the wire is fine.
Step-by-Step Guide
Step 1: Prove it’s CSRF, not credentials
With a fresh cookie jar:
GET /sanctum/csrf-cookie
POST /api/login (or your SaaS login) with session cookie + X-XSRF-TOKEN
Done check: if you see 419 CSRF token mismatch, CSRF failed. If you see 401 with a credentials message, CSRF passed and the controller ran. That’s the split you want when testing a fix.
Step 2: Inspect the XSRF-TOKEN cookie shape
After /sanctum/csrf-cookie, look at XSRF-TOKEN:
- Short plaintext-looking value (e.g. ~40 hex chars) while you excluded it from encryption → suspicious.
- Long encrypted payload (hundreds of chars) → normal Laravel encrypted cookie.
Also confirm Redis/session _token exists and is stable across the GET and POST. If session and cookie generation look fine but POSTs still 419, focus on decrypt of the header.
Step 3: Remove the EncryptCookies exclusion
In Laravel 11/12 bootstrap middleware setup, delete any exclusion that keeps XSRF-TOKEN plaintext “so the SPA can read it.” Let EncryptCookies encrypt it like other cookies.
On the SPA: after csrf-cookie, read document.cookie (or your cookie helper), decodeURIComponent the value, send it as X-XSRF-TOKEN. Don’t keep a stale Nuxt useCookie copy from an earlier page load.
Done check: same bad-password login now returns 401, not 419. Good password returns 200. Cookie length jumps to the encrypted form.
Real-World Example
Stack: Laravel 12 API + Nuxt SaaS SPA, Sanctum cookie sessions, Redis session driver, Cloudflare in front. Symptom: every stateful write on the SaaS panel and tenant apps returned 419 — including login.
We ruled out the usual suspects first. Session id stuck. Redis _token matched what we thought the cookie was. Encoding tricks (%3D vs =) weren’t the root cause.
The smoking gun was in VerifyCsrfToken: the header path decrypts. We had excluded XSRF-TOKEN from encryption with a comment that the SPA needed plaintext. That assumption was wrong. Removing the exclusion fixed production: encrypted cookie out, decrypt in, CSRF passes, controller finally answers 401/200.
Related trap on the same project: the SPA once rewrote only the hostname on a baked http://local:8080 API URL and produced http://saas…:8080 on HTTPS. That’s a separate origin bug. Fix CSRF encryption first; then make sure the browser calls the real same-origin https API.
Pros & Cons
Advantages
- Keeping XSRF encrypted matches what Laravel’s CSRF middleware expects.
- One server change restores login and all cookie-session writes.
- Easy regression signal: bad login must be 401, never 419.
Disadvantages
- Cookie value is opaque — fine for echo-only clients; bad if you wrote custom JS that assumed a raw session token string.
- You still must decodeURIComponent the cookie before putting it in the header.
- Wrong API origin / mixed content will still break CSRF even after this fix.
Best Practices
- Treat 419 vs 401 as different bugs: CSRF vs credentials.
- Never exclude
XSRF-TOKENfromEncryptCookieson modern Laravel. - Re-read the cookie after
/sanctum/csrf-cookie; don’t trust a cached client token. - Send credentials/cookies on CSRF + API calls; match
SANCTUM_STATEFUL_DOMAINSto the real SPA host. - Add a smoke test: stateful POST with echoed XSRF must not 419.
Common Mistakes
- Plaintext XSRF “for the SPA” — why: old blog advice / wrong mental model. Fix: encrypt; SPA only echoes.
- Forgetting decodeURIComponent — why: cookie has
%3D. Fix: decode beforeX-XSRF-TOKEN. - Blaming Sanctum when Origin is wrong — why: baked
http://host:8080on HTTPS. Fix: use page origin / correct public API URL. - Testing without the session cookie — why: request isn’t stateful, CSRF never runs, you see 401 and think CSRF is “fine” while the browser path still 419s.
Frequently Asked Questions
What causes Laravel Sanctum 419 CSRF token mismatch?
The CSRF token from the header/session didn’t match. Common SPA causes: wrong/missing X-XSRF-TOKEN, stale cookie, domain/stateful config mistakes, or decrypt failure when XSRF-TOKEN was left unencrypted.
How do I fix Sanctum 419 on SPA login?
Call /sanctum/csrf-cookie, echo the (decoded) XSRF-TOKEN as X-XSRF-TOKEN, keep cookies on. On Laravel 11/12, do not exclude XSRF-TOKEN from cookie encryption. Confirm bad logins return 401, not 419.
Sanctum vs Passport for this issue?
This 419 path is about cookie-session CSRF for first-party SPAs — Sanctum’s model. Passport/OAuth token APIs don’t use this XSRF cookie flow the same way. See Sanctum vs Passport for which auth to pick.
Is excluding XSRF-TOKEN from encryption ever required?
Not for a normal Axios/fetch SPA that only copies the cookie into a header. Modern Laravel expects that cookie to be encrypted and the header to be decrypted.
Summary
If Sanctum SPA writes all 419, check whether XSRF-TOKEN was excluded from encryption. Laravel decrypts X-XSRF-TOKEN; plaintext cookies fail that step. Remove the exclusion, decode the cookie in the client, and use 401-vs-419 as your done check. For the wider Sanctum setup, see Laravel Sanctum API authentication.
Key Takeaways
- 419 means CSRF failed; 401 means the controller ran.
- Laravel decrypts
X-XSRF-TOKEN— keepXSRF-TOKENencrypted. - The SPA should echo and decode the cookie, not “interpret” a plaintext token.
- Prove the fix with a bad-password login that returns 401.
- Separate origin/HTTPS mistakes from CSRF encryption mistakes.

Comments
0 comments · new ones appear after approval
No comments yet. Be the first to share your thoughts.
Leave a comment
Your comment will be reviewed before it appears.