passport
Passport is the most popular authentication middleware for Node.js, providing a modular framework for implementing authentication strategies in Express and Conn…
Installation
npm install passport
yarn add passport
pnpm add passport
Import
import passport from 'passport';
Quick Example
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
passport.use(new LocalStrategy(
async (username, password, done) => {
const user = await findUser(username);
if (!user || !await verify(password, user.hash)) {
return done(null, false, { message: 'Invalid credentials' });
}
return done(null, user);
}
));About passport
Passport is the most popular authentication middleware for Node.js, providing a modular framework for implementing authentication strategies in Express and Connect applications. Passport separates authentication concerns into strategies — self-contained modules that handle specific authentication mechanisms. Over 500 strategies are available including passport-local for username/password, passport-jwt for JSON Web Tokens, passport-google-oauth20 for Google OAuth, passport-github2 for GitHub, and strategies for Facebook, Twitter, SAML, LDAP, and OpenID Connect. Each strategy handles the specifics of verifying credentials while Passport manages the session serialization, request augmentation (populating req.user), and middleware integration. Passport's middleware approach means adding authentication to a route is as simple as adding passport.authenticate('strategy-name') as route middleware. The library supports multiple simultaneous strategies, custom callback handling for fine-grained control over the authentication response, and flash messages for communicating authentication failures. Passport integrates with Express sessions for stateful authentication and supports stateless JWT-based authentication for APIs. While newer authentication solutions like NextAuth.js provide more integrated experiences for specific frameworks, Passport remains the most flexible and widely-deployed authentication solution for Node.js.
Quick Facts
| Package | passport |
| Category | Auth |
| Weekly Downloads | 2M+ |
| License | MIT |
| Install | npm install passport |
Related Packages
Express is the most widely used web application framework for Node.js, providing a minimal and flexi…
jsonwebtoken is the most widely used library for creating and verifying JSON Web Tokens (JWTs) in No…
bcrypt is a library for hashing passwords using the bcrypt algorithm, which is specifically designed…
express-session is a session middleware for Express that creates server-side sessions identified by …
jose is a universal, standards-compliant JavaScript library implementing JSON Object Signing and Enc…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.