express-session
Session middleware for Express with signed cookies, server-side session stores, rolling expiry, and logout invalidation.
Official Documentation ↗express-session 1.18+; store packages and cookie prefixes vary by deployment. Check the official docs for the latest API surface.Installation
npm install express-sessionpnpm add express-sessionyarn add express-sessionMethods
No methods match this filter.
express-session stores session data server-side and gives the browser a signed session id cookie. It is a stateful authentication primitive: logout can revoke the session immediately, unlike a stateless JWT. The default memory store must be replaced for production.
Cookie and store
Use HttpOnly, Secure, and a deliberate SameSite value. A __Host- prefix requires Secure, Path=/, and no Domain. Configure Redis or another store with its own connection budget and availability plan. The session secret must be long, random, and rotated with a plan for existing cookies.
Session lifecycle
Keep session data small and non-sensitive. Regenerate the session id on login to prevent fixation, destroy it on logout, and invalidate other sessions on password change. Set an idle and absolute timeout; a session that lives forever is a stolen-credential risk.
Errors
The following points are quoted verbatim from the official documentation (https://expressjs.com/en/resources/middleware/session.html). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Warning The default server-side session storage, MemoryStore , is purposely not designed for a production environment. (https://expressjs.com/en/resources/middleware/session.html)
- The callback should be called as callback(error, sessions) . (https://expressjs.com/en/resources/middleware/session.html)
- The callback should be called as callback(error) once the session is destroyed. (https://expressjs.com/en/resources/middleware/session.html)
Security notes
Quoted from the official documentation (https://expressjs.com/en/resources/middleware/session.html); this is not a complete threat model.
- Using cookie-parser may result in issues if the secret is not the same between this module and cookie-parser . (https://expressjs.com/en/resources/middleware/session.html)
- ‘none’ will set the SameSite attribute to None for an explicit cross-site cookie. (https://expressjs.com/en/resources/middleware/session.html)
- The cookie.sameSite option can also be set to the special value ‘auto’ to have this setting automatically match the determined security of the connection. (https://expressjs.com/en/resources/middleware/session.html)
FAQ
- Is the default MemoryStore safe for production?
- No. It leaks memory, does not survive restarts, and is not shared across instances. Use Redis or another supported store.
- Why does the session cookie not persist behind HTTPS?
- Check the cookie secure, sameSite, path, and domain settings, and ensure the proxy forwards the protocol correctly. A __Host- cookie requires Secure, Path=/, and no Domain.
- How do I invalidate a session on password change?
- Destroy the current session and invalidate all other sessions for that user in the session store, or rotate a per-user session version and reject older versions.
Report a problem with this API docOpens a pre-filled issue on GitHub.