Skip to main content

@AccountStatusChangedHook()

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

Class decorator that automatically registers a provider as an account status changed hook. Executes after account enable/disable operations. Non-blocking - errors are logged but don't affect status change.

Overview​

The @AccountStatusChangedHook() 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 status change

Usage​

Basic Hook​

import { Injectable } from '@nestjs/common';
import {
AccountStatusChangedHook,
IAccountStatusChangedHook,
AccountStatusChangedMetadata,
} from '@nauth-toolkit/nestjs';

@Injectable()
@AccountStatusChangedHook()
export class AccountStatusNotificationHook implements IAccountStatusChangedHook {
constructor(private readonly emailService: EmailService) {}

async execute(metadata: AccountStatusChangedMetadata): Promise<void> {
if (metadata.status === 'disabled') {
await this.emailService.sendAccountDisabledEmail({
to: metadata.user.email,
reason: metadata.reason,
});
}
}
}

Module Registration​

import { Module } from '@nestjs/common';
import { AuthModule, NAuthHooksModule } from '@nauth-toolkit/nestjs';
import { AccountStatusNotificationHook } from './hooks/account-status.hook';

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