Skip to content

Event hooks

An event hook runs your code when a user signs up, signs in, signs out, adds a passkey, or recovers with a backup code. Use one to keep your own record for each new user, or to send a notice when a passkey appears on an account. Keyhaw calls the hook from your server, so it runs only for requests that pass through your server. The hook never replaces the upsert on your own requests, which stays the source of truth.

Write the function that creates your record once, and call it from each hook. It takes the user from the event, which holds the user’s ID and the instance’s tenant ID and nothing else.

lib/user-records.ts
import type { KeyhawEvent } from "keyhaw/server"
export async function upsertUser(user: KeyhawEvent["user"]): Promise<void> {
// Create the row once, and leave an existing row as it is.
await db.userRecords.upsert({ keyhawId: user.id })
}

Each adapter takes the same function, called with the event. On Next.js, pass it to createProxy in your proxy.ts. The matcher from the quickstart stays as it is.

proxy.ts
import { createProxy } from "@keyhaw/next"
import { upsertUser } from "./lib/user-records"
export const proxy = createProxy({
onEvent: (event) => upsertUser(event.user),
})

On Astro, eventHookEntrypoint names a module whose default export is the hook. The module runs in the same server as the middleware, so it can import your database.

astro.config.mjs
import keyhaw from "@keyhaw/astro"
import { defineConfig } from "astro/config"
export default defineConfig({
output: "server",
integrations: [keyhaw({ eventHookEntrypoint: "./src/keyhaw-events.ts" })],
})
src/keyhaw-events.ts
import type { KeyhawEvent } from "keyhaw/server"
import { upsertUser } from "../lib/user-records"
export default (event: KeyhawEvent) => upsertUser(event.user)

On Vite, pass the hook to the plugin. A static site has no server to run it, so the plugin warns and ignores the option. With proxy: false, your framework’s own server serves /__keyhaw, so pass the hook to the proxy that server mounts.

vite.config.ts
import { keyhaw } from "@keyhaw/vite-plugin"
import { defineConfig } from "vite"
import { upsertUser } from "./lib/user-records"
export default defineConfig({
plugins: [keyhaw({ onEvent: (event) => upsertUser(event.user) })],
})

To mount the proxy by hand, pass the hook as the second argument to proxyRequest from keyhaw/server. It returns null for a request that is not for Keyhaw, so your router handles that request as it does any other.

import { proxyRequest } from "keyhaw/server"
import { upsertUser } from "./lib/user-records"
export async function handle(request: Request): Promise<Response | null> {
return proxyRequest(request, {
onEvent: (event) => upsertUser(event.user),
})
}

The hook receives five events. Each one comes from a single request to Keyhaw, and no request fires two of them.

Event Fires when
user.created A sign-up creates a user and their first passkey
user.signed_in A user signs in with a passkey
user.signed_out A user signs out
passkey.added A signed-in user adds a passkey to their account
backup_code.used A user recovers their account with a backup code, which adds a passkey and opens a session

A recovery fires backup_code.used and not passkey.added. A hook that reads both events covers every passkey a user did not create at sign-up. A request the Auth API refuses fires nothing.

The proxy waits for your hook up to two seconds before it answers the request. The page reads the session as soon as the response arrives, so a hook that finishes inside that wait is in place before the page needs it. Keep the work short, such as an upsert on a database near your server.

A hook that throws, or whose promise rejects, does not change the response. Keyhaw logs the event’s type and the error’s name, and the user stays signed in. Each event calls the hook once and never retries it, so a hook must not depend on running for every event.

What happens to a hook still running after the two seconds depends on where your server runs.

Runtime A hook still running when the response goes
Next.js on next start, whose proxy runs on Node.js Keeps running until it settles
Next.js on a serverless host May stop when the response ends, and after from next/server extends it
Astro on a Node.js adapter Keeps running until it settles
Astro on Cloudflare Workers Stops when the response ends, and waitUntil from cloudflare:workers extends it
Vite dev and preview servers Keeps running until it settles
A server that mounts proxyRequest by hand Follows its host, as the rows above do

For work that takes longer than the wait, such as sending a notice, hand it to the platform with after or waitUntil, as the security notices guide shows.

Keyhaw fires no event for a user deleted from the dashboard, for a suspension, or for a session that expires, since none of those passes through the proxy.

In the browser, keyhaw.on adds a handler for one of the client’s events and returns a function that removes it. The browser and the server use one naming rule. A past-tense name, such as user.created, says what happened. A present-participle name, such as user.creating, says what is about to happen.

The browser fires two events. user.creating runs before the passkey prompt opens for a sign-up that creates an account, and it never runs when a signed-in user adds a passkey. Call preventDefault() on it to stop the sign-up, which then creates no account and opens no prompt. error runs for every failure a call reports.

import { createKeyhaw } from "keyhaw/client"
const keyhaw = createKeyhaw()
const stop = keyhaw.on("user.creating", (event) => {
// `emailIsTaken` is your own check, and it must answer without waiting
if (emailIsTaken()) event.preventDefault()
})

A handler that throws is logged, and the next handler still runs, so a broken handler never stops a sign-up. A handler cannot wait for a promise, since the passkey prompt has to open inside the time the browser allows after a click. The duplicate sign-up guide runs its check as the person types, so the handler reads the answer without waiting.