Passport logs the user in but req.isAuthenticated() returns false on the next request
req.isAuthenticated() returns false right after loginShort 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.
Fix it fast
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(); });2Match what you see, then jump to its fix
- 1
req.session is undefined, or req.user is never set although login succeededpassport.session() is missing or sits before express-sessionFix - 2
The session cookie arrives but req.user stays unsetserializeUser or deserializeUser is missing or returns nothingFix - 3
No 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
Login works from Postman or the same origin, but not from a React app on another originA cross-origin frontend does not send credentialsFix
- 1
What does the error look like?
req.isAuthenticated() returns false right after loginreq.user is undefined on the next requestLogin 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.js2Fix
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/loginConfirmed by passportjs.org
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.js2Fix
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/meConfirmed by passportjs.org
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-cookie2Fix
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/loginConfirmed by expressjs.com
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-control2Fix
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-controlConfirmed by developer.mozilla.org
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
Back 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 fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix