passkey-settings.js
Raw
1import { startRegistration } from "/assets/simplewebauthn-browser.js";
2
3const section = document.getElementById("passkey-section");
4const btn = document.getElementById("add-passkey-btn");
5const status = document.getElementById("passkey-status");
6
7if (section) section.style.display = "block";
8
9if (btn) {
10 const originalText = btn.textContent;
11 btn.addEventListener("click", async () => {
12 btn.disabled = true;
13 btn.textContent = "Waiting for authenticator…";
14 if (status) status.textContent = "";
15 try {
16 const optsResp = await fetch("/auth/passkey/register/options", {
17 method: "POST",
18 });
19 if (!optsResp.ok) throw new Error("Failed to get options");
20 const opts = await optsResp.json();
21 const result = await startRegistration({ optionsJSON: opts });
22 const verResp = await fetch("/auth/passkey/register/verify", {
23 method: "POST",
24 headers: { "Content-Type": "application/json" },
25 body: JSON.stringify(result),
26 });
27 if (verResp.ok) {
28 window.location.reload();
29 } else {
30 const err = await verResp.json().catch(() => ({}));
31 if (status)
32 status.textContent =
33 "Error: " + (err.error ?? "Registration failed");
34 }
35 } catch (e) {
36 if (status)
37 status.textContent =
38 "Error: " +
39 (e instanceof Error ? e.message : String(e));
40 } finally {
41 btn.disabled = false;
42 btn.textContent = originalText;
43 }
44 });
45}
46