@nymphjs/tilmeld-components 1.0.0-beta.14 → 1.0.0-beta.141

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.
@@ -0,0 +1,558 @@
1
+ <svelte:options runes />
2
+
3
+ {#if $user != null}
4
+ <Dialog
5
+ {use}
6
+ bind:open
7
+ aria-labelledby="tilmeld-two-factor-title"
8
+ aria-describedby="tilmeld-two-factor-content"
9
+ surface$class="tilmeld-two-factor-dialog-surface"
10
+ {...exclude(restProps, [
11
+ 'password$',
12
+ 'code$',
13
+ 'closeButton$',
14
+ 'setupButton$',
15
+ 'removeButton$',
16
+ 'progress$',
17
+ ])}
18
+ >
19
+ <!-- Title cannot contain leading whitespace due to mdc-typography-baseline-top() -->
20
+ <Title id="tilmeld-two-factor-title">{title}</Title>
21
+ {#if hasTOTPSecret === false}
22
+ <Content id="tilmeld-two-factor-content">
23
+ <form
24
+ onsubmit={preventDefault(
25
+ stopPropagation(() => {
26
+ if (totpSecret == null) {
27
+ beginSetup();
28
+ } else {
29
+ enableTOTP();
30
+ }
31
+ }),
32
+ )}
33
+ >
34
+ {#if totpSecret == null}
35
+ <p>
36
+ 2 Factor Authentication (2FA) adds an additional layer of
37
+ security. With 2FA, no one can log in without your authenticator
38
+ device, even with your password.
39
+ </p>
40
+
41
+ <p>
42
+ You'll need an authenticator app that supports TOTP codes. Some
43
+ popular apps are linked here.
44
+ </p>
45
+
46
+ <h6 style="margin-top: 0;">Twilio Authy</h6>
47
+
48
+ <ul>
49
+ <li>
50
+ <a
51
+ href="https://play.google.com/store/apps/details?id=com.authy.authy"
52
+ target="_blank"
53
+ rel="noopener noreferrer">Google Play Store</a
54
+ >
55
+ </li>
56
+ <li>
57
+ <a
58
+ href="https://apps.apple.com/us/app/twilio-authy/id494168017"
59
+ target="_blank"
60
+ rel="noopener noreferrer">Apple App Store</a
61
+ >
62
+ </li>
63
+ <li>
64
+ <a
65
+ href="https://authy.com/download/"
66
+ target="_blank"
67
+ rel="noopener noreferrer">Desktop App</a
68
+ >
69
+ </li>
70
+ </ul>
71
+
72
+ <h6 style="margin-top: 0;">Google Authenticator</h6>
73
+
74
+ <ul>
75
+ <li>
76
+ <a
77
+ href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2"
78
+ target="_blank"
79
+ rel="noopener noreferrer">Google Play Store</a
80
+ >
81
+ </li>
82
+ <li>
83
+ <a
84
+ href="https://apps.apple.com/us/app/google-authenticator/id388497605"
85
+ target="_blank"
86
+ rel="noopener noreferrer">Apple App Store</a
87
+ >
88
+ </li>
89
+ </ul>
90
+
91
+ <h6 style="margin-top: 0;">Microsoft Authenticator</h6>
92
+
93
+ <ul>
94
+ <li>
95
+ <a
96
+ href="https://play.google.com/store/apps/details?id=com.azure.authenticator"
97
+ target="_blank"
98
+ rel="noopener noreferrer">Google Play Store</a
99
+ >
100
+ </li>
101
+ <li>
102
+ <a
103
+ href="https://apps.apple.com/us/app/microsoft-authenticator/id983156458"
104
+ target="_blank"
105
+ rel="noopener noreferrer">Apple App Store</a
106
+ >
107
+ </li>
108
+ </ul>
109
+
110
+ <p>
111
+ Once your authenticator app is ready, continue to generate a QR
112
+ code.
113
+ </p>
114
+ {:else}
115
+ <p>Scan this QR code with your authenticator app:</p>
116
+
117
+ <div style="text-align: center;">
118
+ <img alt="TOTP Set Up QR Code" src={totpSecret.qrcode} />
119
+ </div>
120
+
121
+ <p>Or you can enter this code manually into your app:</p>
122
+
123
+ <div style="text-align: center;">
124
+ <code style="word-break: break-all;">{totpSecret.secret}</code>
125
+ </div>
126
+
127
+ <p>Enter your password to allow this account change:</p>
128
+
129
+ <div>
130
+ <Textfield
131
+ bind:value={password}
132
+ label="Password"
133
+ type="password"
134
+ style="width: 100%;"
135
+ input$autocomplete="current-password"
136
+ {...prefixFilter(restProps, 'password$')}
137
+ />
138
+ </div>
139
+
140
+ <p>
141
+ Enter a code generated by your app to confirm the account is
142
+ configured correctly:
143
+ </p>
144
+
145
+ <div>
146
+ <Textfield
147
+ bind:value={code}
148
+ label="2FA Code"
149
+ style="width: 100%;"
150
+ input$maxlength={6}
151
+ input$minlength={6}
152
+ {...prefixFilter(restProps, 'code$')}
153
+ />
154
+ </div>
155
+ {/if}
156
+
157
+ {#if failureMessage}
158
+ <div class="tilmeld-two-factor-failure">
159
+ {failureMessage}
160
+ </div>
161
+ {/if}
162
+
163
+ {#if loading}
164
+ <div class="tilmeld-two-factor-loading">
165
+ <CircularProgress
166
+ style="height: 24px; width: 24px;"
167
+ indeterminate
168
+ {...prefixFilter(restProps, 'progress$')}
169
+ />
170
+ </div>
171
+ {/if}
172
+ </form>
173
+ </Content>
174
+ <Actions>
175
+ <Button disabled={loading} {...prefixFilter(restProps, 'closeButton$')}>
176
+ <Label>Close</Label>
177
+ </Button>
178
+ {#if totpSecret == null}
179
+ <Button
180
+ onclick={preventDefault(stopPropagation(beginSetup))}
181
+ disabled={loading}
182
+ {...prefixFilter(restProps, 'setupButton$')}
183
+ >
184
+ <Label>Set Up 2FA</Label>
185
+ </Button>
186
+ {:else}
187
+ <Button
188
+ onclick={preventDefault(stopPropagation(enableTOTP))}
189
+ disabled={loading}
190
+ {...prefixFilter(restProps, 'setupButton$')}
191
+ >
192
+ <Label>Enable 2FA</Label>
193
+ </Button>
194
+ {/if}
195
+ </Actions>
196
+ {:else if hasTOTPSecret === true}
197
+ <Content id="tilmeld-two-factor-content">
198
+ <form onsubmit={preventDefault(stopPropagation(disableTOTP))}>
199
+ <p>
200
+ 2 Factor Authentication (2FA) is enabled on your account. If you'd
201
+ like to disable it, you can provide your password and a code
202
+ generated by your Authenticator app. You can re-enable it by
203
+ generating a new QR code at any time.
204
+ </p>
205
+
206
+ <div>
207
+ <Textfield
208
+ bind:value={password}
209
+ label="Password"
210
+ type="password"
211
+ style="width: 100%;"
212
+ input$autocomplete="current-password"
213
+ {...prefixFilter(restProps, 'password$')}
214
+ />
215
+ </div>
216
+
217
+ <div>
218
+ <Textfield
219
+ bind:value={code}
220
+ label="2FA Code"
221
+ style="width: 100%;"
222
+ input$maxlength={6}
223
+ input$minlength={6}
224
+ {...prefixFilter(restProps, 'code$')}
225
+ />
226
+ </div>
227
+
228
+ {@render additional?.()}
229
+
230
+ {#if failureMessage}
231
+ <div class="tilmeld-two-factor-failure">
232
+ {failureMessage}
233
+ </div>
234
+ {/if}
235
+
236
+ {#if loading}
237
+ <div class="tilmeld-two-factor-loading">
238
+ <CircularProgress
239
+ style="height: 24px; width: 24px;"
240
+ indeterminate
241
+ {...prefixFilter(restProps, 'progress$')}
242
+ />
243
+ </div>
244
+ {/if}
245
+ </form>
246
+ </Content>
247
+ <Actions>
248
+ <Button disabled={loading} {...prefixFilter(restProps, 'closeButton$')}>
249
+ <Label>Close</Label>
250
+ </Button>
251
+ <Button
252
+ onclick={preventDefault(stopPropagation(disableTOTP))}
253
+ disabled={loading}
254
+ {...prefixFilter(restProps, 'removeButton$')}
255
+ >
256
+ <Label>Disable 2FA</Label>
257
+ </Button>
258
+ </Actions>
259
+ {:else}
260
+ <div class="tilmeld-two-factor-loading">
261
+ <CircularProgress
262
+ style="height: 24px; width: 24px;"
263
+ indeterminate
264
+ {...prefixFilter(restProps, 'progress$')}
265
+ />
266
+ </div>
267
+ {/if}
268
+ </Dialog>
269
+ {/if}
270
+
271
+ <script lang="ts">
272
+ import type { ComponentProps, Snippet } from 'svelte';
273
+ import { onMount, onDestroy } from 'svelte';
274
+ import type { Writable } from 'svelte/store';
275
+ import { writable } from 'svelte/store';
276
+ import CircularProgress from '@smui/circular-progress';
277
+ import Dialog, { Title, Content, Actions } from '@smui/dialog';
278
+ import Textfield from '@smui/textfield';
279
+ import Button, { Label } from '@smui/button';
280
+ import type { ActionArray } from '@smui/common/internal';
281
+ import { exclude, prefixFilter } from '@smui/common/internal';
282
+ import { preventDefault, stopPropagation } from '@smui/common/events';
283
+ import type { CurrentUserData } from '@nymphjs/tilmeld-client';
284
+ import type { User as UserClass } from '@nymphjs/tilmeld-client';
285
+
286
+ type OwnProps = {
287
+ /**
288
+ * An array of Action or [Action, ActionProps] to be applied to the element.
289
+ */
290
+ use?: ActionArray;
291
+ /**
292
+ * Whether the dialog is open.
293
+ */
294
+ open?: boolean;
295
+ /**
296
+ * The title of the dialog.
297
+ */
298
+ title?: string;
299
+ /**
300
+ * The User class from Nymph.
301
+ */
302
+ User: typeof UserClass;
303
+ /**
304
+ * A writable store of the current user.
305
+ *
306
+ * It will be retrieved from the server if not provided.
307
+ */
308
+ user?: Writable<(UserClass & CurrentUserData) | null | undefined>;
309
+ /**
310
+ * Server provided. You can bind to it if you need to.
311
+ */
312
+ hasTOTPSecret?: boolean | null;
313
+ /**
314
+ * Server provided. You can bind to it if you need to.
315
+ */
316
+ totpSecret?: {
317
+ uri: string;
318
+ qrcode: string;
319
+ secret: string;
320
+ } | null;
321
+ /**
322
+ * User provided. You can bind to it if you need to.
323
+ */
324
+ password?: string;
325
+ /**
326
+ * User provided. You can bind to it if you need to.
327
+ */
328
+ code?: string;
329
+
330
+ /**
331
+ * A spot for additional content.
332
+ */
333
+ additional?: Snippet;
334
+ };
335
+ let {
336
+ use = [],
337
+ open = $bindable(false),
338
+ title = 'Manage Two Factor Authentication',
339
+ User,
340
+ user = $bindable(writable(false as unknown as undefined)),
341
+ hasTOTPSecret = $bindable(null),
342
+ totpSecret = $bindable(null),
343
+ password = $bindable(''),
344
+ code = $bindable(''),
345
+ additional,
346
+ ...restProps
347
+ }: OwnProps & {
348
+ [
349
+ k in keyof ComponentProps<typeof Textfield> as `password\$${k}`
350
+ ]?: ComponentProps<typeof Textfield>[k];
351
+ } & {
352
+ [
353
+ k in keyof ComponentProps<typeof Textfield> as `code\$${k}`
354
+ ]?: ComponentProps<typeof Textfield>[k];
355
+ } & {
356
+ [
357
+ k in keyof ComponentProps<typeof CircularProgress> as `progress\$${k}`
358
+ ]?: ComponentProps<typeof CircularProgress>[k];
359
+ } & {
360
+ [
361
+ k in keyof ComponentProps<
362
+ typeof Button<undefined, 'button'>
363
+ > as `closeButton\$${k}`
364
+ ]?: ComponentProps<typeof Button<undefined, 'button'>>[k];
365
+ } & {
366
+ [
367
+ k in keyof ComponentProps<
368
+ typeof Button<undefined, 'button'>
369
+ > as `setupButton\$${k}`
370
+ ]?: ComponentProps<typeof Button<undefined, 'button'>>[k];
371
+ } & {
372
+ [
373
+ k in keyof ComponentProps<
374
+ typeof Button<undefined, 'button'>
375
+ > as `removeButton\$${k}`
376
+ ]?: ComponentProps<typeof Button<undefined, 'button'>>[k];
377
+ } = $props();
378
+
379
+ let loading = $state(false);
380
+ let failureMessage: string | undefined = $state();
381
+
382
+ $effect(() => {
383
+ if (!open) {
384
+ loading = false;
385
+ failureMessage = undefined;
386
+ totpSecret = null;
387
+ password = '';
388
+ code = '';
389
+ }
390
+ });
391
+
392
+ const onLogin = (currentUser: UserClass & CurrentUserData) => {
393
+ $user = currentUser;
394
+ hasTOTPSecret = null;
395
+ $user.$hasTOTPSecret().then(
396
+ (value) => {
397
+ hasTOTPSecret = value;
398
+ failureMessage = undefined;
399
+ },
400
+ (e) => {
401
+ hasTOTPSecret = null;
402
+ failureMessage = e?.message;
403
+ },
404
+ );
405
+ };
406
+ const onLogout = () => {
407
+ $user = null;
408
+ hasTOTPSecret = null;
409
+ };
410
+
411
+ onMount(async () => {
412
+ User.on('login', onLogin);
413
+ User.on('logout', onLogout);
414
+ if ($user === (false as unknown as undefined)) {
415
+ $user = undefined;
416
+ $user = await User.current();
417
+ }
418
+ if ($user) {
419
+ try {
420
+ hasTOTPSecret = await $user.$hasTOTPSecret();
421
+ failureMessage = undefined;
422
+ } catch (e: any) {
423
+ hasTOTPSecret = null;
424
+ failureMessage = e?.message;
425
+ }
426
+ } else {
427
+ hasTOTPSecret = null;
428
+ }
429
+ });
430
+
431
+ onDestroy(() => {
432
+ User.off('login', onLogin);
433
+ User.off('logout', onLogout);
434
+ });
435
+
436
+ async function beginSetup() {
437
+ loading = true;
438
+
439
+ if ($user == null) {
440
+ failureMessage = 'You must be logged in.';
441
+ loading = false;
442
+ return;
443
+ }
444
+
445
+ try {
446
+ totpSecret = await $user.$getNewTOTPSecret();
447
+ } catch (e: any) {
448
+ failureMessage = e?.message;
449
+ }
450
+ loading = false;
451
+ }
452
+
453
+ async function enableTOTP() {
454
+ if (password === '') {
455
+ failureMessage = 'You need to enter your current password';
456
+ return;
457
+ }
458
+
459
+ if (code === '') {
460
+ failureMessage = 'You need to enter a generated code';
461
+ return;
462
+ }
463
+
464
+ if (totpSecret == null) {
465
+ failureMessage = 'TOTP secret is not set';
466
+ return;
467
+ }
468
+
469
+ failureMessage = undefined;
470
+ loading = true;
471
+
472
+ // Get the current user again, in case their data has changed.
473
+ $user = await User.current();
474
+
475
+ if ($user == null) {
476
+ failureMessage = 'You must be logged in.';
477
+ loading = false;
478
+ return;
479
+ }
480
+
481
+ try {
482
+ // Save the user's TOTP secret.
483
+ const data = await $user.$saveTOTPSecret({
484
+ password,
485
+ secret: totpSecret.secret,
486
+ code,
487
+ });
488
+
489
+ if (!data.result) {
490
+ failureMessage = data.message;
491
+ } else {
492
+ open = false;
493
+ hasTOTPSecret = true;
494
+ }
495
+ } catch (e: any) {
496
+ failureMessage = e?.message;
497
+ }
498
+ loading = false;
499
+ }
500
+
501
+ async function disableTOTP() {
502
+ if (password === '') {
503
+ failureMessage = 'You need to enter your current password';
504
+ return;
505
+ }
506
+
507
+ if (code === '') {
508
+ failureMessage = 'You need to enter a generated code';
509
+ return;
510
+ }
511
+
512
+ failureMessage = undefined;
513
+ loading = true;
514
+
515
+ // Get the current user again, in case their data has changed.
516
+ $user = await User.current();
517
+
518
+ if ($user == null) {
519
+ failureMessage = 'You must be logged in.';
520
+ loading = false;
521
+ return;
522
+ }
523
+
524
+ try {
525
+ // Remove the user's TOTP secret.
526
+ const data = await $user.$removeTOTPSecret({
527
+ password,
528
+ code,
529
+ });
530
+
531
+ if (!data.result) {
532
+ failureMessage = data.message;
533
+ } else {
534
+ open = false;
535
+ hasTOTPSecret = false;
536
+ }
537
+ } catch (e: any) {
538
+ failureMessage = e?.message;
539
+ }
540
+ loading = false;
541
+ }
542
+ </script>
543
+
544
+ <style>
545
+ :global(.mdc-dialog .mdc-dialog__surface.tilmeld-two-factor-dialog-surface) {
546
+ width: 400px;
547
+ max-width: calc(100vw - 32px);
548
+ }
549
+ .tilmeld-two-factor-failure {
550
+ margin-top: 1em;
551
+ color: var(--mdc-theme-error, #f00);
552
+ }
553
+ .tilmeld-two-factor-loading {
554
+ display: flex;
555
+ justify-content: center;
556
+ align-items: center;
557
+ }
558
+ </style>
package/src/index.d.ts CHANGED
@@ -2,5 +2,7 @@ import Account from './Account.svelte';
2
2
  import ChangePassword from './ChangePassword.svelte';
3
3
  import Login from './Login.svelte';
4
4
  import Recover from './Recover.svelte';
5
+ import RevokeTokens from './RevokeTokens.svelte';
6
+ import TwoFactor from './TwoFactor.svelte';
5
7
 
6
- export { Account, ChangePassword, Login, Recover };
8
+ export { Account, ChangePassword, Login, Recover, RevokeTokens, TwoFactor };
package/src/index.ts CHANGED
@@ -2,5 +2,7 @@ import Account from './Account.svelte';
2
2
  import ChangePassword from './ChangePassword.svelte';
3
3
  import Login from './Login.svelte';
4
4
  import Recover from './Recover.svelte';
5
+ import RevokeTokens from './RevokeTokens.svelte';
6
+ import TwoFactor from './TwoFactor.svelte';
5
7
 
6
- export { Account, ChangePassword, Login, Recover };
8
+ export { Account, ChangePassword, Login, Recover, RevokeTokens, TwoFactor };
package/tsconfig.json CHANGED
@@ -2,15 +2,19 @@
2
2
  "extends": "@tsconfig/svelte/tsconfig.json",
3
3
 
4
4
  "compilerOptions": {
5
- "lib": ["DOM", "ES2021"],
6
- "target": "ES2021",
5
+ "types": ["jest"],
6
+ "module": "nodenext",
7
+ "moduleResolution": "nodenext",
8
+ "lib": ["DOM", "es2024"],
9
+ "target": "es2024",
7
10
  "noImplicitAny": true,
8
11
  "strict": true,
9
12
 
10
- "removeComments": true,
13
+ "removeComments": false,
11
14
  "sourceMap": true,
12
- "outDir": "lib",
15
+ "outDir": "dist",
13
16
  "resolveJsonModule": true,
17
+ "rootDir": "src/",
14
18
  "declaration": true
15
19
  },
16
20
  "include": ["src/**/*"],
package/typedoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../../typedoc.base.json"],
3
+ "entryPoints": ["src/index.ts"]
4
+ }