|
10 | 10 | "use strict"; |
11 | 11 |
|
12 | 12 | (function () { |
13 | | - var lib = window.SimpleWebAuthnBrowser; |
| 13 | + const lib = window.SimpleWebAuthnBrowser; |
14 | 14 | if (lib == null) return; |
15 | 15 |
|
16 | 16 | function setStatus(el, message, isError) { |
|
21 | 21 | } |
22 | 22 |
|
23 | 23 | async function postJson(path, body) { |
24 | | - var response = await fetch(path, { |
| 24 | + const response = await fetch(path, { |
25 | 25 | method: "POST", |
26 | 26 | headers: { "Content-Type": "application/json" }, |
27 | 27 | credentials: "same-origin", |
|
39 | 39 | throw new Error("Session expired; redirecting to sign in."); |
40 | 40 | } |
41 | 41 | if (!response.ok) { |
42 | | - var detail; |
| 42 | + let detail; |
43 | 43 | try { |
44 | 44 | detail = await response.json(); |
45 | 45 | } catch (_err) { |
46 | 46 | detail = null; |
47 | 47 | } |
48 | | - var error = new Error( |
| 48 | + const error = new Error( |
49 | 49 | (detail && detail.error) || |
50 | 50 | "Request failed with status " + response.status, |
51 | 51 | ); |
|
55 | 55 | // 204 No Content (used by the registration-finish endpoint on success) |
56 | 56 | // has an empty body and would throw if we asked for JSON. |
57 | 57 | if (response.status === 204) return null; |
58 | | - var len = response.headers.get("Content-Length"); |
| 58 | + const len = response.headers.get("Content-Length"); |
59 | 59 | if (len === "0") return null; |
60 | 60 | return response.json(); |
61 | 61 | } |
62 | 62 |
|
63 | 63 | function bindEnroll(form) { |
64 | 64 | 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) => { |
67 | 67 | 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"]'); |
71 | 71 | if (submitButton) submitButton.disabled = true; |
72 | 72 | 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 | + } |
97 | 95 | }); |
98 | 96 | } |
99 | 97 |
|
100 | 98 | function bindSignIn(button) { |
101 | 99 | 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") || ""; |
105 | 103 | button.disabled = true; |
106 | 104 | 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 | + } |
130 | 126 | }); |
131 | 127 | } |
132 | 128 |
|
|
0 commit comments