Secure your SPA with Laravel Sanctum: Cookie Auth, CSRF, CORS — a practical guide

Introduction
If your website uses a single-page app (SPA) and a Laravel backend, you want authentication that’s secure, reliable, and low-maintenance. Laravel Sanctum’s cookie-based auth gives you browser-managed sessions, CSRF protection, and safer storage than keeping tokens in localStorage — but it needs the right settings to work well.
Why this matters for small businesses
A secure, fast website builds trust and keeps sales funnels moving. Misconfigured authentication can break logins, block analytics, or create support tickets that distract your team. Getting Sanctum right means fewer outages, fewer abandoned carts, and a cleaner user experience — all good for conversions and SEO.
How cookie-based auth works (simple)
Cookie auth means the browser stores a session cookie after a successful login. Laravel uses that cookie plus a CSRF token to make sure requests are genuine. The browser automatically sends the cookie on requests, so your frontend just needs to call a few endpoints in order and include credentials on cross-site requests.
Quick setup steps (non-technical overview)
You don’t need to become a backend expert to understand the essentials. Here are the high-level steps your developer will follow:
- Install Sanctum on the Laravel app and enable its middleware so it can manage CSRF and sessions.
- Configure cookies: mark them Secure (HTTPS), HttpOnly (hidden from JavaScript if possible), and set an appropriate SameSite value.
- Configure CORS to allow your SPA origin and permit credentials so the browser can accept cookies from your API domain.
- On the frontend, call /sanctum/csrf-cookie first, then log in and make authenticated requests with credentials included.
These steps keep things simple for developers while aligning with modern browser standards.
Common problems and how they affect users
Here are real issues that lead to broken logins or mysterious 401/419 errors — and how to fix them:
- Cookie never appears after hitting /sanctum/csrf-cookie: Often caused by CORS settings that block credentialed responses. Use an explicit origin (not "*") and enable credentials on the server.
- Intermittent 419 CSRF errors behind a load balancer: Can happen when sessions aren’t consistent across servers or when clocks drift. Use a central session store, shared keys, or enable sticky sessions.
- Third-party or cross-site requests failing: If an external widget needs to call your API, cookie auth isn’t ideal. Create a limited API token for that integration instead.
These fixes stop the frustrating edge cases that cost time and leads.
CORS and CSRF — plain-English interaction
CORS and CSRF are separate but linked. CORS controls whether the browser will accept responses from another domain. CSRF ensures the request came from your app. For cookies to work cross-origin:
- The server must return Access-Control-Allow-Credentials: true and list the exact origin.
- The browser request must include credentials (fetch or axios uses credentials: 'include').
- If SameSite=None is used for cross-site cookies, Secure=true is required (HTTPS only).
If any of these pieces are missing, the browser will block the cookie or the request, and your users see login problems.
Production-safe defaults (must-haves)
Make these settings part of your deployment checklist to reduce security risk and downtime:
- SESSION_SECURE_COOKIE=true (production only)
- session.same_site='lax' for same-site apps; use 'none' only if cross-site is necessary
- HttpOnly cookies where possible to limit XSS exposure
- Rate limit login endpoints and monitor auth failures
- Use HTTPS everywhere and HSTS headers
Quick checklist for your team
- Backend: Sanctum installed, session driver consistent, CORS allows SPA origin with supports_credentials
- Frontend: call /sanctum/csrf-cookie on startup, use credentials: 'include', test login/logout flows
- Deployment: HTTPS, consistent env vars across nodes, monitoring for 401/419 spikes
Want help or a sanity check?
If you’d rather not babysit these settings, we help small teams implement secure, production-ready auth and site performance optimizations. Read the full technical walkthrough or reach out for consulting: - Our homepage: https://prateeksha.com?utm_source=blogger - Visit our blog: https://prateeksha.com/blog?utm_source=blogger - Full guide on this topic: https://prateeksha.com/blog/laravel-sanctum-spa-cookie-auth-csrf-cors-production-safe-defaults?utm_source=blogger
Conclusion — what to do next
Test your login flows on staging with devtools open: confirm Set-Cookie, XSRF-TOKEN, and that preflight responses include the right headers. If you see intermittent CSRF or missing cookies, follow the checklist above or get a quick audit to stop lost conversions. Ready to lock this down and keep your site converting? Visit our site or the guide linked above and let’s get it working right.
Comments