CSRF token mismatch when a React app logs in to a Laravel Sanctum API
CSRF token mismatch.Short answer
With Sanctum's cookie-based SPA login, Laravel only accepts a state-changing request if the browser first got an XSRF-TOKEN cookie and sends it back with its session cookie. A mismatch means one link broke: no call to /sanctum/csrf-cookie, credentials not sent, CORS not allowing credentials, or the domain not listed as stateful.
Fix it fast
1Find out which cause you have
Open DevTools, Network tab, and look at the failing login request. Check two things: does it carry a Cookie header with the XSRF-TOKEN and session cookies, and does it carry an X-XSRF-TOKEN header? Then fetch the cookie route by hand to see if Laravel answers it.
curl -i https://api.example.com/sanctum/csrf-cookie2Match what you see, then jump to its fix
- 1
The login request has no X-XSRF-TOKEN header and the Network tab shows no earlier call to /sanctum/csrf-cookie.The SPA never asks for the CSRF cookie firstFix - 2
The cookie request succeeds, but the login request has no Cookie header and no X-XSRF-TOKEN header.The browser does not send cookies or the token header cross-originFix - 3
The browser console reports a CORS problem about credentials, or the response lacks Access-Control-Allow-Credentials.CORS does not allow credentialsFix - 4
Everything above is in place, yet login still fails or the user is not remembered afterwards.The SPA's domain is not in Sanctum's stateful list, or the port is missingFix
- 1
What does the error look like?
CSRF token mismatch.Access-Control-Allow-Credentials
Cause 1 of 4
The SPA never asks for the CSRF cookie first
1Check
Search the front-end code for the cookie route. Sanctum expects the SPA to request it before logging in, which makes Laravel set the XSRF-TOKEN cookie.
grep -rn "csrf-cookie" src/2Fix
Call the cookie route and wait for it to finish, then send the login request. Axios reads the XSRF-TOKEN cookie and sends its decoded value in the X-XSRF-TOKEN header.
await axios.get('/sanctum/csrf-cookie');
await axios.post('/login', { email, password });3Verify
In the Network tab the cookie request comes first, and the login request now has an X-XSRF-TOKEN header.
# DevTools > Network > filter 'csrf-cookie' and 'login'Confirmed by raw.githubusercontent.com
Cause 2 of 4
The browser does not send cookies or the token header cross-origin
1Check
When the SPA and the API sit on different origins (for example localhost:5173 and localhost:8000), axios needs two settings to include credentials and the XSRF token.
grep -rn "withCredentials\|withXSRFToken" src/2Fix
Turn both on for the shared axios instance, typically once at start-up.
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;3Verify
Repeat the login and check that the request now carries a Cookie header.
# DevTools > Network > login request > Request HeadersConfirmed by raw.githubusercontent.com
Cause 3 of 4
CORS does not allow credentials
1Check
Look at the response headers of the cookie request for Access-Control-Allow-Credentials.
curl -si -H "Origin: http://localhost:5173" https://api.example.com/sanctum/csrf-cookie | grep -i access-control2Fix
Publish the CORS config and set supports_credentials to true in config/cors.php. Laravel then returns Access-Control-Allow-Credentials with the value True.
php artisan config:publish cors
# config/cors.php
'supports_credentials' => true,3Verify
Run the curl check again and confirm the header is present.
curl -si -H "Origin: http://localhost:5173" https://api.example.com/sanctum/csrf-cookie | grep -i credentialsConfirmed by raw.githubusercontent.com
Cause 4 of 4
The SPA's domain is not in Sanctum's stateful list, or the port is missing
1Check
Sanctum treats a request as a stateful SPA request only when its host is in the stateful option of config/sanctum.php. A URL with a port, such as 127.0.0.1:8000, must be listed with the port.
php artisan config:show sanctum.stateful2Fix
Add the SPA's host (with port if it has one) to the stateful list. If the SPA and API are on different subdomains, also set the session cookie domain with a leading dot in config/session.php.
// config/sanctum.php
'stateful' => ['localhost:5173', 'app.example.com'],
// config/session.php
'domain' => '.example.com',3Verify
Clear the old cookies, then log in again and call an authenticated route.
curl -i -b cookies.txt https://api.example.com/api/userConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Keep one axios instance with withCredentials and withXSRFToken set in a single place.
- Put the SPA host and port into the stateful list as soon as you add a new environment.
- Test login from a clean browser profile, because stale cookies hide this problem.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Security
Laravel answers a form or AJAX call with 419 Page Expired
419 Page Expired (a POST, PUT, PATCH or DELETE request)
Laravel could not match the CSRF token in your request to the one stored in the visitor's session.
5 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fix