Working with PassportJS

Detailed documentation on working with PassportJS library

Passport is the most popular node.js authentication library, well-known by the community and successfully used in many production applications. It's straightforward to integrate this library with a Nest application using the @nestjs/passport module

Steps to Use Passport in NestJS

  1. Install Dependencies You'll need to install @nestjs/passport, passport, and any specific Passport strategies you want to use (e.g., passport-jwt, passport-facebook, etc.).

    yarn add @nestjs/passport passport passport-jwt

  2. Create the Strategy A Passport strategy in NestJS is typically implemented as an injectable service by extending the PassportStrategy class from @nestjs/passport. You use this class to implement the required strategy.

    Example: Facebook Strategy for OAuth Authentication

    import { Injectable, UnauthorizedException } from '@nestjs/common';
    import { PassportStrategy } from '@nestjs/passport';
    import { Strategy, ExtractJwt } from 'passport-jwt';
    import { JwtUserPayload } from 'src/common/interfaces/jwt-user-payload.interface';
    import constants from 'src/constants';
    
    @Injectable()
    export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
        constructor() {
            super({
                jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
                secretOrKey: constants.TOKEN_SECRET,
            });
        }
    
        async validate(payload: JwtUserPayload): Promise<JwtUserPayload> {
            if (!payload) {
                throw new UnauthorizedException();
            }
            return payload;
        }
    }

    Here, validate is called when the strategy is triggered, and you check the credentials.

  3. Register the Strategy In your Auth Module, you register the strategy as a provider so that it can be injected and used.

    import { Module } from '@nestjs/common';
    import { PassportModule } from '@nestjs/passport';
    import { AuthService } from './auth.service';
    import { JwtStrategy } from './jwt-strategy';
    
    @Module({
      imports: [PassportModule],
      providers: [AuthService, JwtStrategy],
    })
    export class AuthModule {}

  4. Use the Strategy in a Guard, you apply Passport strategies using guards. NestJS has a built-in AuthGuard that integrates with Passport.

    Example: Facebook Auth Guard

    import { Injectable } from '@nestjs/common';
    import { AuthGuard } from '@nestjs/passport';
    
    @Injectable()
    export class JwtAuthenticationGuard extends AuthGuard('jwt') {}

    The string 'jwt' refers to the strategy defined in the FacebookStrategy class.

  5. Use the Guard in a Controller Now, you can apply the guard to a route in your controller to protect it using the Passport strategy.

    Example: Using the JwtAuthGuard in a Controller

    typescriptCopy codeimport { Controller, Post, UseGuards, Request } from '@nestjs/common';
    import { AuthService } from './auth.service';
    import { LocalAuthGuard } from './local-auth.guard';
    
    @Controller('auth')
    export class AuthController {
      constructor(private authService: AuthService) {}
    
      @UseGuards(JwtAuthenticationGuard)
        @Post('change-team')
        async changeTeam(@Body('teamId') teamId: string, @GetUser() user: JwtUserPayload) {
            return await this.authService.changeTeam(user, teamId);
        }
    }

    In this case JwtAuthenticationGuard will returns us Authenticated user data.

Key Components:

  1. Strategy: Implements a specific authentication mechanism.

  2. Guard: Applies the strategy to route handlers to protect them.

By using Passport strategies in NestJS, you can easily implement complex authentication mechanisms (like JWT, OAuth, etc.) in a modular and structured way.


Google oAuth2Facebook oAuth2

Last updated