Passport
Overview
Passport is Express-compatible authentication middleware for Node.js, designed for the singular purpose of authenticating requests. It works through an extensible plugin system of "strategies" (local username/password, OAuth, OpenID, etc.) and does not mount routes or impose a database schema, leaving you in control. Read this when adding authentication to an Express or Node.js app.
Official sources
Install / setup
npm install passport
(Verbatim from the official repo README. Each authentication method needs its own strategy package, e.g. npm install passport-local.)
Core concepts
- Middleware: Passport is authentication middleware you plug into the Express request pipeline; it authenticates the incoming request.
- Strategies: pluggable mechanisms (e.g.
passport-local, OAuth strategies) implement a specific way to authenticate; install one per method.
passport.authenticate(): the middleware that runs a named strategy on a route to verify credentials.
- Sessions: Passport can establish a persistent login session, storing a minimal user reference in the session.
serializeUser / deserializeUser: control what user data is stored in the session and how it is restored on later requests.
req.user: once authenticated, Passport populates the authenticated user on the request object.
Best practices
- Install and configure one strategy per authentication method (e.g.
passport-local for username/password); the core package alone authenticates nothing.
- Keep
serializeUser payloads minimal (typically just an ID) and reload the full user in deserializeUser.
- Place
passport.initialize() (and passport.session() when using sessions) in the correct middleware order before protected routes.
- Pair session-based logins with a secure session store and configured cookies; Passport does not manage session storage for you.
Common pitfalls
- Expecting login to work with only
passport installed → you must also install and register a strategy package.
- Wrong middleware ordering →
passport.session() before the session middleware, or routes before passport.initialize(), leaves req.user undefined.
- Assuming Passport provides routes or a schema → it intentionally does not; you define routes, user storage, and verification logic yourself.
Examples
const passport = require("passport")
const LocalStrategy = require("passport-local")
passport.use(new LocalStrategy(async (username, password, done) => {
const user = await findUser(username)
if (!user || !(await verify(user, password))) return done(null, false)
return done(null, user)
}))
passport.serializeUser((user, done) => done(null, user.id))
passport.deserializeUser(async (id, done) => done(null, await getUser(id)))
app.post("/login", passport.authenticate("local", { successRedirect: "/", failureRedirect: "/login" }))
Further reading
Related skills
- ../authjs — higher-level, framework-agnostic JS/TS auth library
- ../auth0 — hosted IdP you can integrate via a Passport strategy
1---2name: passport3description: Passport — Express-compatible authentication middleware for Node.js using pluggable strategies; consult when adding login to an Express/Node app.4domain: stack5category: auth6tags: [auth, nodejs, express, middleware, strategies, sessions, oauth]7official_sources:8 - https://www.passportjs.org/docs/9 - https://github.com/jaredhanson/passport10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/passport13---1415# Passport1617## Overview18Passport is Express-compatible authentication middleware for Node.js, designed for the singular purpose of authenticating requests. It works through an extensible plugin system of "strategies" (local username/password, OAuth, OpenID, etc.) and does not mount routes or impose a database schema, leaving you in control. Read this when adding authentication to an Express or Node.js app.1920## Official sources21- Docs: https://www.passportjs.org/docs/22- Repo: https://github.com/jaredhanson/passport2324## Install / setup25```bash26npm install passport27```28(Verbatim from the official repo README. Each authentication method needs its own strategy package, e.g. `npm install passport-local`.)2930## Core concepts31- Middleware: Passport is authentication middleware you plug into the Express request pipeline; it authenticates the incoming request.32- Strategies: pluggable mechanisms (e.g. `passport-local`, OAuth strategies) implement a specific way to authenticate; install one per method.33- `passport.authenticate()`: the middleware that runs a named strategy on a route to verify credentials.34- Sessions: Passport can establish a persistent login session, storing a minimal user reference in the session.35- `serializeUser` / `deserializeUser`: control what user data is stored in the session and how it is restored on later requests.36- `req.user`: once authenticated, Passport populates the authenticated user on the request object.3738## Best practices39- Install and configure one strategy per authentication method (e.g. `passport-local` for username/password); the core package alone authenticates nothing.40- Keep `serializeUser` payloads minimal (typically just an ID) and reload the full user in `deserializeUser`.41- Place `passport.initialize()` (and `passport.session()` when using sessions) in the correct middleware order before protected routes.42- Pair session-based logins with a secure session store and configured cookies; Passport does not manage session storage for you.4344## Common pitfalls45- Expecting login to work with only `passport` installed → you must also install and register a strategy package.46- Wrong middleware ordering → `passport.session()` before the session middleware, or routes before `passport.initialize()`, leaves `req.user` undefined.47- Assuming Passport provides routes or a schema → it intentionally does not; you define routes, user storage, and verification logic yourself.4849## Examples50```js51const passport = require("passport")52const LocalStrategy = require("passport-local")5354passport.use(new LocalStrategy(async (username, password, done) => {55 const user = await findUser(username)56 if (!user || !(await verify(user, password))) return done(null, false)57 return done(null, user)58}))5960passport.serializeUser((user, done) => done(null, user.id))61passport.deserializeUser(async (id, done) => done(null, await getUser(id)))6263app.post("/login", passport.authenticate("local", { successRedirect: "/", failureRedirect: "/login" }))64```6566## Further reading67- https://www.passportjs.org/concepts/authentication/strategies/ — strategies68- https://www.passportjs.org/packages/ — strategy package catalog6970## Related skills71- ../authjs — higher-level, framework-agnostic JS/TS auth library72- ../auth0 — hosted IdP you can integrate via a Passport strategy