passport
Authentication middleware for Node.js — 500+ strategies for OAuth, OpenID, local username/password, JWT, and more.
passport
ExpressAuthentication middleware for Node.js — 500+ strategies for OAuth, OpenID, local username/password, JWT, and more.
Fit
Bundle (gzip)
2.9 kB
9.4 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Implementing authentication for every provider (Google, GitHub, Facebook, local) from scratch means handling OAuth flows, token exchanges, session management, and user serialization repeatedly for each strategy.
What It Does
Passport uses a strategy pattern — you install a strategy package (e.g. passport-google-oauth20) and configure it once. Passport handles the auth flow, calls your verify callback with the authenticated user profile, and integrates with Express sessions or JWT-based stateless auth.
Installation
npm install passport passport-local passport-jwtUsage Example
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
passport.use(new LocalStrategy(async (username, password, done) => {
const user = await User.findOne({ username });
if (!user || !await user.verifyPassword(password)) return done(null, false);
return done(null, user);
}));
passport.use(new JwtStrategy(
{ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: process.env.JWT_SECRET },
async (payload, done) => {
const user = await User.findById(payload.sub);
return user ? done(null, user) : done(null, false);
}
));
app.post('/login', passport.authenticate('local'), (req, res) => res.json({ token: signJwt(req.user) }));
app.get('/profile', passport.authenticate('jwt', { session: false }), (req, res) => res.json(req.user));Related packages
Node.js body parsing middleware — populate `req.body` from JSON, urlencoded, text, and raw payloads.
compressionExpressHTTP response compression middleware for Express — gzip/deflate responses to reduce bandwidth and improve load times.
cookie-parserExpressCookie parsing middleware for Express — parse Cookie headers into `req.cookies` and support signed cookies for tamper detection.
corsExpressCORS middleware for Express — configure cross-origin resource sharing with a single line of code.
express-async-errorsExpressSimplify Express error handling by automatically passing thrown errors to `next()`.