Maaz Zindani

Loading the index…

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

req.cookies is undefined or empty in a Firebase Cloud Function behind Hosting

Errorreq.cookies is undefined

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

  • Applies to firebase-tools 14, firebase-functions 6, Node.js 22, Express 4, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The browser shows the cookie being sent, but the function sees an empty Cookie headerThe cookie is not named __sessionFix
    2. 2The raw header contains the cookie, but req.cookies is undefinedcookie-parser is not installed or not applied before the routeFix
    3. 3req.cookies is {} while a signed cookie was set with cookie-parserThe cookie is signed, so it is in req.signedCookiesFix

What does the error look like?

  • req.cookies is undefined
  • req.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/debugCookies

2Fix

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

Confirmed by firebase.google.com

Did this fix it?

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

2Fix

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

Confirmed by expressjs.com

Did this fix it?

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 secret

Confirmed by expressjs.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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