🔒
Auth2M+/wkMIT

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
npm install passport
yarn
yarn add passport
pnpm
pnpm add passport

Import

ESM
import passport from 'passport';

Quick Example

usage
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

Packagepassport
CategoryAuth
Weekly Downloads2M+
LicenseMIT
Installnpm install passport

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.