import { useState } from "react"; import { startRegistration } from "@simplewebauthn/browser"; import { api } from "./api.js"; export function Enroll({ token, pdsHostname, onEnrolled, }: { token: string; pdsHostname: string; onEnrolled: () => void; }) { const [name, setName] = useState(""); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const enroll = async () => { setBusy(true); setError(null); try { const options = await api.passkeyRegisterOptions(token); const response = await startRegistration({ optionsJSON: options }); await api.passkeyRegister(name.trim(), response, token); window.history.replaceState(null, "", "/"); onEnrolled(); } catch (e) { const err = e as Error; if (err.name !== "NotAllowedError") setError(err.message); } finally { setBusy(false); } }; return (
{pdsHostname ? `${pdsHostname} — ` : ""}enroll a passkey setName(e.target.value)} autoFocus /> {error && {error}}
); }