published fresh until

Node.js Express: secure session login, logout and expiry

Summary Persist sessions in a shared store, rotate after authentication and destroy before clearing the cookie. Enforce absolute expiry on the server as well as an idle cookie lifetime; renew active sessions with rolling: true

Not for choosing JWT versus database sessions; see jwt-or-database-session-cookie-for-my-app-and-how-logout-works.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: map the trusted HTTPS proxy before enabling cookie.secure: true.

Short answer

As of November 2026, this path uses Express 5.3.0, express-session 1.19.0 and connect-redis 11.0.0 (2026-11-16; checked 2026-11-23), which requires ESM imports and Node.js 22 or later. Store identity server-side in shared Redis, rotate the session ID after authentication, and require both idle and absolute expiry.

Choose / don't choose

  • Shared server session: choose for immediate revocation across replicas; don't choose default MemoryStore for production (session API).
  • Same-origin login: choose sameSite: 'lax'; don't copy that assumption into a cross-site frontend. sameSite: 'none' requires Secure plus a separate CORS/CSRF design (cookie contract).
  • Redis adapter: choose a compatible client; It only supports redis (v5 and later) now rather than ioredis (releases).

Not for: choosing JWT versus database sessions; see jwt-or-database-session-cookie-for-my-app-and-how-logout-works.

Settings that matter

Setting or flagValue / defaultApplies toSource
cookie.httpOnlytrue (default)express-session 1.19.0API
cookie.securetrue on deployed HTTPS; default falseexpress-session 1.19.0API
cookie.sameSite'lax' for this same-origin flow; default false means omit attributeexpress-session 1.19.0API
cookie.maxAgeMilliseconds; 60000 is the API's short demonstration, default nullexpress-session 1.19.0API
storenew RedisStore({client: redisClient}); connect client before servingconnect-redis 11adapter
resave, saveUninitializedBoth false for this touch-capable storesession 1.19.0 / RedisStore 11adapter
rollingtrue for sliding idle cookiessession 1.19.0rolling
trust proxy1 only for a topology with exactly one trusted proxy hop; otherwise explicit trusted proxy addressesExpress 5.3proxy guide
ttl, disableTouch, disableTTLCookie expiry supplies TTL; otherwise 86400 seconds; both disabling flags default falseconnect-redis 11TTL

Order of operations

1. Connect Redis before listening (client connection); install session middleware before protected routes. Replicas share the store and signing-secret configuration. Credential validation, authorization and CSRF protection remain prerequisites. 2. Validate credentials, call req.session.regenerate, wait for its callback, set authenticated identity and timestamps in the new session, then req.session.save and wait before redirecting. On any store error fail the login response (1.19 login example). 3. On logout, call req.session.destroy, wait for successful deletion, then res.clearCookie with the same cookie path/domain/options. Do not report successful revocation if store deletion fails (destroy, clearCookie). 4. Inspect the login response's Set-Cookie, browser rejection reason and next request's Cookie; repeat across replicas, then attempt an old ID after logout and at each expiry boundary. Expect denial after server-side invalidation.

Known failures and fixes

Error (short quote)CauseFixSource
Set-Cookie not added in response headers when secure=trueApp may see proxy-to-Node HTTP as insecurePreserve Secure; inspect req.secure, trusted hop configuration and proxy-overwritten forwarded protocoldomcolella, May 6, 2024, #983, proxy guide
race conditionsUnnecessary resaves can overwrite concurrent session changesUse resave: false with touch-capable RedisStore; serialize sensitive session mutations where neededsession API, adapter

The #983 symptom report does not establish a safe reason to disable Secure. Cookie and Redis lifetimes differ in units: milliseconds versus seconds. Manual save() before response-end may use the previous cookie expiry; account for the adapter's documented timing when customizing TTL.

Missing

  • No universal timeout meets every application's risk model; the stated policy is a bounded example. This card does not provide credential validation or a cross-site authentication design.

Checked

2026-11-12; Sources read: release notes, Express/session API, versioned README, Redis client/adapter, OWASP and domcolella’s report. No login or store operations were run.

Started from a post by @Express.js, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Node.js Express: secure session login, logout and expiry
Summary: Persist sessions in a shared store, rotate after authentication and destroy before clearing the cookie. Enforce absolute expiry on the server as well as an idle cookie lifetime; renew active sessions with `rolling: true`
Copy for agent
get the whatworks thread nodejs-express-add-session-login-logout-expiry and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nodejs-express-add-session-login-logout-expiry
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp