@ofidj/generator-fidj 3.10.2 → 3.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,203 +0,0 @@
1
- export function agreementMarkup() {
2
- return `<div class="signin-agreement"><label class="agreement-choice"><input id="service-agreement" type="checkbox" aria-required="true" disabled><span id="agreement-label">I accept the service agreement for this app.</span></label><button type="button" id="read-agreement" disabled>Read service agreement</button><p id="agreement-status" class="fineprint" role="status">Loading service agreement…</p><button type="button" id="retry-agreement" hidden>Retry</button></div><dialog id="agreement-dialog" aria-labelledby="agreement-heading"><h2 id="agreement-heading">Service agreement</h2><p id="agreement-version"></p><p id="agreement-text"></p><button type="button" id="close-agreement">Close agreement</button></dialog>`;
3
- }
4
-
5
- export function acceptedAgreement(form: HTMLFormElement) {
6
- const checkbox = form.querySelector<HTMLInputElement>("#service-agreement");
7
- if (!checkbox?.checked || checkbox.disabled || !checkbox.dataset.version) return null;
8
- return { termsAccepted: true, termsVersion: checkbox.dataset.version };
9
- }
10
-
11
- export function signInErrorMessage(error: unknown) {
12
- const detail = error as {code?: number; reason?: unknown; message?: unknown};
13
- const reason = typeof detail?.reason === "string" ? detail.reason : typeof detail?.message === "string" ? detail.message : "";
14
- if (detail?.code === 429) return "Too many attempts. Please wait before trying again.";
15
- if (reason === "unknown-user") return "We could not sign in to this account. Check the email and password.";
16
- if (reason === "already exists - inconsistent request") return "An account already uses this email. Check the password or sign in instead.";
17
- if (/ECONNREFUSED|ENOTFOUND|ETIMEDOUT|ECONNRESET|EHOSTUNREACH|network/i.test(reason)) return "We cannot reach Fidj right now. Please try again.";
18
- return "We could not sign in to this account. Please try again.";
19
- }
20
-
21
- export async function bindAgreement(form: HTMLFormElement | null, title: string, endpoint: string, appId: string, checked = false) {
22
- if (!form) return;
23
- // An app whose only door is Fidj carries no agreement block: Fidj asks for
24
- // this app's agreement on the screen that names it, and records the version.
25
- const checkbox = form.querySelector<HTMLInputElement>("#service-agreement");
26
- if (!checkbox) return;
27
- const read = form.querySelector<HTMLButtonElement>("#read-agreement")!;
28
- const dialog = form.querySelector<HTMLDialogElement>("#agreement-dialog")!;
29
- const status = form.querySelector<HTMLElement>("#agreement-status")!;
30
- const retry = form.querySelector<HTMLButtonElement>("#retry-agreement")!;
31
- // What this agreement gates is the app's own door, and only that one. The
32
- // Fidj door is about to be asked the same question on the screen that names
33
- // the app, where the answer is recorded with its version — so an agreement
34
- // that is still loading, or that could not be loaded at all, must not be what
35
- // stands between a person and the door that does not need it.
36
- const gated = form.querySelector("#email-entry") || form;
37
- const submitButtons = gated.querySelectorAll<HTMLButtonElement>('button[type="submit"]');
38
- const update = () => submitButtons.forEach(button => { button.disabled = checkbox.disabled; });
39
- form.querySelector("#agreement-label")!.textContent = `I accept the service agreement for ${title}.`;
40
- update();
41
- checkbox.addEventListener("change", update);
42
- read.addEventListener("click", () => dialog.showModal());
43
- form.querySelector("#close-agreement")!.addEventListener("click", () => dialog.close());
44
- const load = async () => {
45
- retry.hidden = true;
46
- status.textContent = "Loading service agreement…";
47
- try {
48
- const response = await fetch(`${endpoint}/apps/${encodeURIComponent(appId)}`, {signal: AbortSignal.timeout(10000)});
49
- if (!response.ok) throw new Error(response.status === 404 ? "missing" : "unreachable");
50
- const agreement = (await response.json()).app?.agreement;
51
- if (!agreement || typeof agreement.version !== "string" || !agreement.version || typeof agreement.text !== "string" || !agreement.text) throw new Error("missing");
52
- if (!form.isConnected) return;
53
- checkbox.dataset.version = agreement.version;
54
- checkbox.disabled = false;
55
- checkbox.checked = checked;
56
- read.disabled = false;
57
- dialog.querySelector("#agreement-version")!.textContent = `Version ${agreement.version}`;
58
- dialog.querySelector("#agreement-text")!.textContent = agreement.text;
59
- status.textContent = "Required to sign in. Optional data choices stay separate.";
60
- update();
61
- } catch (error) {
62
- if (!form.isConnected) return;
63
- status.textContent = error instanceof Error && error.message === "missing"
64
- ? "This app has no service agreement available."
65
- : "We cannot reach Fidj right now. Try again.";
66
- retry.hidden = false;
67
- }
68
- };
69
- retry.addEventListener("click", load);
70
- await load();
71
- }
72
-
73
- // The entry every app that delegates to the provider renders, in one place
74
- // because both app shapes render it and they had drifted apart.
75
- //
76
- // "Continue with Fidj" alone borrows the grammar of an optional social login —
77
- // that button always sits next to an email and a password — so on an app whose
78
- // accounts *are* Fidj accounts, the missing form reads as something broken. The
79
- // explanation therefore comes before the button, not as reassurance after it,
80
- // and nothing presupposes an account the person may not have yet.
81
- const hintKey = (appId: string) => "fidj.entry." + appId;
82
-
83
- export function signInHint(appId: string) {
84
- try {
85
- return localStorage.getItem(hintKey(appId)) || "";
86
- } catch {
87
- return "";
88
- }
89
- }
90
-
91
- // Remembered on this app's own origin, about this app's own member: no
92
- // cross-site question is asked, and none is answered. Signing out forgets, so a
93
- // shared browser does not show the next person an address.
94
- export function rememberSignIn(appId: string, label: string) {
95
- try {
96
- if (label) localStorage.setItem(hintKey(appId), label);
97
- } catch {}
98
- }
99
-
100
- export function forgetSignIn(appId: string) {
101
- try {
102
- localStorage.removeItem(hintKey(appId));
103
- } catch {}
104
- }
105
-
106
- // Both doors, but not side by side. An app that delegates to Fidj still has
107
- // people who would rather type an address and a password than be sent
108
- // somewhere, so the credential form stays — folded away under a line of text
109
- // rather than standing in front of the door most people should take. Offering
110
- // the two as equals asked every arrival to choose between them, and the one
111
- // that looked like an ordinary login won by looking ordinary.
112
- //
113
- // The trade-off is stated where it is made: on the credential path the app's
114
- // own page handles the Fidj password, so it is the app — not only Fidj — that
115
- // must be trusted with it. The Fidj entry above it never is, which is why it is
116
- // the one wearing the accent.
117
- export type SigninShape = "button" | "inline" | "both";
118
-
119
- export function providerEntry(
120
- title: string,
121
- appId: string,
122
- credentials: string,
123
- isFidjItself = false,
124
- shape: SigninShape = "button",
125
- ) {
126
- const escapeText = (value: unknown) =>
127
- String(value ?? "").replace(
128
- /[&<>"']/g,
129
- (char) =>
130
- ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[
131
- char
132
- ]!,
133
- );
134
- const hint = signInHint(appId);
135
- const both = shape === "both" && Boolean(credentials);
136
- // The app's own form and nothing else. Nothing is folded away because there is
137
- // no second way in to fold it under, and the trade-off is stated plainly:
138
- // on this path it is this site that holds the password.
139
- if (shape === "inline")
140
- return (
141
- `<p class="signin-lead">${
142
- isFidjItself
143
- ? "One account across every app that uses Fidj, and a separate set of choices for each one."
144
- : `${escapeText(title)} accounts are Fidj accounts. Sign in below — ${escapeText(title)} handles your password itself on this page.`
145
- }</p>` +
146
- agreementMarkup() +
147
- credentials
148
- );
149
- const lead = hint
150
- ? `<p class="signin-lead">You signed in here with Fidj before. ${escapeText(title)} accounts are Fidj accounts — continue as yourself, or use another.</p>`
151
- : both
152
- ? `<p class="signin-lead">${escapeText(title)} accounts are Fidj accounts. Fidj asks in a window of its own, so this site never sees your password.</p>`
153
- : isFidjItself
154
- ? `<p class="signin-lead">One account across every app that uses Fidj, and a separate set of choices for each one. Fidj asks in a window of its own; this page stays where it is.</p>`
155
- : `<p class="signin-lead">${escapeText(title)} accounts are Fidj accounts. You will sign in — or create yours — in a Fidj window, so this site never sees your password.</p>`;
156
- // The accent is the Fidj mark, and this is the one control on the screen that
157
- // is Fidj's rather than the app's. It is also the door the app would rather
158
- // people took, and those two happen to want the same thing.
159
- const fidj = hint
160
- ? `<button class="primary fidj-entry" type="submit" name="entry" value="fidj">Continue as ${escapeText(hint)}</button><button type="button" id="forget-hint" class="quiet">Use a different account</button>`
161
- : `<button class="primary fidj-entry" type="submit" name="entry" value="fidj">${isFidjItself ? "Sign in" : "Sign in with Fidj"}</button>`;
162
- // Fidj is the only door: it collects the agreement itself, a moment later, on
163
- // the screen that names the app — and records it with its version. Collecting
164
- // it here first recorded nothing and asked the same question twice.
165
- if (shape !== "both" || !both) return lead + fidj;
166
- // Both doors. The app's own one is a second thought for the person who wants
167
- // it, which is what a disclosure is for: it costs one click and takes nothing
168
- // away, where a second button of equal weight cost everybody a decision.
169
- return (
170
- lead +
171
- fidj +
172
- `<div class="signin-alternate"><button type="button" id="use-email" class="signin-toggle" aria-expanded="false" aria-controls="email-entry"><span class="signin-toggle-label">Inline form<span class="caret" aria-hidden="true"></span></span></button>
173
- <div id="email-entry" hidden>${agreementMarkup()}${credentials}</div></div>`
174
- );
175
- }
176
-
177
- // Opening the app's own form takes the space the Fidj door was using.
178
- //
179
- // The two are alternatives, not a list, and watching one fold away as the other
180
- // arrives is what says so — where a form appearing underneath a button that is
181
- // still there reads as "and also". The toggle stays put through both states, so
182
- // it ends up labelling whichever one is on screen: a caret pointing down at a
183
- // form that is not here yet, and up at the one it would put away.
184
- //
185
- // Shared, because both app shapes render the same entry.
186
- export function showEmailEntry(open: boolean, focus = false) {
187
- const fields = document.getElementById("email-entry");
188
- const toggle = document.getElementById("use-email");
189
- if (!fields || !toggle) return;
190
- toggle.setAttribute("aria-expanded", String(open));
191
- fields.hidden = !open;
192
- const door = document.querySelector<HTMLButtonElement>(".fidj-entry");
193
- if (door) {
194
- door.classList.toggle("is-folded", open);
195
- // Out of the tab order and out of the accessibility tree the moment it
196
- // starts leaving: something mid-fold is not something to press, and an
197
- // animation is not what decides that.
198
- if (open) door.setAttribute("aria-hidden", "true");
199
- else door.removeAttribute("aria-hidden");
200
- door.tabIndex = open ? -1 : 0;
201
- }
202
- if (open && focus) document.getElementById("email")?.focus();
203
- }