Skip to main content

@PasswordChangedHook()

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

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

Overview​

The @PasswordChangedHook() 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 password change

Usage​

Basic Hook​

import { Injectable } from '@nestjs/common';
import {
PasswordChangedHook,
IPasswordChangedHook,
PasswordChangedMetadata,
} from '@nauth-toolkit/nestjs';

@Injectable()
@PasswordChangedHook()
export class PasswordChangedEmailHook implements IPasswordChangedHook {
constructor(private readonly emailService: EmailService) {}

async execute(metadata: PasswordChangedMetadata): Promise<void> {
await this.emailService.sendPasswordChangedAlert({
to: metadata.user.email,
changedBy: metadata.changedBy,
sessionsRevoked: metadata.sessionsRevoked || 0,
});
}
}

Module Registration​

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

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