Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Axios sends no cookie to an Express API even with withCredentials set to true

ErrorThe request has no Cookie header although withCredentials is true

Short answer

withCredentials only tells the browser to attach cookies. For a cross-site call the server must also answer with an explicit origin and Access-Control-Allow-Credentials: true, on the preflight too, and the cookie itself must be allowed across sites. Check the request and response headers in the Network tab first.

  • Applies to axios 1, Express 5, cors middleware for Express, React 19, current desktop browsers, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the request in the browser's Network tab: is there a Cookie header, and does the response carry Access-Control-Allow-Origin and Access-Control-Allow-Credentials? You can ask the server the same question from a terminal, sending the Origin your front end uses. The headers you get back decide the cause.

    curl -i -H "Origin: http://localhost:5173" http://localhost:3000/api/me
    # look for: access-control-allow-origin and access-control-allow-credentials
  2. 2Match what you see, then jump to its fix

    1. 1The Network tab shows no Cookie header on the request, and the console shows no CORS error.withCredentials is not set on the call that needs the cookieFix
    2. 2A CORS error shows up once withCredentials is true. The curl output has no access-control-allow-credentials line, or access-control-allow-origin is *.The server does not accept credentialed requestsFix
    3. 3In the Network tab an OPTIONS request is sent first and fails. It happens for POST with a JSON body or a custom header such as Authorization.The preflight answer lacks the credentials headerFix
    4. 4Every header above is correct, the login response has a Set-Cookie line, and the cookie never appears in the browser's storage, or it is stored and not sent back.The browser treats the cookie as a third-party cookieFix

What does the error look like?

  • The request has no Cookie header although withCredentials is true
  • The login response has a Set-Cookie header, but the browser stores nothing
  • A CORS error appears as soon as withCredentials is switched on

Cause 1 of 4

withCredentials is not set on the call that needs the cookie

1Check

The request config of axios defaults withCredentials to false. It decides whether cross-site requests carry credentials such as cookies, and it has no effect on same-site requests. Browsers do not send credentials on cross-origin calls unless asked to. Search for where the option is set.

grep -rn "withCredentials\|axios.create" src/

2Fix

Set it once on the axios instance the whole app uses, so no call forgets it.

import axios from 'axios';

export const api = axios.create({
  baseURL: 'http://localhost:3000',
  withCredentials: true,
});

3Verify

Repeat the call and open it in the Network tab: the Cookie header is now on the request.

curl -i -H "Origin: http://localhost:5173" http://localhost:3000/api/me

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The server does not accept credentialed requests

1Check

For a request with credentials the browser rejects any response that lacks Access-Control-Allow-Credentials: true. It also blocks the response when Access-Control-Allow-Origin is the * wildcard, and a Set-Cookie in such a response sets nothing. The cors middleware defaults to origin * and leaves the credentials header out unless you ask for it.

grep -rn "cors(" server/ src/ 2>/dev/null

2Fix

Name the exact origin of the front end, and turn credentials on.

const cors = require('cors');

app.use(cors({
  origin: 'http://localhost:5173',
  credentials: true,
}));

3Verify

curl now returns both headers, and the origin is the one you named, not *.

curl -i -H "Origin: http://localhost:5173" http://localhost:3000/api/me | grep -i access-control

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

The preflight answer lacks the credentials header

1Check

A preflight never carries credentials, and its response has to say Access-Control-Allow-Credentials: true as well, before the browser sends the real request with the cookie. Send a preflight yourself and read the headers.

curl -i -X OPTIONS http://localhost:3000/api/login \
  -H "Origin: http://localhost:5173" \
  -H "Access-Control-Request-Method: POST"

2Fix

Make the OPTIONS response carry the same credentialed CORS headers as the real response. The cors README says that app.use(cors()) handles preflight for all routes, while cors() added to a single route needs its own OPTIONS handler for that route.

const corsOptions = { origin: 'http://localhost:5173', credentials: true };

// for the whole app
app.use(cors(corsOptions));

// or, if you only enable cors per route, add the OPTIONS handler too
app.options('/api/login', cors(corsOptions));
app.post('/api/login', cors(corsOptions), loginHandler);

3Verify

The OPTIONS reply now includes access-control-allow-credentials: true.

curl -i -X OPTIONS http://localhost:3000/api/login \
  -H "Origin: http://localhost:5173" \
  -H "Access-Control-Request-Method: POST" | grep -i access-control

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The browser treats the cookie as a third-party cookie

1Check

Cookies set in CORS responses follow the browser's third-party cookie policy, and the SameSite attribute is what steers it. SameSite=None sends the cookie on cross-site requests, and it must come with Secure. Strict only sends it for requests from the same site that set it.

curl -i -X POST http://localhost:3000/api/login -H "Origin: http://localhost:5173" -H "Content-Type: application/json" -d '{}' | grep -i set-cookie

2Fix

If the front end and the API are on different sites, set the cookie with SameSite=None and Secure and serve both over https. Better, put them on the same site so the cookie is not third-party at all.

res.cookie('sid', token, {
  httpOnly: true,
  secure: true,
  sameSite: 'none',
});

3Verify

The Set-Cookie line shows SameSite=None and Secure, and the browser keeps the cookie and sends it on the next call.

curl -i -X POST http://localhost:3000/api/login -H "Origin: http://localhost:5173" -H "Content-Type: application/json" -d '{}' | grep -i set-cookie

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Create one axios instance with withCredentials: true and import it everywhere, instead of setting the option per call.
  • List the allowed front-end origins explicitly in the server config, one per environment, and never combine credentials with *.
  • Test a cookie login from the real front-end origin, not only from curl or Postman, because only the browser enforces these rules.

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

Web apps

Cookies are missing on a fetch request in a Next.js app

Access-Control-Allow-Credentials

In the browser, cookies on a cross-origin fetch need credentials include plus an API that answers with an exact origin and Access-Control-Allow-Credentials true, and a cookie the browser allows cross-site.

4 causes, each with a fix

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

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