Skip to main content
The AuthConfig type provides hooks for authentication events. Configure these in your startApp call under the auth: key.

Validation Hooks

validateSignup

Called before a new user is created during email/password signup. Use this to enforce custom validation rules on signup data. Throw an error to reject the signup.
Don’t enforce password rules here. validateSignup only runs at signup, so a user could sign up with a compliant password and immediately reset to a weaker one — password reset would only enforce the framework’s built-in 8-character floor. Use validatePassword instead: it runs on every password-setting path.
Props: Returns: void | Promise<void>

validatePassword

Enforces your password policy on every path that sets a password — signup and password reset today, plus any future change-password flow. Throw an error to reject the password; the message is surfaced to the client. This is the hook to use for password rules. Modelence enforces a built-in floor of 8 characters (and a 128-character ceiling) before calling it, so your hook only needs to add your own rules on top.
The hook runs after the built-in length checks and before the password is hashed. On the reset path it runs before the reset token is consumed, so a user whose password is rejected can retry with the same link. Props: Returns: void | Promise<void> Use context when a flow needs different treatment — for example, checking a breach-database only on reset:

onBeforeSignup

Available since modelence@0.17.0.
Called after validateSignup and the built-in disposable-email check, but before the user document is inserted. Use this to plug in a custom domain-policy check (e.g. a tenant-specific email-domain verification service) without disabling the built-in disposable-email check. Throw an error to reject the signup — the thrown error is re-thrown to the caller and onSignupError fires. Invoked for 'email' and 'magicLink' provider signups. OAuth signups are not gated because OAuth providers (Google, GitHub, etc.) do not issue disposable accounts.
To replace the built-in disposable-email check entirely with your own logic, set allowDisposableEmails: true (also available since modelence@0.17.0) and enforce your policy in onBeforeSignup.
Props: Returns: void | Promise<void>

validateProfileUpdate

Called before a user’s profile is updated. Use this to enforce custom validation rules on profile updates. Throw an error to reject the update.
Props: Returns: void | Promise<void>

Custom Handle Generation

generateHandle

By default, handles are derived from the user’s email address. This hook lets you generate custom handles based on the user’s email and profile information.
Props: Returns: string | Promise<string> — The generated handle. If the handle conflicts with an existing one, Modelence will automatically append a numeric suffix.

Auth Events

Error Rendering

errorComponent

Use errorComponent to customize how OAuth authentication errors are rendered. By default, OAuth errors are returned as JSON. Providing errorComponent allows you to return a custom HTML response instead. This is useful when OAuth flows are triggered in a browser context.

Full Example