What is @nestjs/passport?
@nestjs/passport is a NestJS module that provides integration with the Passport authentication middleware. It allows developers to implement various authentication strategies in a NestJS application, such as local, JWT, OAuth, and more.
What are @nestjs/passport's main functionalities?
Local Authentication
This feature allows you to implement local authentication using a username and password. The code sample demonstrates how to set up a local strategy with Passport.
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
JWT Authentication
This feature allows you to implement JWT authentication. The code sample demonstrates how to set up a JWT strategy with Passport.
const passport = require('passport');
const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt');
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'your_jwt_secret'
};
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
User.findById(jwt_payload.sub, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
}
});
}));
OAuth Authentication
This feature allows you to implement OAuth authentication using providers like Google. The code sample demonstrates how to set up a Google OAuth strategy with Passport.
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
passport.use(new GoogleStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
clientSecret: 'YOUR_GOOGLE_CLIENT_SECRET',
callbackURL: 'http://www.example.com/auth/google/callback'
},
function(token, tokenSecret, profile, done) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}));
Other packages similar to @nestjs/passport
passport
Passport is a popular authentication middleware for Node.js. It is highly flexible and supports a wide range of authentication strategies. While @nestjs/passport is specifically designed for NestJS, Passport can be used with any Node.js application.
express-session
Express-session is a middleware for managing sessions in Express applications. While it does not provide authentication strategies like @nestjs/passport, it is often used in conjunction with Passport to manage user sessions.
jsonwebtoken
Jsonwebtoken is a library for generating and verifying JSON Web Tokens (JWT). It is often used in combination with Passport's JWT strategy to handle token-based authentication. Unlike @nestjs/passport, it does not provide a complete authentication framework but focuses on JWT handling.
A progressive Node.js framework for building efficient and scalable server-side applications.
Description
Passport utilities module for Nest.
Installation
$ npm i --save @nestjs/passport passport
Quick Start
Overview & Tutorial
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
License
Nest is MIT licensed.