nest-profiler-auth
Types and API of the auth & security collector.
AuthCollectorModuleOptions
Options passed to AuthCollectorModule.forRoot().
Prop
Type
SecurityContext
The data structure populated by the auth collector and stored in profile.security. Defined in @eleven-labs/nest-profiler.
interface SecurityContext {
isAuthenticated: boolean;
user?: Record<string, unknown>; // request.user, with masked fields
roles?: string[]; // user.roles or user.role (normalized)
jwtClaims?: Record<string, unknown>; // decoded JWT payload (no verification)
}See SecurityContext in the core package reference.
Automatic masking
Fields in user whose name matches the pattern /password|secret|key|token|credential|api_key|apikey/i are replaced with ***. Additional fields can be specified via maskUserFields.
Public exports
import { AuthCollectorModule } from '@eleven-labs/nest-profiler-auth';
import { AuthCollector } from '@eleven-labs/nest-profiler-auth';
import type {
AuthCollectorModuleOptions,
AuthCollectorModuleAsyncOptions,
} from '@eleven-labs/nest-profiler-auth';Setup
// In AuthModule (or any module that sets request.user):
import { ConditionalModule } from '@nestjs/config';
import { AuthCollectorModule } from '@eleven-labs/nest-profiler-auth';
const isProfilerEnabled = (env: NodeJS.ProcessEnv) => env['PROFILER_ENABLED'] === 'true';
@Module({
imports: [
ConditionalModule.registerWhen(
AuthCollectorModule.forRoot({ maskUserFields: ['password', 'refreshToken'] }),
isProfilerEnabled,
),
],
})
export class AuthModule {}Prerequisite: A guard or middleware that sets request.user (Passport, custom JWT guard, etc.). The collector reads request.user and the Authorization header from the per-request CLS context.
Async setup
Drive the options from ConfigService (or any provider) with forRootAsync(), gated per environment with ConditionalModule (recommended). It resolves option values only — for the enabled flag and when to prefer forRoot, see Enabling and disabling the profiler.
import { ConditionalModule, ConfigService } from '@nestjs/config';
import { AuthCollectorModule } from '@eleven-labs/nest-profiler-auth';
const isProfilerEnabled = (env: NodeJS.ProcessEnv) => env['PROFILER_ENABLED'] === 'true';
ConditionalModule.registerWhen(
AuthCollectorModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
maskUserFields: config.get<string[]>('profiler.maskUserFields') ?? [
'password',
'refreshToken',
],
}),
}),
isProfilerEnabled,
);