Maaz Zindani

Loading the index…

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

Express sends Set-Cookie but the browser never stores the cookie

ErrorSet-Cookie header present in the response but the cookie is missing in DevTools Application tab

Short answer

res.cookie only writes a Set-Cookie header. The browser decides whether to keep it. It drops the cookie when Secure is set but the page is plain HTTP, when a cross-origin fetch has no credentials, when SameSite=None lacks Secure, or when Domain does not fit the host. Check the response headers first.

  • Applies to Express 5, express-session 1.x, Node.js 22, Chrome and Firefox current
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the raw response. If Set-Cookie is not there, your code never ran or headers were not applied. If it is there, a browser rule rejected it, so work through the causes.

    curl -i http://localhost:3000/login
  2. 2Match what you see, then jump to its fix

    1. 1Works on localhost, fails on the deployed site that sits behind nginx or a load balancersecure: true on plain HTTP, or behind a TLS proxy that Express does not trustFix
    2. 2The front end runs on another origin, for example localhost:5173 calling localhost:3000A cross-origin fetch does not send or accept credentialsFix
    3. 3The cookie is needed for a cross-site request (an embedded or separately hosted front end)SameSite=None without SecureFix
    4. 4The cookie is rejected, or stored but not sent on other routesDomain or path does not fit the host that sets the cookieFix

What does the error look like?

  • Set-Cookie header present in the response but the cookie is missing in DevTools Application tab
  • req.cookies is empty on the next request

Cause 1 of 4

secure: true on plain HTTP, or behind a TLS proxy that Express does not trust

1Check

Secure cookies are sent only over HTTPS, and insecure http: pages cannot set them (localhost is an exception). Behind a proxy that ends TLS, Express sees plain HTTP unless trust proxy is set, so express-session refuses to set a secure cookie.

curl -i https://example.com/login | grep -i set-cookie

2Fix

Tell Express to trust the proxy so it reads X-Forwarded-Proto, and make sure the proxy sends that header.

app.set('trust proxy', 1);

3Verify

The Set-Cookie header now appears on the HTTPS response and the cookie is stored.

curl -i https://example.com/login | grep -i set-cookie

Confirmed by github.com

Did this fix it?

Cause 2 of 4

A cross-origin fetch does not send or accept credentials

1Check

For CORS requests, browsers ignore Set-Cookie unless the request includes credentials. The server must answer with Access-Control-Allow-Credentials true and an explicit origin, not the * wildcard.

curl -i -H 'Origin: http://localhost:5173' http://localhost:3000/login | grep -i access-control

2Fix

Set credentials include on the client, and allow that exact origin with credentials on the server.

// client
fetch('http://localhost:3000/login', { method: 'POST', credentials: 'include' });
// server (cors package)
app.use(cors({ origin: 'http://localhost:5173', credentials: true }));

3Verify

The cookie appears in DevTools Application tab after the call.

curl -i -H 'Origin: http://localhost:5173' http://localhost:3000/login | grep -i access-control-allow

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

SameSite=None without Secure

1Check

SameSite=None sends the cookie on cross-site requests, and the Secure attribute must also be set with it. With Lax, cross-site fetch requests do not carry the cookie.

curl -i http://localhost:3000/login | grep -i set-cookie

2Fix

Set both attributes, and serve both sites over HTTPS.

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

3Verify

Set-Cookie shows SameSite=None; Secure.

curl -i https://api.example.com/login | grep -i set-cookie

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 4

Domain or path does not fit the host that sets the cookie

1Check

Domain must be the setting host or a parent domain, never a public suffix, and a leading dot is ignored. Path decides which URLs receive the cookie; res.cookie defaults to '/'.

curl -i https://api.example.com/login | grep -i set-cookie

2Fix

Remove Domain to get a host-only cookie, or set a parent domain you control.

res.cookie('sid', token, { domain: 'example.com', path: '/', secure: true });

3Verify

The cookie shows in DevTools under the expected domain.

curl -i https://api.example.com/login | grep -i set-cookie

Confirmed by expressjs.com

Did this fix it?

How do I stop it coming back?

  • Test cookies on the real HTTPS domain, not only on localhost.
  • Keep one cookie options object so secure, sameSite, domain and path stay the same on set and clear.
  • Set trust proxy once, with the exact number of proxy hops.

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

Next step

Want this handled for good?

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