@MFAFirstEnabledHook()
Package: @nauth-toolkit/nestjs
Type: Class Decorator
Class decorator that automatically registers a provider as an MFA first enabled hook. Executes when user enables their first MFA device. Non-blocking - errors are logged but don't affect enrollment.
Overview
The @MFAFirstEnabledHook() decorator enables automatic hook registration. Classes decorated with this decorator are discovered at module initialization and registered with the HookRegistryService.
Key Features:
- Automatic hook discovery and registration
- Full dependency injection support
- Executes in
NAuthHooksModule.forFeature()registration order - Non-blocking - errors don't affect enrollment
Usage
Basic Hook
import { Injectable } from '@nestjs/common';
import {
MFAFirstEnabledHook,
IMFAFirstEnabledHook,
MFAFirstEnabledMetadata,
} from '@nauth-toolkit/nestjs';
@Injectable()
@MFAFirstEnabledHook()
export class MFAFirstEnabledConfirmationHook implements IMFAFirstEnabledHook {
constructor(private readonly emailService: EmailService) {}
async execute(metadata: MFAFirstEnabledMetadata): Promise<void> {
await this.emailService.sendMFAEnabledCongratulationsEmail({
to: metadata.user.email,
method: metadata.firstMethod,
});
}
}
Module Registration
import { Module } from '@nestjs/common';
import { AuthModule, NAuthHooksModule } from '@nauth-toolkit/nestjs';
import { MFAFirstEnabledConfirmationHook } from './hooks/mfa-first-enabled.hook';
@Module({
imports: [
AuthModule.forRoot(authConfig),
NAuthHooksModule.forFeature([MFAFirstEnabledConfirmationHook]),
],
})
export class CustomAuthModule {}
Related
- IMFAFirstEnabledHook - Hook interface
- HookRegistryService - Hook registry
- Lifecycle Hooks Guide - Complete hooks overview