Skip to content

Sign-in button

<SignIn /> is one button. A signed-out visitor clicks it and their browser asks for a passkey, and the button tells them what to do next in its own label. This page shows the button alone and in a form, lists every label with the code that causes it, and walks through the same flow in plain JavaScript for an app that does not use React.

Render <SignIn /> where the sign-in should go, and import the stylesheet once.

import { SignIn } from "@keyhaw/react"
import "keyhaw/styles.css"
export function App() {
return <SignIn />
}

The button reads “Continue with a passkey” until a click changes it. A signed-in visitor sees “Sign out” instead, and a click signs them out, which ends their session at once, though an access token already issued works until it expires, for up to 10 minutes. The button is the same element in every state, so keyboard focus stays on it as its label changes.

Put the button inside a form, and give the form an input marked webauthn in its autocomplete. The token goes last, since HTML reads the last token as the one that marks a passkey input.

import { SignIn } from "@keyhaw/react"
export function SignInForm() {
return (
<form action="/account">
<label>
Email
<input name="email" type="email" autoComplete="username webauthn" required />
</label>
<SignIn />
</form>
)
}

The form works as follows.

  • An invalid form opens no prompt. The button checks the form’s own validation first, so a field marked required that is empty stops the click before the browser asks for a passkey.
  • Once the button signs the user in, it submits the form, so the form’s action or onSubmit runs with the session in place. A form with no action stays on the page.
  • Make the button the form’s first submit button, so pressing Enter in a field reaches it.
  • The value of the form’s webauthn input names the passkey the button creates, as passkey names describes.
  • Keep one input marked webauthn on a page. The browser holds one passkey offer for the whole page, so a page with two such forms offers passkeys in both and submits the form of the button that opened the offer last.

Once the page loads, the browser offers passkeys in that input’s autofill, so a user who picks one signs in without clicking the button.

The label names only what the next click does, and never what went wrong. When a call fails, the button keeps a label from the first table, and one status line after it says what went wrong until the next click.

Label When it shows Code
Continue with a passkey Signed out, and after every failure the second table lists None, or the failure’s code
Create an account The sign-in prompt ended with no passkey, or the create prompt was closed no-passkey-used, cancelled
Working… A sign-in or sign-up is running, and clicks do nothing None
Sign out The user is signed in None
The status line reads When it shows Code
That passkey isn’t for this site. Try another passkey. The passkey the user picked belongs to a different app wrong-passkey
This account is suspended. The account is suspended and cannot sign in account-suspended
Too many attempts. Try again in a few minutes. The sign-in was refused for too many attempts rate-limited
This browser can’t use passkeys. The browser has no passkey support unsupported
That didn’t work. Try again. Any other failure, including a network or server error registered-sign-in, network, server, origin-rejected, cancelled after a sign-in, and every other code

The status line is empty otherwise, and it takes no space while empty. A click on “Create an account” opens the prompt that makes a passkey. After a wrong passkey, a suspended account, a browser with no passkey support, or a sign-up that reports registered-sign-in, the next click opens the sign-in prompt, and after any other failure it repeats the call that failed. A sign-out puts the button back to “Continue with a passkey”.

The status line is a <span> with the class keyhaw-sign-in-status and role="status", so a screen reader announces each message while focus stays on the button. keyhaw/styles.css puts it on a line of its own under the button. Without the stylesheet it is plain text, and you style it through that class. A page that wants more words than the status line shows listens for the client’s error event, which carries the failure’s code.

no-passkey-used says only that the sign-in prompt ended with no passkey. It never means the user has no account, since the browser reports a closed prompt, a device that holds no passkey for your site, and a timeout the same way, and a returning user whose passkey sits on another device reads the same as a new one.

Read the session with useSession() and render nothing when the user is signed in. A page that shows its own signed-in view can hide the button this way.

import { SignIn, useSession } from "@keyhaw/react"
export function SignInSlot() {
const session = useSession()
if (session.status === "signed-in") return null
return <SignIn />
}

A plain JavaScript page can subscribe to the same session with keyhaw.session.subscribe, which calls its listener as the session changes and returns a function that stops the subscription. The first subscription also asks the instance for the session, so the status reads loading until that answers.

Put a link that reads “Recover your account” beside the sign-in button. The link is for a user whose device holds none of their passkeys, since a sign-in fails when the device holds no passkey for the account. The button then reads “Create an account”, which would give a returning user a second account, so the link is their way back to the one they have.

import { SignIn } from "@keyhaw/react"
export function SignInPage() {
return (
<div>
<SignIn />
<a href="/recover">Recover your account</a>
</div>
)
}

Point the link at a page of your own that holds <RecoverAccount />. There the user enters a backup code they saved, and the component adds a passkey to the device they are on and signs them in with it. Recovery lives in that component, so the sign-in button does not carry it.

import { RecoverAccount } from "@keyhaw/react"
export function RecoverPage() {
return <RecoverAccount />
}

A sign-up creates a user and a passkey, and your code can cancel it before it happens. Handling a duplicate sign-up shows how to stop a sign-up when your own system already holds the account.

createKeyhaw makes the client that the button calls, and it works in any page. Every call resolves to { data, error }, and a failure reports its error.code from the table above.

import { createKeyhaw } from "keyhaw/client"
const keyhaw = createKeyhaw({ publishableKey: "pk_..." })
const { data, error } = await keyhaw.signIn()
if (error?.code === "no-passkey-used") {
await keyhaw.signUp()
}

Pass immediate: true to signIn to offer only a passkey the device already holds, with no dialog when it holds none. A browser without Immediate UI mode opens the full prompt instead.

const { data, error } = await keyhaw.signIn({ immediate: true })

Offer passkeys in the autofill of a form’s webauthn input with autofill. Pass the button the form belongs to, and an AbortSignal that ends the offer, and an onPick callback that runs when the user picks a passkey and the sign-in begins.

const button = document.querySelector("#sign-in")
const offer = new AbortController()
const { data, error } = await keyhaw.autofill({
button,
signal: offer.signal,
onPick: () => {
button.textContent = "Working…"
},
})

Sign the user out with signOut, which ends their session at once, though an access token already issued works until it expires, for up to 10 minutes.

await keyhaw.signOut()

A page without React runs the same flow the button runs inside a form. Mark the form’s input webauthn, as above, and give the form a submit button.

<form id="sign-in-form">
<input name="email" type="email" autocomplete="email webauthn" required />
<button id="sign-in" type="submit">Continue with a passkey</button>
<p id="sign-in-message" role="status"></p>
</form>

The script offers passkeys in autofill on load, checks the form before each prompt, ignores a click while a call runs, turns the button into “Create an account” when no passkey is used, and submits the form once the user is signed in, so your own submit handler runs with the session in place. The form has no action, so its handler keeps it on the page. A failure shows its message beside the button, and after one that means the account may already exist, the next click signs in again rather than create a second account.

const form = document.querySelector("#sign-in-form")
const button = document.querySelector("#sign-in")
const message = document.querySelector("#sign-in-message")
let next = "signIn"
let isRunning = false
// After these, the account may already exist, so the next click signs in rather than create another
const SIGN_IN_AGAIN = new Set([
"wrong-passkey",
"account-suspended",
"unsupported",
"registered-sign-in",
])
// The form has no action of its own, so it stays on the page and your code reads its fields
form.addEventListener("submit", (event) => {
event.preventDefault()
saveProfile(new FormData(form)).catch(console.error)
})
function showSignedIn() {
next = "signOut"
button.textContent = "Sign out"
// Your submit handler runs with the session in place
form.requestSubmit()
}
// Offer passkeys in the form's webauthn input while the user is signed out
async function offerAutofill() {
const { data, error } = await keyhaw.autofill({ button })
if (data !== null) showSignedIn()
else if (error !== null) await offerAutofill()
}
button.addEventListener("click", async (event) => {
// The click signs the user in first, so it never submits the form on its own
event.preventDefault()
// A second click while a call runs does nothing, and an invalid form opens no prompt
if (isRunning || (next !== "signOut" && !form.reportValidity())) return
isRunning = true
button.setAttribute("aria-disabled", "true")
message.textContent = ""
try {
if (next === "signOut") {
await keyhaw.signOut({ button })
next = "signIn"
button.textContent = "Continue with a passkey"
offerAutofill().catch(console.error)
return
}
const { data, error } =
next === "signIn" ? await keyhaw.signIn({ button }) : await keyhaw.signUp({ button })
if (data !== null) {
showSignedIn()
} else if (error?.code === "no-passkey-used" || error?.code === "cancelled") {
// No passkey was used, or the create prompt was closed, so the next click creates one
next = "signUp"
button.textContent = "Create an account"
} else if (error !== null) {
if (SIGN_IN_AGAIN.has(error.code)) next = "signIn"
message.textContent = error.message
}
} finally {
isRunning = false
button.removeAttribute("aria-disabled")
}
})
offerAutofill().catch(console.error)