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.

Not in Main Barrel Export

AdaptiveMFARiskDetectedHook is not exported from the @nauth-toolkit/nestjs main entry point. Register this hook manually using HookRegistryService instead of the decorator pattern.

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
  • Priority-based execution ordering
  • 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,
});
}
}
}

With Priority

@Injectable()
@AdaptiveMFARiskDetectedHook({ priority: 1 })
export class RiskEmailHook implements IAdaptiveMFARiskDetectedHook {
// Executes first
}

Default Priority: 100

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 {}