File

apps/recallassess/recallassess-api/src/api/shared/decorators/client-auth.decorator.ts

Description

Type definition for data returned by @ClientAuth() decorator

Index

Properties

Properties

companyId
companyId: number
Type : number
email
email: string
Type : string
firstName
firstName: string
Type : string
fullName
fullName: string
Type : string
isActive
isActive: boolean
Type : boolean
lastName
lastName: string
Type : string
participant
participant: literal type
Type : literal type
participantId
participantId: number
Type : number
role
role: string
Type : string
import { getCLRequestContext } from "@api/shared/context";
import { createParamDecorator, UnauthorizedException } from "@nestjs/common";

/**
 * Parameter decorator to inject authenticated client (participant) data into controller methods
 *
 * @example
 * ```typescript
 * @Get()
 * async getParticipants(@ClientAuth() auth: CLAuthData) {
 *   // auth.companyId and auth.participantId are available
 *   return this.service.getFilteredParticipants(auth.companyId);
 * }
 * ```
 *
 * @throws UnauthorizedException if no authenticated participant is found
 */
export const ClientAuth = createParamDecorator(() => {
  // Get current request context
  const requestContext = getCLRequestContext();

  if (!requestContext) {
    throw new UnauthorizedException("Client request context not available. Ensure middleware is applied.");
  }

  const participant = requestContext.participantLoggedIn;

  if (!participant) {
    throw new UnauthorizedException("Authentication required");
  }

  // Return auth data
  return {
    companyId: participant.company_id,
    participantId: participant.id,
    participant: participant,
    email: participant.email,
    firstName: participant.first_name,
    lastName: participant.last_name,
    fullName: `${participant.first_name} ${participant.last_name}`.trim(),
    role: participant.role,
    isActive: participant.is_active,
  };
});

/**
 * Type definition for data returned by @ClientAuth() decorator
 */
export interface CLAuthData {
  companyId: number;
  participantId: number;
  participant: {
    id: number;
    company_id: number;
    email: string;
    first_name: string;
    last_name: string;
    role: string;
    is_active: boolean;
  };
  email: string;
  firstName: string;
  lastName: string;
  fullName: string;
  role: string;
  isActive: boolean;
}

results matching ""

    No results matching ""