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.
Send the notice from the hook
Section titled âSend the notice from the hookâ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.
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.", ) } })}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.
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.
What the browser shows instead
Section titled âWhat the browser shows insteadâ<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.
Related
Section titled âRelatedâ- 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.