Express middleware for validating JWTs (JSON Web Tokens) via the jsonwebtoken library. As of v8.5.1, it supports async secret retrieval, token revocation checks, and a customizable request property (default `req.auth`). It is fully typed (TypeScript) and ESM/CJS compatible. Key differentiators: built-in `.unless()` for path exclusion, optional `credentialsRequired` for public endpoints, and all jsonwebtoken verify options (audience, issuer, clockTolerance, etc.). However, v7→v8 introduced several breaking changes: the exported function is now `expressjwt` (not `jwt`), the request property changed from `req.user` to `req.auth`, and `algorithms` is now required to prevent downgrade attacks. The package is maintained by Auth0 with quarterly releases.
npm install express-jwtVerified import paths — ran on the pinned version, not inferred.
Shows basic usage: importing expressjwt, setting up a protected route with secret and algorithms, and accessing the decoded payload from req.auth.
Replace `import jwt from 'express-jwt'` with `import { expressjwt } from 'express-jwt'`.Access decoded payload via `req.auth` instead of `req.user`.
Always pass `algorithms: ['HS256']` (or your chosen algorithm) in options.
Use only one set of algorithms (symmetric or asymmetric) and validate against the expected algorithms list.
If you want optional auth, consider using `.unless()` or handle missing tokens in your own middleware.
Change import to `import { expressjwt as jwt } from 'express-jwt'`Ensure secret is a non-empty string or Buffer, e.g., `secret: process.env.JWT_SECRET ?? 'fallback'`
Add `algorithms: ['HS256']` (or your desired algorithm) to the options object.