Skip to main content

@AdaptiveMFARiskDetectedHook()

Package: @nauth-toolkit/nestjs Type: Class Decorator

Class decorator that automatically registers a provider as an adaptive MFA risk detected hook. Executes when adaptive MFA detects risk factors. Non-blocking - errors are logged but don't affect authentication.

Overview​

The @AdaptiveMFARiskDetectedHook() 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 authentication

Usage​

Basic Hook​

import { Injectable } from '@nestjs/common';
import {
AdaptiveMFARiskDetectedHook,
IAdaptiveMFARiskDetectedHook,
AdaptiveMFARiskDetectedMetadata,
} from '@nauth-toolkit/nestjs';

@Injectable()
@AdaptiveMFARiskDetectedHook()
export class RiskAlertHook implements IAdaptiveMFARiskDetectedHook {
constructor(private readonly emailService: EmailService) {}

async execute(metadata: AdaptiveMFARiskDetectedMetadata): Promise<void> {
if (metadata.riskLevel === 'high') {
await this.emailService.sendRiskAlertEmail({
to: metadata.user.email,
riskScore: metadata.riskScore,
riskFactors: metadata.riskFactors,
});
}
}
}

Module Registration​

import { Module } from '@nestjs/common';
import { AuthModule, NAuthHooksModule } from '@nauth-toolkit/nestjs';
import { RiskAlertHook } from './hooks/risk-alert.hook';

@Module({
imports: [
AuthModule.forRoot(authConfig),
NAuthHooksModule.forFeature([RiskAlertHook]),
],
})
export class CustomAuthModule {}