Skip to content

Auth hooks

Auth hooks run your code at five points of the sign-in flow. Each hook is an HTTPS endpoint (signed with a secret) or a Postgres function. Set them up in the dashboard: Authentication → Hooks.

Hook Called Your answer
custom_access_token every time an access token is issued (sign-in, refresh) a JSON object of claims (at most 4 KB)
send_email every auth email: magic link, one-time code, recovery, invitation, confirmation. Replaces your SMTP provider any 2xx means delivered
send_sms every SMS with a one-time code any 2xx means delivered
password_verification after every password sign-in attempt, right or wrong { "decision": "continue" | "reject", "message"?: string }
mfa_verification after every MFA code attempt, right or wrong { "decision": "continue" | "reject", "message"?: string }

Choose Set up on a hook, pick HTTPS endpoint or Postgres function, set the timeout (100 to 10000 ms) and what happens when the hook fails. For an HTTPS endpoint Base shows the signing secret once: store it in your server’s secrets. Send test calls the hook once with a sample payload ("test": true).

Only organization owners can change hooks, because a hook receives emails, one-time codes and sign-in links. Developers can read the configuration.

You can also use the Management API (PUT /v1/projects/{ref}/auth/hooks/{kind} with { type, url | function, timeout_ms, fail_mode, enabled }). See the Management API.

Base sends a POST with a JSON body { "type": "<hook>", "project_id": "…", …payload }.

Hook Payload
custom_access_token user_id, session_id, auth_method, is_anonymous, app_metadata
send_email kind (magic_link, otp_sign-in, recovery, …), to, subject, html, text, url, token
send_sms to, message, token, purpose
password_verification valid, email, ip, and user_id when the account exists
mfa_verification valid, user_id, method (totp or recovery_code), factor_type

The request is signed with the Standard Webhooks scheme (webhook-id, webhook-timestamp, webhook-signature; HMAC-SHA256 over id.timestamp.body). Verify the signature and reject timestamps older than 5 minutes.

import { createHmac, timingSafeEqual } from 'node:crypto'
export function verify(headers: Record<string, string>, rawBody: string, secret: string) {
const key = Buffer.from(secret.replace('whsec_', ''), 'base64')
const mac = createHmac('sha256', key)
.update(`${headers['webhook-id']}.${headers['webhook-timestamp']}.${rawBody}`)
.digest()
const fresh = Math.abs(Date.now() / 1000 - Number(headers['webhook-timestamp'])) < 300
return fresh && headers['webhook-signature'].split(' ').some((s) => {
const got = Buffer.from(s.replace(/^v1,/, ''), 'base64')
return got.length === mac.length && timingSafeEqual(got, mac)
})
}

The endpoint must be public https (plain http is refused at run time too). Private, loopback and link-local addresses are refused, redirects are not followed, and the answer may be at most 64 KB.

schema.function(event jsonb) returns jsonb, run as the limited role base_auth_hook with a statement timeout equal to the hook timeout. Grant it EXECUTE. The function must live in your own schema (pg_*, information_schema, auth and base_internal are refused).

create or replace function public.password_policy(event jsonb) returns jsonb
language plpgsql security definer set search_path = '' as $$
begin
insert into public.login_attempts (email, ok, ip)
values (event->>'email', (event->>'valid')::boolean, event->>'ip');
if (event->>'valid')::boolean
and exists (select 1 from public.accounts where email = event->>'email' and blocked) then
return jsonb_build_object('decision', 'reject', 'message', 'Contact support');
end if;
return '{}'::jsonb;
end $$;
revoke all on function public.password_policy(jsonb) from public;
grant execute on function public.password_policy(jsonb) to base_auth_hook;

The single hook_function setting of Authentication keeps working; a custom_access_token hook set here takes precedence.

Setting A failing hook (timeout, error, non-2xx, unreadable answer)
Closed (default) fails the operation with 500 internal_error and details.reason = "<hook>_hook_failed" (claims_hook_failed for the claims hook)
Open is skipped; the operation continues as if no hook existed (send_email then uses your SMTP provider)

A reject decision is not a failure: it answers 403 hook_rejected with your message, whatever the setting. A wrong password or code is only reported to the hook (valid: false): the hook cannot turn it into a success. Use Closed for hooks that enforce a rule and Open for hooks that only add information.