33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
function b64ToBytes(b64) {
|
|
const bin = atob(b64.replace(/-/g, "+").replace(/_/g, "/"));
|
|
const bytes = new Uint8Array(bin.length);
|
|
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
|
|
return bytes;
|
|
}
|
|
|
|
document.getElementById("add-passkey").addEventListener("click", async () => {
|
|
const csrf = document.getElementById("add-passkey").dataset.csrf;
|
|
const start = await fetch("/auth/passkey/register/start", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ csrf }),
|
|
});
|
|
const options = await start.json();
|
|
const pk = options.publicKey;
|
|
pk.challenge = b64ToBytes(pk.challenge);
|
|
pk.user.id = b64ToBytes(pk.user.id);
|
|
if (pk.excludeCredentials) {
|
|
pk.excludeCredentials.forEach((c) => (c.id = b64ToBytes(c.id)));
|
|
}
|
|
const credential = await navigator.credentials.create(options);
|
|
const response = { csrf, response: credential };
|
|
const finish = await fetch("/auth/passkey/register/finish", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(response),
|
|
});
|
|
if (finish.ok) {
|
|
window.location.href = "/account";
|
|
}
|
|
});
|