apps/recallassess/recallassess-api/src/api/shared/decorators/client-auth.decorator.ts
Type definition for data returned by @ClientAuth() decorator
Properties |
| companyId |
companyId:
|
Type : number
|
email:
|
Type : string
|
| firstName |
firstName:
|
Type : string
|
| fullName |
fullName:
|
Type : string
|
| isActive |
isActive:
|
Type : boolean
|
| lastName |
lastName:
|
Type : string
|
| participant |
participant:
|
Type : literal type
|
| participantId |
participantId:
|
Type : number
|
| role |
role:
|
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;
}