Skip to content

Send security notices

Keyhaw holds no email address and never contacts a user, so a notice about a new passkey is yours to send. Send it from an event hook, to the address in your own records. Keyhaw never sees that address. Send a notice for passkey.added, and a stronger one for backup_code.used, since a recovery means the person could not sign in with a passkey they already had.

Look up the address by the user’s ID, and send only when your records have one. The hook hands the send to the platform, so the response does not wait for the mail server.

lib/security-notices.ts
import type { KeyhawEvent } from "keyhaw/server"
import { after } from "next/server"
import { sendEmail } from "./mail"
export function notify(event: KeyhawEvent): void {
if (event.type !== "passkey.added" && event.type !== "backup_code.used") return
// `after` keeps the send running once the response has gone
after(async () => {
const address = await db.accounts.emailFor(event.user.id)
if (address === null) return
if (event.type === "backup_code.used") {
await sendEmail(
address,
"Your account was recovered with a backup code",
"If this was not you, sign in and remove the passkey, then change your backup codes.",
)
} else {
await sendEmail(
address,
"A passkey was added to your account",
"If you did not add this passkey, sign in and remove it.",
)
}
})
}
proxy.ts
import { createProxy } from "@keyhaw/next"
import { notify } from "./lib/security-notices"
export const proxy = createProxy({ onEvent: notify })

On Astro running on Cloudflare Workers, the platform stops the work when the response ends, so waitUntil from cloudflare:workers keeps the send running.

src/keyhaw-events.ts
import type { KeyhawEvent } from "keyhaw/server"
import { waitUntil } from "cloudflare:workers"
import { sendEmail } from "./mail"
export default function notify(event: KeyhawEvent): void {
if (event.type !== "passkey.added" && event.type !== "backup_code.used") return
waitUntil(
(async () => {
const address = await db.accounts.emailFor(event.user.id)
if (address === null) return
await sendEmail(
address,
"A passkey was added to your account",
"If you did not add this passkey, sign in and remove it.",
)
})(),
)
}

A send is not retried. Keyhaw does not log a failure in a send you hand to after or waitUntil, so catch the error in your own code, and keep the address out of what you log.

<Account /> also shows a passkey that a backup code added. It shows the line “A backup code added a passkey on” followed by the date, with a Dismiss button, and it sends no notice. Keep your own notice for the person’s email, since a person who never opens the account page would not see that line. It shows only for a passkey a backup code added after the passkey that opened the current session, and it stays until the person dismisses it.

  • Event hooks covers the hook, its wait, and what each runtime does with a send still running after the response.
  • Handle a duplicate sign-up covers the sign-ups a notice does not catch.