Maaz Zindani

Loading the index…

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

Passport logs the user in but req.isAuthenticated() returns false on the next request

Errorreq.isAuthenticated() returns false right after login

Short answer

req.isAuthenticated() only checks that req.user is set. On later requests req.user is filled by passport.session(), which needs express-session before it, a serializeUser and deserializeUser pair that returns a user, and a session cookie the browser really sends back. One of those four is missing.

  • Applies to Passport 0.7, express-session 1.18, Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log what Passport sees on a protected request. This shows whether the cookie arrives, whether a session exists and whether req.user is set.

    app.use((req, res, next) => {
      console.log(req.headers.cookie, !!req.session, req.user);
      next();
    });
  2. 2Match what you see, then jump to its fix

    1. 1req.session is undefined, or req.user is never set although login succeededpassport.session() is missing or sits before express-sessionFix
    2. 2The session cookie arrives but req.user stays unsetserializeUser or deserializeUser is missing or returns nothingFix
    3. 3No session cookie appears in the browser, or a new session starts every requestThe cookie is marked secure but the request is plain HTTP or behind a proxyFix
    4. 4Login works from Postman or the same origin, but not from a React app on another originA cross-origin frontend does not send credentialsFix

What does the error look like?

  • req.isAuthenticated() returns false right after login
  • req.user is undefined on the next request
  • Login redirect works, but protected routes redirect back to /login

Cause 1 of 4

passport.session() is missing or sits before express-session

1Check

Passport's session support depends on express-session, so express-session must be added first and passport.session() after it. passport.session() is an alias for passport.authenticate('session').

grep -n "session(\|passport.session\|passport.initialize" app.js

2Fix

Mount express-session first, then Passport's session middleware, and only then the routes.

app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false }));
app.use(passport.session());
app.use('/', routes);

3Verify

Log in, then request a protected route in the same browser.

curl -i -c jar.txt -b jar.txt -d "username=a&password=b" http://localhost:3000/login

Confirmed by passportjs.org

Did this fix it?

Cause 2 of 4

serializeUser or deserializeUser is missing or returns nothing

1Check

serializeUser decides what is stored in the session at login. deserializeUser returns the user on later requests, and Passport assigns that to req.user. isAuthenticated reads req.user (it returns true only if that value is truthy), so a deserializeUser that finds no user gives false.

grep -n "serializeUser\|deserializeUser" app.js

2Fix

Store a stable id and look the user up again. Return the user, or an error, never undefined.

passport.serializeUser((user, cb) => cb(null, user.id));
passport.deserializeUser(async (id, cb) => {
  try { cb(null, await User.findById(id)); } catch (e) { cb(e); }
});

3Verify

Log in and print req.user in a protected route. It should be the user document.

curl -s -b jar.txt http://localhost:3000/me

Confirmed by passportjs.org

Did this fix it?

Cause 3 of 4

The cookie is marked secure but the request is plain HTTP or behind a proxy

1Check

With cookie.secure set, express-session does not set the cookie when you reach the site over HTTP. Behind a proxy that ends TLS, Express must trust the proxy, or the cookie is not set.

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

2Fix

Use HTTPS in production and set trust proxy when a proxy terminates TLS. Use secure: false only on local HTTP development.

app.set('trust proxy', 1);
app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false,
  cookie: { secure: true },
}));

3Verify

Log in over HTTPS and check that a Set-Cookie header comes back and is sent on the next request.

curl -i -c jar.txt https://example.com/login

Confirmed by expressjs.com

Did this fix it?

Cause 4 of 4

A cross-origin frontend does not send credentials

1Check

Cross-origin fetch and XMLHttpRequest calls do not send cookies by default, and a response with a wildcard origin has its Set-Cookie ignored.

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

2Fix

Send credentials from the client, and answer with Access-Control-Allow-Credentials: true and an explicit origin, not *.

// client
fetch('http://localhost:3000/me', { credentials: 'include' });
// server: allow that exact origin and credentials
res.set('Access-Control-Allow-Origin', 'http://localhost:5173');
res.set('Access-Control-Allow-Credentials', 'true');

3Verify

Log in from the frontend and reload. The user should stay signed in.

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

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Keep the order: express-session, passport.session(), then routes.
  • Store only an id in the session and test deserializeUser with a missing user.
  • Use a persistent session store in production; the default MemoryStore is not meant for it.

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.