@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.
Not in Main Barrel Export
PasswordChangedHook is not exported from the @nauth-toolkit/nestjs main entry point. Register this hook manually using HookRegistryService instead of the decorator pattern.
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
- Priority-based execution ordering
- 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,
});
}
}
With Priority
@Injectable()
@PasswordChangedHook({ priority: 1 })
export class PasswordChangedEmailHook implements IPasswordChangedHook {
// Executes first
}
Default Priority: 100
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 {}
Related
- IPasswordChangedHook - Hook interface
- HookRegistryService - Hook registry
- Lifecycle Hooks Guide - Complete hooks overview