Axios sends no cookie to an Express API even with withCredentials set to true
The request has no Cookie header although withCredentials is trueShort 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.
Fix it fast
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-credentials2Match what you see, then jump to its fix
- 1
The 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
A 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
In 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
Every 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
- 1
What does the error look like?
The request has no Cookie header although withCredentials is trueThe login response has a Set-Cookie header, but the browser stores nothingA 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/meConfirmed by raw.githubusercontent.com
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/null2Fix
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-controlConfirmed by developer.mozilla.org
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-controlConfirmed by raw.githubusercontent.com
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-cookie2Fix
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-cookieConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
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 fixWeb 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 fixWeb apps
Socket.IO client fails to connect with xhr poll error
connect_error: xhr poll error
xhr poll error is only the reason string on the client's connect_error event: the first HTTP request to the server failed.
4 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fix