🟢 Node.js Intermediate

What is Passport.js?

Answer

Passport.js is the most widely used authentication middleware for Node.js and Express. It has a modular strategy-based architecture — you install specific strategy packages for each authentication method: passport-local (username/password), passport-jwt (JSON Web Tokens), passport-google-oauth20 (Google OAuth), passport-github, etc. There are 500+ strategies available. Passport normalizes the authentication process: regardless of strategy, successful authentication calls done(null, user) which attaches the user to req.user. Integration with Express: app.use(passport.initialize()); (required) and app.use(passport.session()); (for session-based auth). Protect routes with passport.authenticate("jwt", { session: false }) as middleware. For OAuth, Passport handles the redirect flow and token exchange. Serialize/deserialize user to/from session: passport.serializeUser() and passport.deserializeUser().