req.cookies is undefined or empty in a Firebase Cloud Function behind Hosting
req.cookies is undefinedShort answer
Two separate rules cause this. Behind Firebase Hosting, cookies are stripped from the request except one cookie named __session. And in Express, req.cookies exists only when cookie-parser is in use. Rename the cookie to __session, add cookie-parser, and read the value from req.cookies.__session.
Fix it fast
1Find out which cause you have
Log what the function really receives: the raw Cookie header and the parsed object. If the header is empty, the cookie was removed before it reached you. If the header has it but the object does not, the parser is missing.
exports.debugCookies = onRequest((req, res) => { res.json({ header: req.headers.cookie ?? null, parsed: req.cookies ?? null }); });2Match what you see, then jump to its fix
- 1
The browser shows the cookie being sent, but the function sees an empty Cookie headerThe cookie is not named __sessionFix - 2
The raw header contains the cookie, but req.cookies is undefinedcookie-parser is not installed or not applied before the routeFix - 3
req.cookies is {} while a signed cookie was set with cookie-parserThe cookie is signed, so it is in req.signedCookiesFix
- 1
What does the error look like?
req.cookies is undefinedreq.cookies is {} although the browser sends the cookie
Cause 1 of 3
The cookie is not named __session
1Check
Hosting strips cookies from requests before they reach your app. Only the cookie named __session passes through to the execution of your app.
curl -s -H 'Cookie: other=1; __session=abc' https://example.web.app/debugCookies2Fix
Set the cookie under the name __session, and read that name in the function. Use it only when you really serve content that depends on the user, because it becomes part of the cache key.
// setting it
res.cookie('__session', token);
// reading it
const token = req.cookies.__session;3Verify
The debug function shows __session in the header.
curl -s -H 'Cookie: __session=abc' https://example.web.app/debugCookiesConfirmed by firebase.google.com
Cause 2 of 3
cookie-parser is not installed or not applied before the route
1Check
In Express, req.cookies is an object of the request's cookies only when the cookie-parser middleware is used. Without it the property is not filled.
npm ls cookie-parser2Fix
Install cookie-parser and register it before any route that reads cookies.
// npm install cookie-parser
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser());3Verify
The debug route now returns the cookie in the parsed field.
curl -s -H 'Cookie: __session=abc' https://example.web.app/debugCookiesConfirmed by expressjs.com
Cause 3 of 3
The cookie is signed, so it is in req.signedCookies
1Check
A signed cookie is not placed in req.cookies. It sits in req.signedCookies, unsigned and ready for use, and cookie-parser needs the secret.
// log both objects: console.log(req.cookies, req.signedCookies)2Fix
Pass the secret to cookie-parser and read req.signedCookies.
app.use(cookieParser(process.env.COOKIE_SECRET));
const token = req.signedCookies.__session;3Verify
req.signedCookies.__session returns the original value.
// call the route with a cookie signed by the same secretConfirmed by expressjs.com
How do I stop it coming back?
- Treat __session as the single cookie your function can rely on behind Hosting, and keep everything else out of cookies.
- Keep cookie-parser in the same app setup file as your other middleware.
- Add a tiny debug route in staging that prints req.headers.cookie.
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:
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Back end
Express sends Set-Cookie but the browser never stores the cookie
req.cookies is empty on the next request
res.cookie only writes a Set-Cookie header.
4 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 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
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix