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
phoneChangedAfter phone number changeNoAlert the account email, flag removed SMS MFA devices
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 registration order. In NestJS that is the order they are listed in NAuthHooksModule.forFeature():

// NestJS - array order is execution order
NAuthHooksModule.forFeature([
EmailNotificationHook, // Executes first
AnalyticsHook, // Executes second
CrmSyncHook, // Executes last
]);

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

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
IPhoneChangedHookPhone changed hook interfaceIPhoneChangedHook
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()
@PhoneChangedHook()Phone changed hook decorator@PhoneChangedHook()
@SessionsRevokedHook()Sessions revoked hook decorator@SessionsRevokedHook()
@UserProfileUpdatedHook()User profile updated hook decorator@UserProfileUpdatedHook()
NAuthHooksModuleHook registration moduleNAuthHooksModule

What's Next​