Skip to content

Commit a7e367a

Browse files
committed
Modernise the passkey.js client script
Two cleanups in one pass, both inside src/public/passkey.js: - Replace every `var` with `const`, except for the two locals in postJson's !response.ok branch (`detail` and `error.message` fallback path) which are written conditionally and stay `let`. The rest of the script was already using async/await and arrow functions, so `const` is in keeping with the style. - Replace the `(async function () { ... })()` IIFEs that wrapped the async work inside the submit and click handlers with `async (event) => { ... }` / `async () => { ... }` passed directly to addEventListener. The extra indentation level goes away and the handler is a single function instead of a sync function that immediately fires off an async one. No runtime behaviour change. #487 (comment) #487 (comment) Assisted-by: Claude Code:claude-opus-4-7
1 parent 6fc9c03 commit a7e367a

1 file changed

Lines changed: 56 additions & 60 deletions

File tree

src/public/passkey.js

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"use strict";
1111

1212
(function () {
13-
var lib = window.SimpleWebAuthnBrowser;
13+
const lib = window.SimpleWebAuthnBrowser;
1414
if (lib == null) return;
1515

1616
function setStatus(el, message, isError) {
@@ -21,7 +21,7 @@
2121
}
2222

2323
async function postJson(path, body) {
24-
var response = await fetch(path, {
24+
const response = await fetch(path, {
2525
method: "POST",
2626
headers: { "Content-Type": "application/json" },
2727
credentials: "same-origin",
@@ -39,13 +39,13 @@
3939
throw new Error("Session expired; redirecting to sign in.");
4040
}
4141
if (!response.ok) {
42-
var detail;
42+
let detail;
4343
try {
4444
detail = await response.json();
4545
} catch (_err) {
4646
detail = null;
4747
}
48-
var error = new Error(
48+
const error = new Error(
4949
(detail && detail.error) ||
5050
"Request failed with status " + response.status,
5151
);
@@ -55,78 +55,74 @@
5555
// 204 No Content (used by the registration-finish endpoint on success)
5656
// has an empty body and would throw if we asked for JSON.
5757
if (response.status === 204) return null;
58-
var len = response.headers.get("Content-Length");
58+
const len = response.headers.get("Content-Length");
5959
if (len === "0") return null;
6060
return response.json();
6161
}
6262

6363
function bindEnroll(form) {
6464
if (form == null) return;
65-
var status = document.getElementById("passkey-enroll-status");
66-
form.addEventListener("submit", function (event) {
65+
const status = document.getElementById("passkey-enroll-status");
66+
form.addEventListener("submit", async (event) => {
6767
event.preventDefault();
68-
var nicknameInput = form.querySelector('input[name="nickname"]');
69-
var nickname = nicknameInput ? nicknameInput.value.trim() : "";
70-
var submitButton = form.querySelector('button[type="submit"]');
68+
const nicknameInput = form.querySelector('input[name="nickname"]');
69+
const nickname = nicknameInput ? nicknameInput.value.trim() : "";
70+
const submitButton = form.querySelector('button[type="submit"]');
7171
if (submitButton) submitButton.disabled = true;
7272
setStatus(status, "Follow the prompts on your device…", false);
73-
(async function () {
74-
try {
75-
var options = await postJson("/auth/passkeys/registration/begin");
76-
var registrationResponse = await lib.startRegistration({
77-
optionsJSON: options,
78-
});
79-
await postJson("/auth/passkeys/registration/finish", {
80-
nickname: nickname,
81-
registrationResponse: registrationResponse,
82-
});
83-
setStatus(status, "Passkey added.", false);
84-
window.location.reload();
85-
} catch (err) {
86-
var name = err && err.name ? err.name : "";
87-
var message =
88-
name === "NotAllowedError"
89-
? "Enrollment was cancelled."
90-
: err && err.message
91-
? err.message
92-
: "Could not enroll a passkey.";
93-
setStatus(status, message, true);
94-
if (submitButton) submitButton.disabled = false;
95-
}
96-
})();
73+
try {
74+
const options = await postJson("/auth/passkeys/registration/begin");
75+
const registrationResponse = await lib.startRegistration({
76+
optionsJSON: options,
77+
});
78+
await postJson("/auth/passkeys/registration/finish", {
79+
nickname: nickname,
80+
registrationResponse: registrationResponse,
81+
});
82+
setStatus(status, "Passkey added.", false);
83+
window.location.reload();
84+
} catch (err) {
85+
const name = err && err.name ? err.name : "";
86+
const message =
87+
name === "NotAllowedError"
88+
? "Enrollment was cancelled."
89+
: err && err.message
90+
? err.message
91+
: "Could not enroll a passkey.";
92+
setStatus(status, message, true);
93+
if (submitButton) submitButton.disabled = false;
94+
}
9795
});
9896
}
9997

10098
function bindSignIn(button) {
10199
if (button == null) return;
102-
var status = document.getElementById("passkey-signin-status");
103-
button.addEventListener("click", function () {
104-
var next = button.getAttribute("data-next") || "";
100+
const status = document.getElementById("passkey-signin-status");
101+
button.addEventListener("click", async () => {
102+
const next = button.getAttribute("data-next") || "";
105103
button.disabled = true;
106104
setStatus(status, "Choose a passkey on your device…", false);
107-
(async function () {
108-
try {
109-
var options = await postJson("/login/passkey/begin");
110-
var authenticationResponse = await lib.startAuthentication({
111-
optionsJSON: options,
112-
});
113-
var result = await postJson("/login/passkey/finish", {
114-
next: next,
115-
authenticationResponse: authenticationResponse,
116-
});
117-
window.location.assign(result.redirect || "/");
118-
} catch (err) {
119-
var name = err && err.name ? err.name : "";
120-
var message =
121-
name === "NotAllowedError"
122-
? "Sign-in was cancelled."
123-
: err && err.message
124-
? err.message
125-
: "Could not sign in with a passkey.";
126-
setStatus(status, message, true);
127-
button.disabled = false;
128-
}
129-
})();
105+
try {
106+
const options = await postJson("/login/passkey/begin");
107+
const authenticationResponse = await lib.startAuthentication({
108+
optionsJSON: options,
109+
});
110+
const result = await postJson("/login/passkey/finish", {
111+
next: next,
112+
authenticationResponse: authenticationResponse,
113+
});
114+
window.location.assign(result.redirect || "/");
115+
} catch (err) {
116+
const name = err && err.name ? err.name : "";
117+
const message =
118+
name === "NotAllowedError"
119+
? "Sign-in was cancelled."
120+
: err && err.message
121+
? err.message
122+
: "Could not sign in with a passkey.";
123+
setStatus(status, message, true);
124+
button.disabled = false;
125+
}
130126
});
131127
}
132128

0 commit comments

Comments
 (0)