Repository navigation
Expand file tree
/
Copy pathverify-code.js
More file actions
204 lines (189 loc) · 7.64 KB
/
Copy pathverify-code.js
File metadata and controls
204 lines (189 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Example only: what an app's script does for Verify Code, as the React
// pattern does. The code's field is OTP Input's, from otp-input.js. Once the
// code is whole, typed, pasted, or filled in from a message, the form sends
// itself. Sending checks the code first: an error goes under the field, with
// aria-invalid on its input, the field takes focus, and the form shakes once,
// unless the reader asked for reduced motion. Then the button shows it's
// working while the app checks the code, here a stand-in that takes a moment
// and accepts only 123456, and fails, as a request that can't reach the
// server does, for 000000. A code it turns down puts its message under the
// field, which takes focus with the code selected, so typing replaces it.
// The right code, your app would go on to its next page, which says so; here
// a toast does, from toast.js: the form's
// data-example-verify-code-success, or "Code accepted".
// "Send a new code" waits, counting down, 30 seconds or the form's
// data-example-verify-code-resend-after, then sends one, here always, says
// so where the address is, and waits again. A form a script adds later, such
// as the Sign-in Form's step that asks for a code, is set up as it arrives;
// its way back is that script's.
{
const somethingWrong = "Something went wrong. Try again.";
// Answers after a moment, as a request to a server would.
const answer = (result) =>
new Promise((resolve, reject) =>
setTimeout(() => {
try {
resolve(result());
} catch (error) {
reject(error);
}
}, 800),
);
const verify = (data) =>
answer(() => {
if (data.get("code") === "000000") throw new Error("The server can't be reached.");
return data.get("code") === "123456" ? undefined : { code: "That code isn't right. Check it, or send a new one." };
});
const resend = () => answer(() => ({ sent: true }));
// The message an answer has for the form: a message, or any message by field.
const messageOf = (result) =>
typeof result === "string"
? result
: result && typeof result === "object" && !("sent" in result)
? Object.values(result)
.filter((message) => typeof message === "string" && message)
.join(" ")
: "";
const clock = (seconds) => `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
const working = (button, busy) => {
if (busy) {
const spinner = document.createElement("span");
spinner.className = "hspinner";
spinner.setAttribute("aria-hidden", "true");
button.append(spinner);
button.setAttribute("aria-busy", "true");
button.setAttribute("aria-disabled", "true");
} else {
button.querySelector(":scope > .hspinner")?.remove();
button.removeAttribute("aria-busy");
button.removeAttribute("aria-disabled");
}
};
const setUp = (form) => {
const field = form.elements.namedItem("code");
const error = document.getElementById(`${field.id}-error`);
const description = form.querySelector(".hauth-form-header > .hauth-form-description");
const button = form.querySelector(":scope > .hbutton");
const again = form.querySelector(".hverify-code-resend > .hauth-form-action");
const wait = Number(form.dataset.exampleVerifyCodeResendAfter ?? 30);
const letters = field.closest(".hotp-input").dataset.characters === "alphanumeric";
let busy = false;
let last = field.value;
let timer;
const show = (message) => {
error.textContent = message ?? "";
if (message) field.setAttribute("aria-invalid", "true");
else field.removeAttribute("aria-invalid");
};
const fail = (message) => {
show(message);
form.setAttribute("data-shake", "");
field.focus();
field.select();
};
// "Send a new code in 0:24", then "Send a new code", updated as each
// second passes.
const countDown = (until) => {
clearTimeout(timer);
const tick = () => {
const remaining = until - Date.now();
const left = remaining > 0 ? Math.ceil(remaining / 1000) : 0;
again.textContent = left > 0 ? `Send a new code in ${clock(left)}` : "Send a new code";
if (left > 0) again.setAttribute("aria-disabled", "true");
else again.removeAttribute("aria-disabled");
if (left > 0) timer = setTimeout(tick, remaining - (left - 1) * 1000);
};
tick();
};
if (again) countDown(Date.now() + wait * 1000);
form.addEventListener("animationend", (event) => {
if (event.target === form) form.removeAttribute("data-shake");
});
// After OTP Input's script has cleaned what was typed.
form.addEventListener("input", (event) => {
if (event.target !== field) return;
if (error.textContent) show(undefined);
if (field.value.length === field.maxLength && field.value !== last) form.requestSubmit();
last = field.value;
});
form.addEventListener("submit", async (event) => {
event.preventDefault();
if (busy) return;
const code = field.value;
const size = field.maxLength;
const missing = !code
? "Enter the code."
: code.length < size
? `Enter all ${size} ${letters ? "characters" : "digits"}.`
: undefined;
if (missing) {
show(missing);
form.setAttribute("data-shake", "");
field.focus();
return;
}
show(undefined);
busy = true;
working(button, true);
let result;
try {
result = await verify(new FormData(form));
} catch {
result = somethingWrong;
}
working(button, false);
busy = false;
const message = messageOf(result);
if (message) return fail(message);
document.dispatchEvent(
new CustomEvent("example:toast", {
detail: { status: "success", title: form.dataset.exampleVerifyCodeSuccess ?? "Code accepted" },
}),
);
});
again?.addEventListener("click", async (event) => {
event.preventDefault();
if (busy || again.getAttribute("aria-disabled") === "true") return;
busy = true;
show(undefined);
again.setAttribute("aria-disabled", "true");
again.setAttribute("aria-busy", "true");
let result;
try {
result = await resend(new FormData(form, again));
} catch {
result = somethingWrong;
}
busy = false;
again.removeAttribute("aria-busy");
const message = messageOf(result);
if (message) {
again.removeAttribute("aria-disabled");
return fail(message);
}
// An answer of nothing changes nothing.
if (typeof result !== "object" || result === null) {
again.removeAttribute("aria-disabled");
return;
}
// The address goes back in as it was, an element, never as markup.
const address = description.querySelector(".hauth-form-email");
if (address) description.replaceChildren("We've sent a new code to ", address, ".");
countDown(Date.now() + wait * 1000);
});
};
// Each form on the page, and each one a script adds later.
const ready = new WeakSet();
const setUpIn = (node) => {
if (!(node instanceof Element)) return;
for (const form of [node, ...node.querySelectorAll(".hauth-form[data-example-verify-code]")]) {
if (!form.matches(".hauth-form[data-example-verify-code]") || ready.has(form)) continue;
ready.add(form);
setUp(form);
}
};
setUpIn(document.documentElement);
new MutationObserver((records) => {
for (const record of records) for (const node of record.addedNodes) setUpIn(node);
}).observe(document.documentElement, { childList: true, subtree: true });
}