ferst-core 0.17.1 → 0.19.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.
- package/bin/ferst-modules.mjs +8 -13
- package/components/BlockRenderer.astro +0 -2
- package/components/islands/MembersAccount.tsx +501 -395
- package/content/blocks.ts +2 -19
- package/content/cookies.ts +2 -3
- package/content/recipe-questions.ts +0 -1
- package/examples/recipes.ts +0 -1
- package/lib/members-account.ts +352 -283
- package/lib/members.ts +88 -134
- package/package.json +1 -1
- package/components/recipes/MembersContent.astro +0 -176
|
@@ -1,395 +1,501 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The members' account page (`/account`, the Ferst Members module): a React island
|
|
3
|
-
* (docs/BLOCK-ARCHITECTURE.md §8), drawn in the browser only, since all of it depends on who the
|
|
4
|
-
* visitor is.
|
|
5
|
-
*
|
|
6
|
-
* Signed out, it is the sign-in card: an email, the organisation's terms box when it has terms, then
|
|
7
|
-
* the code sent to that email
|
|
8
|
-
* member
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type
|
|
28
|
-
type
|
|
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
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const [
|
|
64
|
-
const [
|
|
65
|
-
const [
|
|
66
|
-
const [
|
|
67
|
-
const [
|
|
68
|
-
const [
|
|
69
|
-
const [
|
|
70
|
-
const [
|
|
71
|
-
const [
|
|
72
|
-
const [
|
|
73
|
-
const [
|
|
74
|
-
const [
|
|
75
|
-
const [
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const
|
|
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
|
-
if (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (out.kind === '
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
{
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
{
|
|
356
|
-
</
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The members' account page (`/account`, the Ferst Members module): a React island
|
|
3
|
+
* (docs/BLOCK-ARCHITECTURE.md §8), drawn in the browser only, since all of it depends on who the
|
|
4
|
+
* visitor is.
|
|
5
|
+
*
|
|
6
|
+
* Signed out, it is the sign-in card: an email, the organisation's terms box when it has terms, then
|
|
7
|
+
* the code sent to that email; a new person's first code makes them a member when the organisation lets
|
|
8
|
+
* them join. A member who has set a password may sign in with it instead (0.19.0), and sets or changes
|
|
9
|
+
* it on this page, freshly signed in; a forgotten one is a code sign-in and a new password. Signed in, it shows who they are and where they stand
|
|
10
|
+
* (a member, waiting for approval, or a guest who booked and may join), their name, their password,
|
|
11
|
+
* signing out, and deleting their account. `?next=` brings them back to the page that sent them, on this site only.
|
|
12
|
+
*
|
|
13
|
+
* Every call is the site's own `/ferst-api/members/…` (same-origin, the session a first-party
|
|
14
|
+
* cookie); lib/members-account reads every answer and holds the words. React Aria gives the fields,
|
|
15
|
+
* the tick box and the buttons their keyboard and screen-reader behaviour. It brings no look of its
|
|
16
|
+
* own: the page's CSS (components/MembersAccount.astro) styles it from the site's tokens.
|
|
17
|
+
*/
|
|
18
|
+
import { useEffect, useRef, useState, type FormEvent, type ReactNode } from 'react';
|
|
19
|
+
import { Button, Checkbox, Form, Input, Label, Text, TextField } from 'react-aria-components';
|
|
20
|
+
import { MEMBERS_ME, accountLinkText, isLocalHost, readMembersAnswer, rememberAccountLabel, type MembersAnswer } from '../../lib/members';
|
|
21
|
+
import {
|
|
22
|
+
ACCOUNT_TEXT as T, MEMBERS_JOIN, MEMBERS_PASSWORD, MEMBERS_PASSWORD_SIGN_IN, MEMBERS_SIGN_IN, MEMBERS_SIGN_OUT, MEMBERS_START, MEMBERS_VERIFY,
|
|
23
|
+
PASSWORD_MAX, canJoin, cleanCode, cleanName, hasTerms, looksLikeEmail, passwordProblem, readCard, readDone, readJoin, readMe,
|
|
24
|
+
readPasswordSignIn, readRename, readSetPassword, readStart, readVerify, safeNext, type Joining, type Member, type SignInCard,
|
|
25
|
+
} from '../../lib/members-account';
|
|
26
|
+
|
|
27
|
+
type Screen = 'loading' | 'local' | 'unavailable' | 'email' | 'password' | 'code' | 'account';
|
|
28
|
+
type Notice = { tone: 'info' | 'done' | 'error'; text: string } | null;
|
|
29
|
+
type Errors = Partial<Record<'email' | 'agree' | 'code' | 'name' | 'confirm' | 'password' | 'newPassword', string>>;
|
|
30
|
+
interface Me { member: Member; org: string; joining: Joining }
|
|
31
|
+
|
|
32
|
+
/** One call to the members service, as the site's own address; null when the network failed. */
|
|
33
|
+
async function call(method: string, url: string, body?: unknown): Promise<MembersAnswer | null> {
|
|
34
|
+
try {
|
|
35
|
+
const res = await fetch(url, {
|
|
36
|
+
method,
|
|
37
|
+
credentials: 'same-origin',
|
|
38
|
+
headers: { Accept: 'application/json', ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) },
|
|
39
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
40
|
+
});
|
|
41
|
+
return await readMembersAnswer(res);
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The header's account link follows along: the member's first name, or "Sign in". */
|
|
48
|
+
function tellHeader(member: Member | null) {
|
|
49
|
+
const text = accountLinkText(member ? { status: 200, body: { member: { display_name: member.displayName } } } : { status: 401, body: {} });
|
|
50
|
+
if (!text) return;
|
|
51
|
+
document.querySelectorAll('[data-members-account]').forEach((a) => { a.textContent = text; });
|
|
52
|
+
rememberAccountLabel(text);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A field's error, read out as it appears and tied to the field (React Aria's errorMessage slot). */
|
|
56
|
+
const FieldError = ({ text }: { text?: string }) =>
|
|
57
|
+
text ? <Text slot="errorMessage" className="m-account__error" role="alert">{text}</Text> : null;
|
|
58
|
+
|
|
59
|
+
export default function MembersAccount() {
|
|
60
|
+
const local = useRef(import.meta.env.DEV || isLocalHost(location.hostname)).current;
|
|
61
|
+
const next = useRef(safeNext(new URLSearchParams(location.search).get('next'))).current;
|
|
62
|
+
|
|
63
|
+
const [screen, setScreen] = useState<Screen>('loading');
|
|
64
|
+
const [card, setCard] = useState<SignInCard | null>(null);
|
|
65
|
+
const [me, setMe] = useState<Me | null>(null);
|
|
66
|
+
const [email, setEmail] = useState('');
|
|
67
|
+
const [agree, setAgree] = useState(false);
|
|
68
|
+
const [signinId, setSigninId] = useState('');
|
|
69
|
+
const [code, setCode] = useState('');
|
|
70
|
+
const [name, setName] = useState('');
|
|
71
|
+
const [password, setPassword] = useState('');
|
|
72
|
+
const [newPassword, setNewPassword] = useState('');
|
|
73
|
+
const [confirm, setConfirm] = useState('');
|
|
74
|
+
const [deleting, setDeleting] = useState(false);
|
|
75
|
+
const [welcome, setWelcome] = useState<string | null>(null);
|
|
76
|
+
const [notice, setNotice] = useState<Notice>(null);
|
|
77
|
+
const [errors, setErrors] = useState<Errors>({});
|
|
78
|
+
const [busy, setBusy] = useState<string | null>(null);
|
|
79
|
+
|
|
80
|
+
const heading = useRef<HTMLHeadingElement>(null);
|
|
81
|
+
const codeField = useRef<HTMLInputElement>(null);
|
|
82
|
+
const emailField = useRef<HTMLInputElement>(null);
|
|
83
|
+
const passwordField = useRef<HTMLInputElement>(null);
|
|
84
|
+
const newPasswordField = useRef<HTMLInputElement>(null);
|
|
85
|
+
/** Moving focus follows the visitor's own actions, never the first load. */
|
|
86
|
+
const moved = useRef(false);
|
|
87
|
+
|
|
88
|
+
useEffect(() => { void load(); }, []);
|
|
89
|
+
|
|
90
|
+
// A new step: the code field when a code is on its way, else the step's heading.
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (!moved.current) return;
|
|
93
|
+
if (screen === 'code') codeField.current?.focus();
|
|
94
|
+
else heading.current?.focus();
|
|
95
|
+
}, [screen]);
|
|
96
|
+
|
|
97
|
+
async function load() {
|
|
98
|
+
const v = readMe(await call('GET', MEMBERS_ME), local);
|
|
99
|
+
if (v.kind === 'member') {
|
|
100
|
+
showAccount(v);
|
|
101
|
+
// A guest may join: the card says whether the organisation has terms to agree to.
|
|
102
|
+
if (canJoin(v.member, v.joining)) {
|
|
103
|
+
const c = readCard(await call('GET', MEMBERS_SIGN_IN), local);
|
|
104
|
+
if (c.kind === 'card') setCard(c.card);
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (v.kind === 'signed-out') return showCard(null);
|
|
109
|
+
setScreen(v.kind);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function showAccount(m: Me) {
|
|
113
|
+
setMe(m);
|
|
114
|
+
setName(m.member.displayName ?? '');
|
|
115
|
+
setErrors({});
|
|
116
|
+
setScreen('account');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Back to the sign-in card, with what just happened (signed out, deleted). */
|
|
120
|
+
async function showCard(said: Notice) {
|
|
121
|
+
const v = readCard(await call('GET', MEMBERS_SIGN_IN), local);
|
|
122
|
+
setMe(null);
|
|
123
|
+
setWelcome(null);
|
|
124
|
+
setDeleting(false);
|
|
125
|
+
setConfirm('');
|
|
126
|
+
setCode('');
|
|
127
|
+
setPassword('');
|
|
128
|
+
setNewPassword('');
|
|
129
|
+
setAgree(false);
|
|
130
|
+
setErrors({});
|
|
131
|
+
if (v.kind !== 'card') return setScreen(v.kind);
|
|
132
|
+
setCard(v.card);
|
|
133
|
+
setNotice(said);
|
|
134
|
+
setScreen('email');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function act<R>(what: string, run: () => Promise<R>) {
|
|
138
|
+
moved.current = true;
|
|
139
|
+
setBusy(what);
|
|
140
|
+
return run().finally(() => setBusy(null));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function startSignIn(again = false) {
|
|
144
|
+
const id = readStart(await call('POST', MEMBERS_START, { email: email.trim(), ...(hasTerms(card) ? { agree } : {}) }));
|
|
145
|
+
if (!id) return setNotice({ tone: 'error', text: T.tryAgain });
|
|
146
|
+
setSigninId(id);
|
|
147
|
+
setCode('');
|
|
148
|
+
setErrors({});
|
|
149
|
+
setNotice(again ? { tone: 'info', text: T.newCodeSent } : null);
|
|
150
|
+
if (again) codeField.current?.focus();
|
|
151
|
+
setScreen('code');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function onEmail(e: FormEvent) {
|
|
155
|
+
e.preventDefault();
|
|
156
|
+
const found: Errors = {};
|
|
157
|
+
if (!looksLikeEmail(email)) found.email = T.emailInvalid;
|
|
158
|
+
if (hasTerms(card) && !agree) found.agree = T.agreeNeeded;
|
|
159
|
+
setErrors(found);
|
|
160
|
+
if (found.email) return emailField.current?.focus();
|
|
161
|
+
if (found.agree) return;
|
|
162
|
+
void act('start', () => startSignIn());
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function onCode(e: FormEvent) {
|
|
166
|
+
e.preventDefault();
|
|
167
|
+
const c = cleanCode(code);
|
|
168
|
+
if (!c) {
|
|
169
|
+
setErrors({ code: T.codeInvalid });
|
|
170
|
+
return codeField.current?.focus();
|
|
171
|
+
}
|
|
172
|
+
void act('verify', async () => {
|
|
173
|
+
const out = readVerify(await call('POST', MEMBERS_VERIFY, { signin_id: signinId, code: c }));
|
|
174
|
+
setNotice(null);
|
|
175
|
+
if (out.kind === 'signed-in') return signedInNow(out.member, out.welcome);
|
|
176
|
+
if (out.kind === 'new-code') {
|
|
177
|
+
setCode('');
|
|
178
|
+
setErrors({});
|
|
179
|
+
setNotice({ tone: 'info', text: T.newCodeToFinish });
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (out.kind === 'try-again') {
|
|
183
|
+
setErrors({});
|
|
184
|
+
setNotice({ tone: 'error', text: T.tryAgain });
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
setErrors({ code: out.kind === 'wrong-code' ? T.wrongCode(out.triesLeft) : T.expired });
|
|
188
|
+
codeField.current?.select();
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Signed in: straight back to the page that sent them, unless there's a welcome to read first. */
|
|
193
|
+
function signedInNow(member: Member, welcomeText: string | null): Promise<void> | void {
|
|
194
|
+
tellHeader(member);
|
|
195
|
+
setPassword('');
|
|
196
|
+
if (next && member.status === 'active' && !welcomeText) {
|
|
197
|
+
location.assign(next);
|
|
198
|
+
return new Promise<void>(() => {}); // stay busy while the page changes
|
|
199
|
+
}
|
|
200
|
+
setWelcome(welcomeText);
|
|
201
|
+
return showAccount({ member, org: card?.org ?? '', joining: card?.joining ?? 'open' });
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Between "email me a code" and "sign in with your password", keeping the email typed. */
|
|
205
|
+
function switchTo(to: 'email' | 'password') {
|
|
206
|
+
moved.current = true;
|
|
207
|
+
setErrors({});
|
|
208
|
+
setNotice(null);
|
|
209
|
+
setPassword('');
|
|
210
|
+
setScreen(to);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function onPassword(e: FormEvent) {
|
|
214
|
+
e.preventDefault();
|
|
215
|
+
const found: Errors = {};
|
|
216
|
+
if (!looksLikeEmail(email)) found.email = T.emailInvalid;
|
|
217
|
+
if (!password) found.password = T.passwordNeeded;
|
|
218
|
+
setErrors(found);
|
|
219
|
+
if (found.email) return emailField.current?.focus();
|
|
220
|
+
if (found.password) return passwordField.current?.focus();
|
|
221
|
+
void act('password', async () => {
|
|
222
|
+
const out = readPasswordSignIn(await call('POST', MEMBERS_PASSWORD_SIGN_IN, { email: email.trim(), password }));
|
|
223
|
+
setNotice(null);
|
|
224
|
+
if (out.kind === 'signed-in') return signedInNow(out.member, null);
|
|
225
|
+
if (out.kind === 'wrong') {
|
|
226
|
+
setErrors({ password: T.wrongPassword });
|
|
227
|
+
return passwordField.current?.select();
|
|
228
|
+
}
|
|
229
|
+
setNotice({ tone: 'error', text: T.tryAgain });
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function onNewPassword(e: FormEvent) {
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
const problem = passwordProblem(newPassword);
|
|
236
|
+
if (problem) {
|
|
237
|
+
setErrors({ newPassword: problem });
|
|
238
|
+
return newPasswordField.current?.focus();
|
|
239
|
+
}
|
|
240
|
+
void act('new-password', async () => {
|
|
241
|
+
const out = readSetPassword(await call('PUT', MEMBERS_PASSWORD, { password: newPassword }));
|
|
242
|
+
if (out.kind === 'saved' && me) {
|
|
243
|
+
setMe({ ...me, member: out.member });
|
|
244
|
+
setNewPassword('');
|
|
245
|
+
setErrors({});
|
|
246
|
+
return setNotice({ tone: 'done', text: T.passwordSaved });
|
|
247
|
+
}
|
|
248
|
+
if (out.kind === 'signed-out') return showCard({ tone: 'info', text: T.signInAgain });
|
|
249
|
+
if (out.kind === 'weak') {
|
|
250
|
+
setErrors({ newPassword: out.message });
|
|
251
|
+
return newPasswordField.current?.focus();
|
|
252
|
+
}
|
|
253
|
+
setErrors({});
|
|
254
|
+
setNotice({ tone: 'error', text: out.kind === 'sign-in-again' ? T.passwordFresh : T.tryAgain });
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function otherEmail() {
|
|
259
|
+
moved.current = true;
|
|
260
|
+
setErrors({});
|
|
261
|
+
setNotice(null);
|
|
262
|
+
setCode('');
|
|
263
|
+
setScreen('email');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function onJoin(e: FormEvent) {
|
|
267
|
+
e.preventDefault();
|
|
268
|
+
if (!me) return;
|
|
269
|
+
if (hasTerms(card) && !agree) return setErrors({ agree: T.agreeNeeded });
|
|
270
|
+
void act('join', async () => {
|
|
271
|
+
const out = readJoin(await call('POST', MEMBERS_JOIN, hasTerms(card) ? { agree } : {}));
|
|
272
|
+
if (out.kind === 'joined') {
|
|
273
|
+
setWelcome(out.welcome);
|
|
274
|
+
setNotice(null);
|
|
275
|
+
return showAccount({ ...me, member: out.member });
|
|
276
|
+
}
|
|
277
|
+
if (out.kind === 'signed-out') return showCard({ tone: 'info', text: T.signInAgain });
|
|
278
|
+
if (out.kind === 'agree') return setErrors({ agree: T.agreeNeeded });
|
|
279
|
+
setNotice({ tone: 'error', text: out.kind === 'invite-only' ? T.inviteOnly : out.kind === 'paused' ? T.paused(me.org) : T.tryAgain });
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function onName(e: FormEvent) {
|
|
284
|
+
e.preventDefault();
|
|
285
|
+
const clean = cleanName(name);
|
|
286
|
+
if (!clean) return setErrors({ name: T.nameInvalid });
|
|
287
|
+
void act('name', async () => {
|
|
288
|
+
const out = readRename(await call('PATCH', MEMBERS_ME, { display_name: clean }));
|
|
289
|
+
if (out.kind === 'saved' && me) {
|
|
290
|
+
tellHeader(out.member);
|
|
291
|
+
setMe({ ...me, member: out.member });
|
|
292
|
+
setErrors({});
|
|
293
|
+
return setNotice({ tone: 'done', text: T.nameSaved });
|
|
294
|
+
}
|
|
295
|
+
if (out.kind === 'signed-out') return showCard({ tone: 'info', text: T.signInAgain });
|
|
296
|
+
if (out.kind === 'invalid') return setErrors({ name: T.nameInvalid });
|
|
297
|
+
setNotice({ tone: 'error', text: T.tryAgain });
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function signOut() {
|
|
302
|
+
void act('sign-out', async () => {
|
|
303
|
+
const out = readDone(await call('POST', MEMBERS_SIGN_OUT));
|
|
304
|
+
if (out === 'try-again') return setNotice({ tone: 'error', text: T.tryAgain });
|
|
305
|
+
tellHeader(null);
|
|
306
|
+
await showCard({ tone: 'done', text: T.signedOut });
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function onDelete(e: FormEvent) {
|
|
311
|
+
e.preventDefault();
|
|
312
|
+
if (confirm.trim().toLowerCase() !== 'delete') return;
|
|
313
|
+
void act('delete', async () => {
|
|
314
|
+
const out = readDone(await call('DELETE', MEMBERS_ME, { confirm: 'delete' }));
|
|
315
|
+
if (out === 'try-again') return setNotice({ tone: 'error', text: T.deleteLater });
|
|
316
|
+
tellHeader(null);
|
|
317
|
+
await showCard(out === 'done' ? { tone: 'done', text: T.deleted } : { tone: 'info', text: T.signInAgain });
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const agreeBox = card && hasTerms(card) && (
|
|
322
|
+
<div className="m-account__agree">
|
|
323
|
+
<Checkbox
|
|
324
|
+
className="m-account__check"
|
|
325
|
+
isSelected={agree}
|
|
326
|
+
onChange={(v) => { setAgree(v); setErrors((x) => ({ ...x, agree: undefined })); }}
|
|
327
|
+
isInvalid={Boolean(errors.agree)}
|
|
328
|
+
aria-describedby={errors.agree ? 'm-account-agree-error' : undefined}
|
|
329
|
+
>
|
|
330
|
+
<span className="m-account__box" aria-hidden="true">
|
|
331
|
+
<svg viewBox="0 0 18 18"><polyline points="3.5 9.5 7.5 13 14.5 5" /></svg>
|
|
332
|
+
</span>
|
|
333
|
+
<span>{T.agree(card.org, card)}</span>
|
|
334
|
+
</Checkbox>
|
|
335
|
+
<p className="m-account__links">
|
|
336
|
+
{card.termsUrl && <ExternalLink href={card.termsUrl}>{T.terms}</ExternalLink>}
|
|
337
|
+
{card.privacyUrl && <ExternalLink href={card.privacyUrl}>{T.privacy}</ExternalLink>}
|
|
338
|
+
</p>
|
|
339
|
+
{errors.agree && <p id="m-account-agree-error" className="m-account__error" role="alert">{errors.agree}</p>}
|
|
340
|
+
</div>
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
let body: ReactNode = null;
|
|
344
|
+
if (screen === 'local' || screen === 'unavailable') {
|
|
345
|
+
body = <p className="m-account__notice">{screen === 'local' ? T.local : T.unavailable}</p>;
|
|
346
|
+
} else if (screen === 'email' && card) {
|
|
347
|
+
body = (
|
|
348
|
+
<div className="m-account__card">
|
|
349
|
+
<h2 className="m-account__step" ref={heading} tabIndex={-1}>{T.signInTitle(card.org, card.joining, card.joiningPaused)}</h2>
|
|
350
|
+
<p className="m-account__intro">{T.signInIntro(card.org, card.joining, card.joiningPaused)}</p>
|
|
351
|
+
<Form className="m-account__form" onSubmit={onEmail} validationBehavior="aria">
|
|
352
|
+
<TextField className="m-account__field" name="email" type="email" autoComplete="email" value={email} onChange={setEmail} isInvalid={Boolean(errors.email)} isRequired>
|
|
353
|
+
<Label className="m-account__label">{T.email}</Label>
|
|
354
|
+
<Input className="m-account__input" ref={emailField} spellCheck={false} />
|
|
355
|
+
<FieldError text={errors.email} />
|
|
356
|
+
</TextField>
|
|
357
|
+
{agreeBox}
|
|
358
|
+
<Button type="submit" className="m-account__go" isPending={busy === 'start'}>{T.continue}</Button>
|
|
359
|
+
</Form>
|
|
360
|
+
<div className="m-account__row">
|
|
361
|
+
<Button className="m-account__link" onPress={() => switchTo('password')}>{T.usePassword}</Button>
|
|
362
|
+
</div>
|
|
363
|
+
</div>
|
|
364
|
+
);
|
|
365
|
+
} else if (screen === 'password' && card) {
|
|
366
|
+
body = (
|
|
367
|
+
<div className="m-account__card">
|
|
368
|
+
<h2 className="m-account__step" ref={heading} tabIndex={-1}>{T.passwordTitle(card.org)}</h2>
|
|
369
|
+
<p className="m-account__intro">{T.passwordIntro}</p>
|
|
370
|
+
<Form className="m-account__form" onSubmit={onPassword} validationBehavior="aria">
|
|
371
|
+
<TextField className="m-account__field" name="email" type="email" autoComplete="username" value={email} onChange={setEmail} isInvalid={Boolean(errors.email)} isRequired>
|
|
372
|
+
<Label className="m-account__label">{T.email}</Label>
|
|
373
|
+
<Input className="m-account__input" ref={emailField} spellCheck={false} />
|
|
374
|
+
<FieldError text={errors.email} />
|
|
375
|
+
</TextField>
|
|
376
|
+
<TextField className="m-account__field" name="password" type="password" autoComplete="current-password" value={password} onChange={setPassword} isInvalid={Boolean(errors.password)} maxLength={PASSWORD_MAX} isRequired>
|
|
377
|
+
<Label className="m-account__label">{T.password}</Label>
|
|
378
|
+
<Input className="m-account__input" ref={passwordField} />
|
|
379
|
+
<FieldError text={errors.password} />
|
|
380
|
+
</TextField>
|
|
381
|
+
<Button type="submit" className="m-account__go" isPending={busy === 'password'}>{T.signIn}</Button>
|
|
382
|
+
</Form>
|
|
383
|
+
<div className="m-account__row">
|
|
384
|
+
<Button className="m-account__link" onPress={() => switchTo('email')}>{T.useCode}</Button>
|
|
385
|
+
</div>
|
|
386
|
+
</div>
|
|
387
|
+
);
|
|
388
|
+
} else if (screen === 'code') {
|
|
389
|
+
body = (
|
|
390
|
+
<div className="m-account__card">
|
|
391
|
+
<h2 className="m-account__step" ref={heading} tabIndex={-1}>{T.codeTitle}</h2>
|
|
392
|
+
<p className="m-account__intro">{T.codeSent(email.trim())}</p>
|
|
393
|
+
<Form className="m-account__form" onSubmit={onCode} validationBehavior="aria">
|
|
394
|
+
<TextField className="m-account__field" name="code" value={code} onChange={setCode} isInvalid={Boolean(errors.code)} isRequired>
|
|
395
|
+
<Label className="m-account__label">{T.code}</Label>
|
|
396
|
+
<Input className="m-account__input m-account__input--code" ref={codeField} inputMode="numeric" autoComplete="one-time-code" maxLength={12} spellCheck={false} />
|
|
397
|
+
<Text slot="description" className="m-account__hint">{T.codeHint}</Text>
|
|
398
|
+
<FieldError text={errors.code} />
|
|
399
|
+
</TextField>
|
|
400
|
+
<Button type="submit" className="m-account__go" isPending={busy === 'verify'}>{T.signIn}</Button>
|
|
401
|
+
</Form>
|
|
402
|
+
<div className="m-account__row">
|
|
403
|
+
<Button className="m-account__link" onPress={() => void act('resend', () => startSignIn(true))} isPending={busy === 'resend'}>{T.newCode}</Button>
|
|
404
|
+
<Button className="m-account__link" onPress={otherEmail}>{T.otherEmail}</Button>
|
|
405
|
+
</div>
|
|
406
|
+
</div>
|
|
407
|
+
);
|
|
408
|
+
} else if (screen === 'account' && me) {
|
|
409
|
+
const { member, org, joining } = me;
|
|
410
|
+
body = (
|
|
411
|
+
<>
|
|
412
|
+
<h2 className="m-account__step" ref={heading} tabIndex={-1}>{T.signedInAs(member)}</h2>
|
|
413
|
+
<p className="m-account__status" data-status={member.status}>{T.status(member, org, joining)}</p>
|
|
414
|
+
{welcome && <p className="m-account__welcome">{welcome}</p>}
|
|
415
|
+
{canJoin(member, joining) && card?.joiningPaused && <p className="m-account__status">{T.paused(org)}</p>}
|
|
416
|
+
{canJoin(member, joining) && !card?.joiningPaused && (
|
|
417
|
+
<Form className="m-account__form m-account__join" onSubmit={onJoin} validationBehavior="aria">
|
|
418
|
+
{agreeBox}
|
|
419
|
+
<Button type="submit" className="m-account__go" isPending={busy === 'join'}>{T.join(org)}</Button>
|
|
420
|
+
</Form>
|
|
421
|
+
)}
|
|
422
|
+
{next && <p><a className="m-account__back" href={next}>{T.back}</a></p>}
|
|
423
|
+
|
|
424
|
+
<section className="m-account__part" aria-labelledby="m-account-name">
|
|
425
|
+
<h2 id="m-account-name" className="m-account__part-title">{T.nameTitle}</h2>
|
|
426
|
+
<Form className="m-account__form m-account__form--row" onSubmit={onName} validationBehavior="aria">
|
|
427
|
+
<TextField className="m-account__field" name="display_name" autoComplete="name" value={name} onChange={setName} isInvalid={Boolean(errors.name)} maxLength={60}>
|
|
428
|
+
<Label className="m-account__label">{T.name}</Label>
|
|
429
|
+
<Input className="m-account__input" />
|
|
430
|
+
<Text slot="description" className="m-account__hint">{T.nameHint}</Text>
|
|
431
|
+
<FieldError text={errors.name} />
|
|
432
|
+
</TextField>
|
|
433
|
+
<Button type="submit" className="m-account__plain" isPending={busy === 'name'}>{T.saveName}</Button>
|
|
434
|
+
</Form>
|
|
435
|
+
</section>
|
|
436
|
+
|
|
437
|
+
<section className="m-account__part" aria-labelledby="m-account-password">
|
|
438
|
+
<h2 id="m-account-password" className="m-account__part-title">{T.passwordPart}</h2>
|
|
439
|
+
<p className="m-account__intro">{member.hasPassword ? T.passwordHas : T.passwordNone}</p>
|
|
440
|
+
<Form className="m-account__form m-account__form--row" onSubmit={onNewPassword} validationBehavior="aria">
|
|
441
|
+
{/* The email beside the new password, so a password manager files it under the right account. */}
|
|
442
|
+
<input type="email" name="username" autoComplete="username" value={member.email} readOnly hidden />
|
|
443
|
+
<TextField className="m-account__field" name="new-password" type="password" autoComplete="new-password" value={newPassword} onChange={setNewPassword} isInvalid={Boolean(errors.newPassword)} maxLength={PASSWORD_MAX}>
|
|
444
|
+
<Label className="m-account__label">{T.newPassword}</Label>
|
|
445
|
+
<Input className="m-account__input" ref={newPasswordField} />
|
|
446
|
+
<Text slot="description" className="m-account__hint">{T.passwordHint}</Text>
|
|
447
|
+
<FieldError text={errors.newPassword} />
|
|
448
|
+
</TextField>
|
|
449
|
+
<Button type="submit" className="m-account__plain" isPending={busy === 'new-password'}>{member.hasPassword ? T.changePassword : T.setPassword}</Button>
|
|
450
|
+
</Form>
|
|
451
|
+
</section>
|
|
452
|
+
|
|
453
|
+
<div className="m-account__part m-account__row">
|
|
454
|
+
<Button className="m-account__plain" onPress={signOut} isPending={busy === 'sign-out'}>{T.signOut}</Button>
|
|
455
|
+
<Button
|
|
456
|
+
className="m-account__link m-account__link--danger"
|
|
457
|
+
aria-expanded={deleting}
|
|
458
|
+
aria-controls="m-account-delete"
|
|
459
|
+
onPress={() => { setDeleting((d) => !d); setConfirm(''); }}
|
|
460
|
+
>
|
|
461
|
+
{T.deleteOpen}
|
|
462
|
+
</Button>
|
|
463
|
+
</div>
|
|
464
|
+
{deleting && (
|
|
465
|
+
<Form id="m-account-delete" className="m-account__form m-account__delete" onSubmit={onDelete} validationBehavior="aria">
|
|
466
|
+
<p className="m-account__intro">{T.deleteWarning(org)}</p>
|
|
467
|
+
<TextField className="m-account__field" name="confirm" value={confirm} onChange={setConfirm} autoComplete="off">
|
|
468
|
+
<Label className="m-account__label">{T.deleteConfirm}</Label>
|
|
469
|
+
<Input className="m-account__input" spellCheck={false} />
|
|
470
|
+
</TextField>
|
|
471
|
+
<div className="m-account__row">
|
|
472
|
+
<Button type="submit" className="m-account__danger" isDisabled={confirm.trim().toLowerCase() !== 'delete'} isPending={busy === 'delete'}>{T.deleteGo}</Button>
|
|
473
|
+
<Button className="m-account__link" onPress={() => { setDeleting(false); setConfirm(''); }}>{T.deleteKeep}</Button>
|
|
474
|
+
</div>
|
|
475
|
+
</Form>
|
|
476
|
+
)}
|
|
477
|
+
</>
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (screen === 'loading' || !body) return <p className="m-account__wait" role="status">{T.loading}</p>;
|
|
482
|
+
return (
|
|
483
|
+
<div className="m-account__app" data-account-ready>
|
|
484
|
+
{/* One region for what just happened, kept across steps so a screen reader hears it. */}
|
|
485
|
+
<div className="m-account__live" role="status">
|
|
486
|
+
{notice && <p className={`m-account__notice m-account__notice--${notice.tone}`}>{notice.text}</p>}
|
|
487
|
+
</div>
|
|
488
|
+
{body}
|
|
489
|
+
</div>
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** A link to the organisation's own terms, opening beside the card so nothing typed is lost. */
|
|
494
|
+
function ExternalLink({ href, children }: { href: string; children: ReactNode }) {
|
|
495
|
+
return (
|
|
496
|
+
<a href={href} target="_blank" rel="noopener noreferrer">
|
|
497
|
+
{children}
|
|
498
|
+
<span className="m-account__sr"> {T.opensNewTab}</span>
|
|
499
|
+
</a>
|
|
500
|
+
);
|
|
501
|
+
}
|