Passport
Express authentication middleware with pluggable strategies, session integration, and per-route authentication guards.
Official Documentation ↗Passport 0.6/0.7 with Express 4; strategy packages have separate versions. Check the official docs for the latest API surface.Installation
npm install passport passport-local express-sessionpnpm add passport passport-local express-sessionyarn add passport passport-local express-sessionMethods
No methods match this filter.
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.
- Username & Password Sign In with Google Sign In with Facebook Email Magic Link Auth0 Integration How-to Guides Step-by-step guides to help you accomplish a task. (https://www.passportjs.org/docs/)
- Username & Password Reference Technical descriptions of how Passport works. (https://www.passportjs.org/docs/)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.