Technical document

Designing Secure SPA Authentication with Laravel Sanctum

We will organize how to handle Cookie, CSRF, CORS, session expiration, and permission checks in SPA authentication using Laravel Sanctum.
Authentication / Laravel~10 minPublication Date September 22, 2026Update date September 22, 2026
Designing Secure SPA Authentication with Laravel Sanctum

Summary

Key points of this document

  • Where are the design challenges?
  • How to define boundaries
  • Details that matter in implementation

Overview

SPA authentication is not secure just because the login screen works. Cookie, CSRF, CORS, session expiration, and permission checks must be designed as a single flow. Sanctum's SPA authentication is difficult not only because it has many configuration items, but also because browser Cookie, CSRF, CORS, and session expiration work together as one.

Where are the design challenges?

Even with Laravel Sanctum, if you widely allow CORS, omit CSRF token acquisition, or let all APIs pass if authenticated, the security of the SPA will decrease.

Domain and HTTPS conditions differ in local development, staging, and production. If Cookie attributes, SameSite, Secure, and session domain settings deviate for each environment, login issues or unintended transmissions will occur.

Laravel Sanctum makes SPA authentication easier to handle, but when it doesn't work, the cause isn't solely in the Laravel code. SameSite, Secure, domain, CORS, proxy, and the presence/absence of HTTPS are involved, leading to failures that are difficult to reproduce due to environmental differences.

How to define boundaries

The authentication flow is considered by dividing it into CSRF Cookie acquisition, login, session confirmation, permission acquisition, and API calls. The frontend not only checks if it's logged in, but also acquires available functions from the API, separating display control and API permission checks.

In the design, authentication is defined not as a single login API, but as a flow of CSRF Cookie acquisition, login, authenticated user acquisition, logout, and expiration. The React side also treats 401 not as a mere error, but as a change in session state.

Details that matter in implementation

On the Laravel side, Sanctum's stateful domains, CORS allowed origins, and session Cookie settings are managed with environment variables. On the React side, 401 and 419 are distinguished, with session expiration leading to re-login, and insufficient permissions treated as a permission error.

In implementation, for the API client,credentials: includeis fixed, and CSRF acquisition is made an explicit step before login. On the Laravel side, stateful domains, session domain, CORS origins, and SameSite are managed with environment variables, absorbing the differences between production and local environments through configuration.

  • Distinguish between 401, 419, and 403, and do not display unauthenticated, CSRF mismatch, and insufficient permissions with the same message.
  • When logging out, destroy the server session and clear client-side cache.
  • In the admin panel, verify user restoration not only immediately after login but also upon page reload.

Observing failure modes

During verification, test unauthorized requests from different origins, POST without CSRF, expired sessions, API calls after logout, and updates with insufficient permissions. Also check Cookie attributes in the browser.

During verification, confirm whether Cookies are sent in HTTPS environments equivalent to local, staging, and production. Specifically, look at conditions close to separate subdomain configurations, behind reverse proxies, and browser third-party Cookie restrictions.

Discarded options and tradeoffs

While Cookie-based SPA authentication works well with browsers, it requires a correct understanding of CORS and CSRF settings. If the main purpose is mobile apps or external API exposure, a token-based approach might be more suitable.

While Cookie-based SPA authentication is compatible with browser standards, it is sensitive to environmental settings. The Bearer token method is easier for API clients to handle, but increases storage location and leakage risks. For admin panel use cases, correctly designing Cookie and CSRF is valuable.

Decision criteria to keep in mind

Sanctum is a tool that makes SPA authentication easy to start, but expanding settings too broadly reduces security. Treating Cookie, CSRF, CORS, and permissions as a series of contracts is a prerequisite for stable operation.

Sanctum authentication is not about enabling Laravel features, but about aligning the trust boundaries between the browser and the server. Being able to observe failure codes separately improves the maintainability of authentication.

Technical documents

We will continue to increase technical documentation.

We will continue to organize our thoughts on AI, cloud, business app development, requirements definition, and operational design into documents.

Go to technical documentation list