Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

CSRF token mismatch when a React app logs in to a Laravel Sanctum API

ErrorCSRF 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.

  • Applies to Laravel 12, Laravel Sanctum, axios 1.x, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-cookie
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The browser console reports a CORS problem about credentials, or the response lacks Access-Control-Allow-Credentials.CORS does not allow credentialsFix
    4. 4Everything 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

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

Did this fix it?

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 Headers

Confirmed by raw.githubusercontent.com

Did this fix it?

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-control

2Fix

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 credentials

Confirmed by raw.githubusercontent.com

Did this fix it?

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.stateful

2Fix

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/user

Confirmed by raw.githubusercontent.com

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.