Skip to main content

@EmailChangedHook()

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

Class decorator that automatically registers a provider as an email changed hook. Executes after email address changes. Non-blocking - errors are logged but don't affect email change.

Overview​

The @EmailChangedHook() 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 email change

Usage​

Basic Hook​

import { Injectable } from '@nestjs/common';
import {
EmailChangedHook,
IEmailChangedHook,
EmailChangedMetadata,
} from '@nauth-toolkit/nestjs';

@Injectable()
@EmailChangedHook()
export class EmailChangedNotificationHook implements IEmailChangedHook {
constructor(private readonly emailService: EmailService) {}

async execute(metadata: EmailChangedMetadata): Promise<void> {
// Alert to old email
await this.emailService.sendEmailChangedAlertEmail({
to: metadata.oldEmail,
newEmail: metadata.newEmail,
});

// Confirmation to new email
await this.emailService.sendEmailChangedConfirmationEmail({
to: metadata.newEmail,
});
}
}

Module Registration​

import { Module } from '@nestjs/common';
import { AuthModule, NAuthHooksModule } from '@nauth-toolkit/nestjs';
import { EmailChangedNotificationHook } from './hooks/email-changed.hook';

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