Why auth:sanctum Fails After Initial Login in an Angular SPA
You can successfully hit the /user/csrf endpoint to grab a token, and your login request creates a valid session record in the database. Yet, every subsequent call to a route protected by auth:sanctum returns an unauthenticated response before the controller ever sees it. The browser console confirms that cookies are being sent on these requests.
This specific failure mode usually points to a mismatch between how Laravel is configured for session statefulness and how your Angular application is handling the initial handshake or subsequent navigation. Let's break down the mechanics of what auth:sanctum expects versus what is likely happening in your setup.
The Stateful Domain Configuration Trap
Your environment variables reveal the most common culprit: SANCTUM_STATEFUL_DOMAINS='.appname.local,localhost,127.0.0.1'.
Laravel's Sanctum uses this setting to determine which origins are trusted for stateful sessions (those that use cookies). When a request hits a protected route, Laravel checks if the incoming request originates from one of these domains. If it does, Laravel expects to find a valid session cookie attached to the request.
However, there is a critical nuance in how Angular handles navigation versus direct API calls. If your Angular app is served directly from appname.local:4200, and you are making requests to appname.local:8000, the browser sends the correct domain headers. But if your initial login flow involves navigating away from the Angular app or if the CSRF token logic isn't perfectly synchronized with the session driver, the stateful check can fail silently or explicitly depending on the Laravel version.
More importantly, ensure that SESSION_DOMAIN=.appname.local matches exactly what is defined in SANCTUM_STATEFUL_DOMAINS. If your production environment changes to a different TLD or subdomain (e.g., .myapp.com) without updating both variables, Sanctum will treat the request as untrusted and reject the session immediately.
Verifying the Cookie Path and Name
Another frequent oversight involves the SESSION_COOKIE configuration. You have set this to appnameapi_session. By default, Laravel cookies are scoped to the root path (/). If your Angular app is being served at a specific sub-path (e.g., /dashboard), or if your API routes are prefixed (e.g., /api/v1), you must ensure the cookie path matches.
If you are using SESSION_DOMAIN=.appname.local, the cookie will be sent to all subdomains and paths on that domain. However, if you ever switch to a specific path configuration in .env, verify that the cookie name used by your Angular interceptor (appnameapi_session) matches exactly what is stored in the database and returned by Laravel after login.
Handling the CSRF Token Synchronization
Your workflow involves fetching a CSRF token from /user/csrf before logging in. This is correct for non-prefixed API routes or when using X-CSRF-TOKEN headers. However, if you are relying on the auth:sanctum middleware to validate stateful sessions, the presence of the X-CSRF-TOKEN header becomes less critical than the validity of the session cookie itself.
If your Angular service sends both the cookie and the CSRF header, ensure that the CSRF token stored in your Angular runtime (often in a variable like token) matches the one Laravel expects if you are using stateless validation logic mixed with stateful sessions. In pure stateful Sanctum setups, the session ID itself acts as the primary authentication mechanism once the user logs in.
Debugging the Middleware Flow
To confirm where the rejection is happening, inspect the Laravel request log or enable detailed logging in config/logging.php. Look for messages indicating "Unverified CSRF token" or "No active session found".
If you see an active session ID in the database but the middleware rejects it, check if SESSION_SECURE_COOKIE=false is causing issues in a mixed HTTP/HTTPS environment. If your Angular dev server (localhost:4200) and Laravel backend (appname.local:8000) are on different ports or protocols, ensure that SESSION_SECURE_COOKIE remains false during development to prevent the browser from blocking the cookie entirely.
In summary, the issue almost certainly lies in how the stateful domain is resolved against the incoming request headers or a subtle path mismatch in your session configuration. Aligning SANCTUM_STATEFUL_DOMAINS with your actual deployment hostnames and ensuring the session cookie path covers your API endpoints will resolve the unauthenticated responses.