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 Handling

OAuth errors surface at the provider callback URL (/api/_internal/auth/...), which is a server route outside your client bundle. Nothing there can render your app’s UI, so by default the user lands on a JSON body with no way back. Two options change that.

oauthErrorRedirectUrl

Use oauthErrorRedirectUrl to send the browser back into your app when a web OAuth flow fails. It accepts a path, resolved against your site URL (MODELENCE_SITE_URL), or an absolute URL. The failure is appended as ?error=<message>&errorCode=<code>, the same contract a mobile flow delivers on its deep link, so your app can show it on a real page.
Then read the params on that page:
error is for display and its wording may change. Branch on errorCode (invalid_state, email_exists, account_inactive, …) for anything programmatic. When both oauthErrorRedirectUrl and errorComponent are set, the redirect wins and errorComponent is not called.

errorComponent (deprecated)

errorComponent is deprecated in favor of oauthErrorRedirectUrl. It renders OAuth errors as a standalone HTML document at the callback URL instead of JSON, which means the page cannot use your app’s UI. It is still honored for existing apps, but is ignored whenever oauthErrorRedirectUrl is set.

Full Example