File

src/auth/guards/jwt-auth-base.guard.ts

Description

Base class for JWT authentication guards

Implements

CanActivate

Index

Methods

Constructor

constructor(jwtService: JwtService, jwtConfiguration: ConfigType<>)

Creates an instance of JwtAuthGuardBase.

Parameters :
Name Type Optional Description
jwtService JwtService No
  • The JWT service for token verification.
jwtConfiguration ConfigType<> No
  • The JWT configuration.

Methods

Protected attachUserToContext
attachUserToContext(context: ExecutionContext, payload: ActiveUserData)

Abstract method to attach the user data to the execution context.

Parameters :
Name Type Optional Description
context ExecutionContext No
  • The execution context of the request.
payload ActiveUserData No
  • The user data to attach.
Returns : void
Async canActivate
canActivate(context: ExecutionContext)

CanActivate method to check if the request can be activated based on the JWT token.

Parameters :
Name Type Optional Description
context ExecutionContext No
  • The execution context of the request.
Returns : Promise<boolean>
  • Returns a promise that resolves to true if the request can be activated, otherwise throws an UnauthorizedException.
Protected Abstract extractToken
extractToken(context: ExecutionContext)

Abstract method to extract the token from the execution context.

Parameters :
Name Type Optional Description
context ExecutionContext No
  • The execution context of the request.
Returns : string | undefined
  • The extracted token if present, otherwise undefined.
import {
  CanActivate,
  ExecutionContext,
  Inject,
  UnauthorizedException,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import jwtConfig from '../config/jwt.config';
import { ConfigType } from '@nestjs/config';
import { ActiveUserData } from '../interfaces/active-user-data.interface';

/**
 * Base class for JWT authentication guards
 * @abstract
 * @class JwtAuthGuardBase
 * @implements {CanActivate}
 * @description This class provides the base functionality for JWT authentication guards, including token extraction and user attachment to the context.
 */
export abstract class JwtAuthGuardBase implements CanActivate {
  /**
   * Creates an instance of JwtAuthGuardBase.
   * @param {JwtService} jwtService - The JWT service for token verification.
   * @param {ConfigType<typeof jwtConfig>} jwtConfiguration - The JWT configuration.
   */
  constructor(
    protected readonly jwtService: JwtService,
    @Inject(jwtConfig.KEY)
    private readonly jwtConfiguration: ConfigType<typeof jwtConfig>,
  ) {}

  /**
   * Abstract method to extract the token from the execution context.
   * @param {ExecutionContext} context - The execution context of the request.
   * @returns {string | undefined} - The extracted token if present, otherwise undefined.
   */
  protected abstract extractToken(
    context: ExecutionContext,
  ): string | undefined;

  /**
   * CanActivate method to check if the request can be activated based on the JWT token.
   * @param {ExecutionContext} context - The execution context of the request.
   * @returns {Promise<boolean>} - Returns a promise that resolves to true if the request can be activated, otherwise throws an UnauthorizedException.
   * @throws {UnauthorizedException} - If the user is not authenticated or does not have the required permissions.
   */
  async canActivate(context: ExecutionContext): Promise<boolean> {
    const token = this.extractToken(context);
    if (!token) {
      throw new UnauthorizedException();
    }
    try {
      const payload = await this.jwtService.verifyAsync(
        token,
        this.jwtConfiguration,
      );
      this.attachUserToContext(context, payload);
      return true;
    } catch {
      throw new UnauthorizedException();
    }
  }

  /**
   * Abstract method to attach the user data to the execution context.
   * @param {ExecutionContext} context - The execution context of the request.
   * @param {ActiveUserData} payload - The user data to attach.
   */
  protected attachUserToContext(
    context: ExecutionContext,
    payload: ActiveUserData,
  ) {}
}

results matching ""

    No results matching ""