Passport & Strategies

Nest integrates Passport, giving you authentication strategies as injectable providers.

Syntaxclass LocalStrategy extends PassportStrategy(Strategy)

Passport is the most popular Node.js authentication library, and Nest wraps it with @nestjs/passport. Passport organizes login logic into strategies.

How it fits Nest

A strategy is a class that extends PassportStrategy. You implement a validate() method that returns the authenticated user, and Nest attaches it to the request.

Common strategies include local (username and password) and jwt (bearer token). Install the matching packages, e.g. @nestjs/passport passport passport-local.

Example

Example · typescript
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { AuthService } from './auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private auth: AuthService) {
    super();
  }

  async validate(username: string, password: string) {
    const user = await this.auth.validateUser(username, password);
    if (!user) throw new UnauthorizedException();
    return user; // attached to request.user
  }
}

When to use it

  • A developer registers a LocalStrategy (username/password) with Passport so the login endpoint validates credentials via the existing UsersService.
  • A team adds a JwtStrategy that Passport uses to verify the Bearer token on every protected route without writing the verification logic manually.
  • An engineer adds a GoogleStrategy to enable OAuth sign-in, reusing Passport's callback mechanism that Nest already understands.

More examples

Install Passport packages

Installs the Nest Passport adapter, Passport core, and the local (username/password) strategy with its type definitions.

Example · bash
npm install @nestjs/passport passport passport-local
npm install -D @types/passport-local

LocalStrategy implementation

Extends PassportStrategy with the local strategy, using email as the username field and delegating validation to AuthService.

Example · ts
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { AuthService } from '../auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({ usernameField: 'email' });
  }

  async validate(email: string, password: string) {
    const user = await this.authService.validateUser(email, password);
    if (!user) throw new UnauthorizedException();
    return user;
  }
}

Register strategy in AuthModule

Registers PassportModule and LocalStrategy as providers so Nest can resolve the strategy when the auth guard requests it.

Example · ts
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './strategies/local.strategy';
import { AuthService } from './auth.service';

@Module({
  imports: [PassportModule, UsersModule],
  providers: [AuthService, LocalStrategy],
})
export class AuthModule {}

Discussion

  • Be the first to comment on this lesson.