API Reference

Passport

Express authentication middleware with pluggable strategies, session integration, and per-route authentication guards.

Official Documentation ↗
CompatibilityThis documentation covers version Passport 0.6/0.7 with Express 4; strategy packages have separate versions. Check the official docs for the latest API surface.

Installation

npmnpm install passport passport-local express-session
pnpmpnpm add passport passport-local express-session
yarnyarn add passport passport-local express-session

Methods

Passport is authentication middleware for Express. A strategy verifies credentials, and Passport attaches the authenticated user to the request and optionally the session. It does not provide authorization or password hashing.

Strategy and password verification

Use a strategy such as passport-local for username/password, or an OAuth strategy for delegated login. Verify passwords with Argon2 or bcrypt and return the same generic error for unknown users and wrong passwords. Never store plaintext or reversible passwords.

Sessions and middleware order

For session-based login, configure express-session, passport.initialize(), and passport.session() in that order. Serialize only a stable user id and load the user on each request. Middleware order mistakes produce a req.user that is undefined or a session that never persists.

Authorization is separate

A successful login proves identity, not permission. Add role and ownership checks to sensitive routes and server actions. Rotate sessions on privilege changes and destroy the session on logout.

Security notes

Quoted from the official documentation (https://www.passportjs.org/docs/); this is not a complete threat model.

FAQ

Does Passport store passwords?
No. It delegates verification to the strategy. Use Argon2 or bcrypt to hash and verify passwords; never store plaintext.
Why is req.user undefined after login?
The session middleware or passport.session() is missing, or serializeUser/deserializeUser is not configured. Check the middleware order.
Is passport.authenticate enough authorization?
No. It authenticates the user. Role and resource ownership checks must still run in the route or service.