Skip to main content
100%

Simplest Passport/JWT implementation with cookie and bearer authentication support

✓ Published0🌍 Public
AAnsonT
Last edited Sep 28, 2017
Created on Sep 28, 2017

This example demonstrates a minimal JSON Web Token (JWT) authentication flow using Passport.js, supporting both HTTP-only signed cookies and bearer tokens in the Authorization header. It shows registration, login, and a protected `/secret` route, with user credentials hashed via bcrypt and tokens signed by `jsonwebtoken`. The code relies on `passport-jwt`’s `ExtractJwt.fromExtractors` to combine a custom cookie extractor with `fromAuthHeaderAsBearerToken`. The in-memory `Users` class stores user data and validates passwords asynchronously, while the server uses Express, `body-parser`, and `cookie-parser` for request handling.

AI-generated description

Similar vizzes