passport-jwt
A Passport.js strategy for authenticating requests using JSON Web Tokens
Repository Health
Technical Analysis
passport-jwt is a strategy module for Passport, the widely-used Node.js authentication middleware, that lets applications authenticate HTTP requests using JSON Web Tokens instead of cookies or sessions. It parses a JWT out of an incoming request via a pluggable extractor function, verifies its signature and claims (issuer, audience, algorithms, expiration) using the jsonwebtoken library, and hands the decoded payload to a user-supplied verify callback that resolves the associated user record.
Because it plugs directly into Passport’s strategy interface, teams already using Passport for other authentication schemes (local, OAuth, etc.) can add stateless, token-based auth to specific routes with minimal additional code, making it a common choice for securing RESTful APIs consumed by single-page apps and mobile clients.
What You Get
- A JwtStrategy class that plugs into any existing Passport.js authentication setup
- A library of built-in token extractors (header, bearer auth header, body field, URL query parameter, or a custom composed extractor chain)
- Signature and claim verification (issuer, audience, algorithms, expiration) delegated to the battle-tested jsonwebtoken library
- Support for both static secrets/keys and dynamic secretOrKeyProvider callbacks for per-request key resolution
- An option to pass the full request into the verify callback for request-aware user lookups
Common Use Cases
- Securing RESTful API endpoints consumed by single-page applications or mobile clients without maintaining server-side sessions
- Adding stateless bearer-token authentication alongside existing Passport strategies like passport-local or OAuth providers
- Verifying tokens issued by a separate auth service or identity provider before allowing access to protected routes
- Supporting key rotation scenarios via secretOrKeyProvider, where the verification key depends on the token or request
Under The Hood
Architecture: passport-jwt is a thin adapter layer over two dependencies: passport-strategy (for the Strategy base class and pass/fail/error request lifecycle) and jsonwebtoken (for actual signature/claims verification). lib/strategy.js defines JwtStrategy, whose authenticate() method pulls a raw token string from the request via a caller-supplied jwtFromRequest function, resolves a secret/key via secretOrKeyProvider (a normalized wrapper around the simpler secretOrKey option), and delegates cryptographic verification to JwtStrategy.JwtVerifier (lib/verify_jwt.js, itself a wrapper around jsonwebtoken.verify). On success the decoded payload is handed to the user’s verify callback, whose done(err, user, info) result drives Passport’s success/fail/error branching. Tech Stack: Plain CommonJS JavaScript with no build step; runtime dependencies are just jsonwebtoken and passport-strategy, and dev dependencies (mocha, chai, chai-passport-strategy, sinon, nyc) support a classic Mocha/Chai unit-test suite with coverage reporting. Code Quality: The test/ directory has one spec file per concern (initialization, request extraction, validation, verify-callback behavior) with mocked requests (mock_request.js) and fixture data (testdata.js), giving reasonably thorough coverage of the strategy’s branching logic; the production code itself is small (a few hundred lines across strategy.js, extract_jwt.js, auth_header.js, verify_jwt.js) and uses util.inherits-style prototypal inheritance rather than ES6 classes, reflecting its 2014-era origins. API Design: The public surface is deliberately minimal — a Strategy constructor plus an ExtractJwt namespace of extractor factories — and the extractor-function convention (any (req) => token|null function is valid) makes it trivial to compose custom token sources (e.g. cookies) without modifying the library, keeping the integration surface small for consumers already familiar with Passport’s strategy pattern.
Used by 19 apps in this directory
Kutt
Analytics · Marketing
Self-hosted URL shortener with custom domains, per-link analytics, and zero build step required.
Laudspeaker
Automation · Marketing
Open-source customer engagement platform for building visual, event-triggered messaging journeys across email, SMS, push, in-app, and webhooks.
LibreChat
AI Assistants · Developer Tools
Unite every major AI model in one self-hosted chat platform with agents, code execution, MCP tools, and enterprise authentication.
NocoDB
Databases · Low Code Platforms · No Code Platforms
Turn any SQL database into a collaborative no-code spreadsheet with automatic REST APIs and real-time views.
Novu
Developer Tools
Open-source communication infrastructure that connects your products and AI agents to every channel your users live on — Inbox, Email, SMS, Push, Chat, and more.
Swetrix
Analytics
Privacy-first, cookieless web analytics with error tracking, session replays, and performance monitoring — self-host or use Cloud.
Teable
Databases · No Code Platforms
A no-code PostgreSQL database with spreadsheet UX, real-time collaboration, and native AI agents — built for teams that outgrow Airtable.
ToolJet
AI Agents · Low Code Platforms · No Code Platforms
Open-source AI-native platform to build and deploy internal tools, workflows, and AI agents with a visual drag-and-drop builder and 80+ data source integrations.
twenty
CRM
The open-source CRM you build, ship, and version like the rest of your stack — with customizable objects, AI agents, and a TypeScript SDK.