Hooks Reference
Every config-time hook with its execution-order timeline.
This is the exhaustive hook reference. For the conceptual overview of when to use a hook vs an event, see Events & Hooks.
Execution-order timeline — signup flow
Execution-order timeline — login flow
Execution-order timeline — refresh flow
Hook reference
registrationHooks.beforeSignup(payload, ctx) => Promise<payload> | payload
Gate or mutate signup. Throw to abort.
registrationHooks.onSignup(user, payload) => Promise<void>
Runs after the user is inserted but before the first session is built. Assign initial roles here so they land in the first JWT:
user.beforeCreate(input) => input | Promise<input>
Final mutation before the user row is inserted.
user.afterCreate(user) => Promise<void>
Sync side effects. Throws here roll back the signup.
user.getSessionUserData(user, helpers) => SessionUserData | Promise<SessionUserData>
The most-called hook: runs on signup, login, and refresh. Use it to inject AppUser fields into the session payload.
user.sensitiveFields: string[]
List of NestAuthUser fields to never serialize into responses (e.g., 'metadata.internalNotes').
loginHooks.onLogin(user, ctx) => Promise<void>
ctx carries request, userAccess, platformAccess. Useful for syncing external systems on every login.
session.customizeSessionData(default, user) => SessionDataPayload
Reshape the snapshot stored on the session row. Don't bloat — this is loaded on every refresh.
session.customizeTokenPayload(default, session) => JWTTokenPayload
Final claims for the JWT. Anything you add here is in the access token. Keep it small.
session.onCreated / onRefreshed / onRevoked
Fire-and-forget. For audit, cache invalidation, etc.
authorization.resolveRoles(user) => Promise<string[]>
Replaces the default DB lookup. Return whatever roles the user has.
authorization.resolvePermissions(user, roles) => Promise<string[]>
Replaces the default role→permission expansion.
guards.beforeAuth(request) => { reject?, reason? } | Promise<…>
guards.afterAuth(request, user) => { reject?, reason? } | Promise<…>
Same shape, runs after auth succeeded. Useful for device fingerprint check that depends on user.
errorHandler(error, context) => any
context is one of 'login' | 'signup' | 'password_reset' | 'mfa_verify' | …. Transform the error into your app's standard shape.
resolveConfig(context) => Partial<IAuthModuleOptions> | Promise<…>
Per-request config override. The most common use is mode-switching:
clientConfig.factory(default, context) => any
Reshapes the public /auth/client-config response — what your frontend reads to decide which sign-in buttons to render.
password.hash(password) => Promise<string> / password.verify(password, hash) => Promise<boolean>
Drop-in replacements for argon2. Use this if your security team mandates a specific algorithm.
otp.generate(length, format) => string | Promise<string>
Drop-in replacement for the default OTP generator.
audit.onEvent(event) => Promise<void> | void
Sink for audit events. See Audit Logging.
Related
- Events & Hooks — when to pick a hook vs an event.
- Module reference.
- Hook recipes — concrete worked examples for the most useful hooks.