Skip to main content

Lifecycle Hooks

Lifecycle hooks let you inject custom logic at specific points in the authentication flow --- validation, notifications, integrations, and business logic without modifying core authentication code.

How Hooks Work

Available Hooks

User Lifecycle Hooks

HookWhenCan Block?Use Cases
preSignupBefore user creationYesValidation, domain whitelisting, invite codes
postSignupAfter user creationNoAnalytics, CRM sync, resource provisioning
onboardingCompletedWhen onboarding is completeNoWelcome emails, onboarding flows, "account ready" notifications
userProfileUpdatedAfter profile attribute changesNoCRM sync, analytics tracking, audit logging

Security & Authentication Hooks

HookWhenCan Block?Use Cases
passwordChangedAfter password changeNoSecurity alerts, force logout notifications
mfaFirstEnabledAfter first MFA device setupNoCongratulations email, security confirmation
mfaDeviceRemovedAfter MFA device deletionNoSecurity alerts, backup device reminders
mfaMethodAddedAfter MFA method addedNoSecurity alerts, inventory tracking
adaptiveMfaRiskDetectedWhen adaptive MFA risk evaluation runs and notifyUser: trueNoRisk alert emails, admin notifications, SIEM logging

Account Management Hooks

HookWhenCan Block?Use Cases
accountStatusChangedAfter account enable/disableNoAccount disabled notifications, re-enablement confirmations
emailChangedAfter email address changeNoDual notification (old + new email), security alerts
accountLockedAfter account lockoutNoLockout notifications, unlock instructions
sessionsRevokedAfter sessions revokedNoSecurity alerts, forced logout notifications

Hook Behavior

Blocking vs Non-Blocking

  • Blocking Hooks (preSignup): Can throw exceptions to prevent the operation
  • Non-Blocking Hooks (all others): Errors are logged but don't affect the operation

Execution Order

Hooks execute in priority order (lower values first). Multiple hooks with the same priority execute in registration order:

// NestJS with decorators
@PasswordChangedHook({ priority: 1 }) // Executes first
export class EmailNotificationHook implements IPasswordChangedHook {}

@PasswordChangedHook({ priority: 2 }) // Executes second
export class AnalyticsHook implements IPasswordChangedHook {}

@PasswordChangedHook() // Default priority 100, executes last
export class CrmSyncHook implements IPasswordChangedHook {}

// Express/Fastify manual registration
hookRegistry.registerPasswordChanged(new EmailNotificationHook());
hookRegistry.registerPasswordChanged(new AnalyticsHook());
hookRegistry.registerPasswordChanged(new CrmSyncHook());
// Registration order determines execution order

Error Handling

Non-blocking hooks catch and log errors automatically:

// Hook throws error → Error logged → Next hook still executes
try {
await hook.execute(metadata);
} catch (error) {
logger.error('Hook failed:', error);
// Continue with next hook
}

API Reference

Interfaces

InterfaceDescriptionDocumentation
IPreSignupHookProviderPre-signup hook interfaceIPreSignupHookProvider
IPostSignupHookProviderPost-signup hook interfaceIPostSignupHookProvider
IOnboardingCompletedHookOnboarding completed hook interfaceIOnboardingCompletedHook
IUserProfileUpdatedHookUser profile updated hook interfaceIUserProfileUpdatedHook
IPasswordChangedHookPassword changed hook interfaceIPasswordChangedHook
IMFAFirstEnabledHookMFA first enabled hook interfaceIMFAFirstEnabledHook
IMFADeviceRemovedHookMFA device removed hook interfaceIMFADeviceRemovedHook
IMFAMethodAddedHookMFA method added hook interfaceIMFAMethodAddedHook
IAdaptiveMFARiskDetectedHookAdaptive MFA risk detected hookIAdaptiveMFARiskDetectedHook
IAccountStatusChangedHookAccount status changed hook interfaceIAccountStatusChangedHook
IEmailChangedHookEmail changed hook interfaceIEmailChangedHook
IAccountLockedHookAccount locked hook interfaceIAccountLockedHook
ISessionsRevokedHookSessions revoked hook interfaceISessionsRevokedHook

Services

ServiceDescriptionDocumentation
HookRegistryServiceHook registration serviceHookRegistryService

NestJS Decorators

note

There is currently no @OnboardingCompletedHook() decorator. If you need custom onboarding-completed behavior in NestJS, register an IOnboardingCompletedHook via HookRegistryService.

DecoratorDescriptionDocumentation
@PreSignupHook()Pre-signup hook decorator@PreSignupHook()
@PostSignupHook()Post-signup hook decorator@PostSignupHook()
@PasswordChangedHook()Password changed hook decorator@PasswordChangedHook()
@MFAFirstEnabledHook()MFA first enabled hook decorator@MFAFirstEnabledHook()
@MFADeviceRemovedHook()MFA device removed hook decorator@MFADeviceRemovedHook()
@AdaptiveMFARiskDetectedHook()Adaptive MFA risk detected hook@AdaptiveMFARiskDetectedHook()
@AccountStatusChangedHook()Account status changed hook decorator@AccountStatusChangedHook()
@EmailChangedHook()Email changed hook decorator@EmailChangedHook()
@AccountLockedHook()Account locked hook decorator@AccountLockedHook()
@SessionsRevokedHook()Sessions revoked hook decorator@SessionsRevokedHook()
@UserProfileUpdatedHook()User profile updated hook decorator@UserProfileUpdatedHook()
NAuthHooksModuleHook registration moduleNAuthHooksModule

What's Next