File

src/auth/guards/http-access-token.guard.ts

Description

Guard for handling HTTP access tokens

Extends

JwtAuthGuardBase

Index

Methods

Methods

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

Attaches the user data to the request 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
Protected extractToken
extractToken(context: ExecutionContext)

Extracts the access token from the request cookies.

Parameters :
Name Type Optional Description
context ExecutionContext No
  • The execution context of the request.
Returns : string | undefined
  • The access token if present, otherwise undefined.
import { Injectable, ExecutionContext } from '@nestjs/common';
import {
  ACCESS_TOKEN_COOKIE_NAME,
  REQUEST_USER_KEY,
} from 'src/auth/constants/auth.constants';
import { JwtAuthGuardBase } from './jwt-auth-base.guard';
import { Request } from 'express';
import { ActiveUserData } from '../interfaces/active-user-data.interface';

/**
 * Guard for handling HTTP access tokens
 * @class HttpAccessTokenGuard
 * @extends JwtAuthGuardBase
 * @version 1
 * @description This guard extracts the access token from cookies and attaches the user data to the request context.
 */
@Injectable()
export class HttpAccessTokenGuard extends JwtAuthGuardBase {
  /**
   * Extracts the access token from the request cookies.
   * @param {ExecutionContext} context - The execution context of the request.
   * @returns {string | undefined} - The access token if present, otherwise undefined.
   */
  protected extractToken(context: ExecutionContext): string | undefined {
    const request = context.switchToHttp().getRequest<Request>();
    return request.cookies?.[ACCESS_TOKEN_COOKIE_NAME];
  }

  /**
   * Attaches the user data to the request context.
   * @param {ExecutionContext} context - The execution context of the request.
   * @param {ActiveUserData} payload - The user data to attach.
   */
  protected attachUserToContext(
    context: ExecutionContext,
    payload: ActiveUserData,
  ) {
    const request = context.switchToHttp().getRequest<Request>();
    request[REQUEST_USER_KEY] = payload;
  }
}

results matching ""

    No results matching ""