What is Passport.js and how does it integrate with Express?
Answer
Passport.js is authentication middleware for Express that supports 500+ authentication strategies through a unified API. Common strategies: Local (username/password), JWT, OAuth2 (Google, GitHub, Facebook login). Initialize: app.use(passport.initialize());. Define a strategy: passport.use(new LocalStrategy(async (username, password, done) => { const user = await User.findOne({ username }); if (!user || !user.validPassword(password)) return done(null, false); return done(null, user); }));. Protect routes: app.get('/profile', passport.authenticate('jwt', { session: false }), handler);. Passport is flexible and well-documented, making it easy to add social logins or swap authentication mechanisms without changing route code.
Previous
What is the difference between PUT and PATCH in REST?
Next
How do you test an Express.js API?
More Express.js Questions
View all →- Intermediate How do you implement JWT authentication in Express.js?
- Intermediate What is Express middleware chaining and how does it work?
- Intermediate What is the helmet package and why should you use it?
- Intermediate How do you implement rate limiting in Express.js?
- Intermediate What is input validation and how do you do it in Express?