@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.
@@ -1,11 +1,13 @@
1
- {#if clientConfig != null && user != null}
1
+ <svelte:options runes />
2
+
3
+ {#if $clientConfig != null && $user != null}
2
4
  <Dialog
3
- use={usePass}
5
+ {use}
4
6
  bind:open
5
7
  aria-labelledby="tilmeld-account-title"
6
8
  aria-describedby="tilmeld-account-content"
7
9
  surface$class="tilmeld-account-dialog-surface"
8
- {...exclude($$restProps, [
10
+ {...exclude(restProps, [
9
11
  'username$',
10
12
  'email$',
11
13
  'nameFirst$',
@@ -13,247 +15,417 @@
13
15
  'nameLast$',
14
16
  'phone$',
15
17
  'changePasswordLink$',
18
+ 'revokeTokensLink$',
16
19
  'closeButton$',
17
20
  'saveButton$',
18
21
  'changePassword$',
22
+ 'revokeTokens$',
23
+ 'twoFactor$',
19
24
  'progress$',
20
25
  ])}
21
26
  >
22
27
  <!-- Title cannot contain leading whitespace due to mdc-typography-baseline-top() -->
23
28
  <Title id="tilmeld-account-title">{title}</Title>
24
29
  <Content id="tilmeld-account-content">
25
- {#if !clientConfig.emailUsernames && clientConfig.allowUsernameChange}
26
- <div>
27
- <Textfield
28
- bind:value={user.username}
29
- label="Username"
30
- type="text"
31
- style="width: 100%;"
32
- helperLine$style="width: 100%;"
33
- invalid={usernameVerified === false}
34
- input$autocomplete="username"
35
- input$autocapitalize="off"
36
- input$spellcheck="false"
37
- input$emptyValueUndefined
38
- {...prefixFilter($$restProps, 'username$')}
39
- >
40
- <HelperText persistent slot="helper">
41
- {usernameVerifiedMessage || ''}
42
- </HelperText>
43
- </Textfield>
44
- </div>
45
- {/if}
30
+ <form onsubmit={preventDefault(stopPropagation(save))}>
31
+ {#if !$clientConfig.emailUsernames && $clientConfig.allowUsernameChange}
32
+ <div>
33
+ <Textfield
34
+ bind:value={$user.username}
35
+ label="Username"
36
+ type="text"
37
+ style="width: 100%;"
38
+ helperLine$style="width: 100%;"
39
+ invalid={usernameVerified === false}
40
+ input$autocomplete="username"
41
+ input$autocapitalize="off"
42
+ input$spellcheck="false"
43
+ input$emptyValueUndefined
44
+ {...prefixFilter(restProps, 'username$')}
45
+ >
46
+ {#snippet helper()}
47
+ <HelperText persistent>
48
+ {usernameVerifiedMessage || ''}
49
+ </HelperText>
50
+ {/snippet}
51
+ </Textfield>
52
+ </div>
53
+ {/if}
46
54
 
47
- {#if clientConfig.emailUsernames || clientConfig.userFields.indexOf('email') !== -1}
48
- <div>
49
- <Textfield
50
- bind:value={user.email}
51
- label="Email"
52
- type="email"
53
- style="width: 100%;"
54
- helperLine$style="width: 100%;"
55
- invalid={emailVerified === false}
56
- input$autocomplete="email"
57
- input$autocapitalize="off"
58
- input$spellcheck="false"
59
- input$emptyValueUndefined
60
- {...prefixFilter($$restProps, 'email$')}
61
- >
62
- <HelperText persistent slot="helper">
63
- {emailVerifiedMessage || ''}
64
- </HelperText>
65
- </Textfield>
66
- </div>
67
- {/if}
55
+ {#if $clientConfig.emailUsernames || $clientConfig.userFields.includes('email')}
56
+ <div>
57
+ <Textfield
58
+ bind:value={$user.email}
59
+ label="Email"
60
+ type="email"
61
+ style="width: 100%;"
62
+ helperLine$style="width: 100%;"
63
+ invalid={emailVerified === false}
64
+ input$autocomplete="email"
65
+ input$autocapitalize="off"
66
+ input$spellcheck="false"
67
+ input$emptyValueUndefined
68
+ {...prefixFilter(restProps, 'email$')}
69
+ >
70
+ {#snippet helper()}
71
+ <HelperText persistent>
72
+ {emailVerifiedMessage || ''}
73
+ </HelperText>
74
+ {/snippet}
75
+ </Textfield>
76
+ </div>
77
+ {/if}
68
78
 
69
- {#if clientConfig.userFields.indexOf('name') !== -1}
70
- <div>
71
- <Textfield
72
- bind:value={user.nameFirst}
73
- label="First Name"
74
- type="text"
75
- style="width: 100%;"
76
- input$autocomplete="given-name"
77
- input$emptyValueUndefined
78
- {...prefixFilter($$restProps, 'nameFirst$')}
79
- />
80
- </div>
79
+ {#if $clientConfig.userFields.includes('name')}
80
+ <div>
81
+ <Textfield
82
+ bind:value={$user.nameFirst}
83
+ label="First Name"
84
+ type="text"
85
+ style="width: 100%;"
86
+ input$autocomplete="given-name"
87
+ input$emptyValueUndefined
88
+ {...prefixFilter(restProps, 'nameFirst$')}
89
+ />
90
+ </div>
81
91
 
82
- <div>
83
- <Textfield
84
- bind:value={user.nameMiddle}
85
- label="Middle Name"
86
- type="text"
87
- style="width: 100%;"
88
- input$autocomplete="additional-name"
89
- input$emptyValueUndefined
90
- {...prefixFilter($$restProps, 'nameMiddle$')}
91
- />
92
- </div>
92
+ <div>
93
+ <Textfield
94
+ bind:value={$user.nameMiddle}
95
+ label="Middle Name"
96
+ type="text"
97
+ style="width: 100%;"
98
+ input$autocomplete="additional-name"
99
+ input$emptyValueUndefined
100
+ {...prefixFilter(restProps, 'nameMiddle$')}
101
+ />
102
+ </div>
93
103
 
94
- <div>
95
- <Textfield
96
- bind:value={user.nameLast}
97
- label="Last Name"
98
- type="text"
99
- style="width: 100%;"
100
- input$autocomplete="family-name"
101
- input$emptyValueUndefined
102
- {...prefixFilter($$restProps, 'nameLast$')}
103
- />
104
- </div>
105
- {/if}
104
+ <div>
105
+ <Textfield
106
+ bind:value={$user.nameLast}
107
+ label="Last Name"
108
+ type="text"
109
+ style="width: 100%;"
110
+ input$autocomplete="family-name"
111
+ input$emptyValueUndefined
112
+ {...prefixFilter(restProps, 'nameLast$')}
113
+ />
114
+ </div>
115
+ {/if}
116
+
117
+ {#if $clientConfig.userFields.includes('phone')}
118
+ <div>
119
+ <Textfield
120
+ bind:value={$user.phone}
121
+ label="Phone Number"
122
+ type="tel"
123
+ style="width: 100%;"
124
+ input$autocomplete="tel"
125
+ input$name="phone"
126
+ input$emptyValueUndefined
127
+ {...prefixFilter(restProps, 'phone$')}
128
+ />
129
+ </div>
130
+ {/if}
106
131
 
107
- {#if clientConfig.userFields.indexOf('phone') !== -1}
108
- <div>
109
- <Textfield
110
- bind:value={user.phone}
111
- label="Phone Number"
112
- type="tel"
113
- style="width: 100%;"
114
- input$autocomplete="tel"
115
- input$name="phone"
116
- input$emptyValueUndefined
117
- {...prefixFilter($$restProps, 'phone$')}
118
- />
132
+ {@render additional?.()}
133
+
134
+ <div class="tilmeld-account-action">
135
+ <a
136
+ href={'javascript:void(0);'}
137
+ onclick={() => {
138
+ open = false;
139
+ changePasswordOpen = true;
140
+ }}
141
+ {...prefixFilter(restProps, 'changePasswordLink$')}
142
+ >
143
+ Change your password.
144
+ </a>
119
145
  </div>
120
- {/if}
121
146
 
122
- <slot name="additional" />
147
+ <div class="tilmeld-account-action">
148
+ <a
149
+ href={'javascript:void(0);'}
150
+ onclick={() => {
151
+ open = false;
152
+ revokeTokensOpen = true;
153
+ }}
154
+ {...prefixFilter(restProps, 'revokeTokensLink$')}
155
+ >
156
+ Log out of other sessions.
157
+ </a>
123
158
 
124
- <div class="tilmeld-account-action">
125
- <a
126
- href="javascript:void(0);"
127
- on:click={() => {
128
- open = false;
129
- changePasswordOpen = true;
130
- }}
131
- {...prefixFilter($$restProps, 'changePasswordLink$')}
132
- >
133
- Change your password.
134
- </a>
135
- </div>
159
+ <div class="tilmeld-account-action">
160
+ <a
161
+ href={'javascript:void(0);'}
162
+ onclick={() => {
163
+ open = false;
164
+ twoFactorOpen = true;
165
+ }}
166
+ {...prefixFilter(restProps, 'twoFactor$')}
167
+ >
168
+ {#if hasTOTPSecret === false}
169
+ Enable two factor authentication (2FA).
170
+ {:else}
171
+ Manage two factor authentication (2FA).
172
+ {/if}
173
+ </a>
174
+ </div>
136
175
 
137
- {#if failureMessage}
138
- <div class="tilmeld-account-failure">
139
- {failureMessage}
140
- </div>
141
- {/if}
176
+ {#if failureMessage}
177
+ <div class="tilmeld-account-failure">
178
+ {failureMessage}
179
+ </div>
180
+ {/if}
142
181
 
143
- {#if saving}
144
- <div class="tilmeld-account-loading">
145
- <CircularProgress
146
- style="height: 24px; width: 24px;"
147
- indeterminate
148
- {...prefixFilter($$restProps, 'progress$')}
149
- />
182
+ {#if loading}
183
+ <div class="tilmeld-account-loading">
184
+ <CircularProgress
185
+ style="height: 24px; width: 24px;"
186
+ indeterminate
187
+ {...prefixFilter(restProps, 'progress$')}
188
+ />
189
+ </div>
190
+ {/if}
150
191
  </div>
151
- {/if}
192
+ </form>
152
193
  </Content>
153
194
  <Actions>
154
- <Button disabled={saving} {...prefixFilter($$restProps, 'closeButton$')}>
195
+ <Button disabled={loading} {...prefixFilter(restProps, 'closeButton$')}>
155
196
  <Label>Close</Label>
156
197
  </Button>
157
198
  <Button
158
- on:click$preventDefault$stopPropagation={save}
159
- disabled={saving}
160
- {...prefixFilter($$restProps, 'saveButton$')}
199
+ onclick={preventDefault(stopPropagation(save))}
200
+ disabled={loading}
201
+ {...prefixFilter(restProps, 'saveButton$')}
161
202
  >
162
203
  <Label>Save Changes</Label>
163
204
  </Button>
164
205
  </Actions>
165
206
  </Dialog>
207
+
166
208
  <ChangePassword
167
209
  {User}
168
210
  bind:open={changePasswordOpen}
169
211
  bind:user
170
- {...prefixFilter($$restProps, 'changePassword$')}
212
+ {...prefixFilter(restProps, 'changePassword$')}
213
+ />
214
+
215
+ <RevokeTokens
216
+ {User}
217
+ bind:open={revokeTokensOpen}
218
+ bind:user
219
+ {...prefixFilter(restProps, 'revokeTokens$')}
220
+ />
221
+
222
+ <TwoFactor
223
+ {User}
224
+ bind:open={twoFactorOpen}
225
+ bind:user
226
+ bind:hasTOTPSecret
227
+ {...prefixFilter(restProps, 'twoFactor$')}
171
228
  />
172
229
  {/if}
173
230
 
174
231
  <script lang="ts">
232
+ import type { ComponentProps, Snippet } from 'svelte';
175
233
  import { onMount, onDestroy } from 'svelte';
176
- import { get_current_component } from 'svelte/internal';
234
+ import type { Writable } from 'svelte/store';
235
+ import { writable } from 'svelte/store';
177
236
  import CircularProgress from '@smui/circular-progress';
178
237
  import Dialog, { Title, Content, Actions } from '@smui/dialog';
179
238
  import Textfield from '@smui/textfield';
180
239
  import HelperText from '@smui/textfield/helper-text';
181
240
  import Button, { Label } from '@smui/button';
182
241
  import type { ActionArray } from '@smui/common/internal';
183
- import {
184
- forwardEventsBuilder,
185
- exclude,
186
- prefixFilter,
187
- } from '@smui/common/internal';
242
+ import { exclude, prefixFilter } from '@smui/common/internal';
243
+ import { preventDefault, stopPropagation } from '@smui/common/events';
244
+ import type { SmuiElementPropMap } from '@smui/common';
188
245
  import type { ClientConfig, CurrentUserData } from '@nymphjs/tilmeld-client';
189
246
  import type { User as UserClass } from '@nymphjs/tilmeld-client';
190
247
  import ChangePassword from './ChangePassword.svelte';
248
+ import RevokeTokens from './RevokeTokens.svelte';
249
+ import TwoFactor from './TwoFactor.svelte';
191
250
 
192
- const forwardEvents = forwardEventsBuilder(get_current_component());
251
+ type OwnProps = {
252
+ /**
253
+ * An array of Action or [Action, ActionProps] to be applied to the element.
254
+ */
255
+ use?: ActionArray;
256
+ /**
257
+ * Whether the dialog is open.
258
+ */
259
+ open?: boolean;
260
+ /**
261
+ * The title of the dialog.
262
+ */
263
+ title?: string;
264
+ /**
265
+ * A writable store of the Nymph client config.
266
+ *
267
+ * It will be retrieved from the server if not provided.
268
+ */
269
+ clientConfig?: Writable<ClientConfig | undefined>;
270
+ /**
271
+ * The User class from Nymph.
272
+ */
273
+ User: typeof UserClass;
274
+ /**
275
+ * A writable store of the current user.
276
+ *
277
+ * It will be retrieved from the server if not provided.
278
+ */
279
+ user?: Writable<(UserClass & CurrentUserData) | null | undefined>;
193
280
 
194
- export let use: ActionArray = [];
195
- $: usePass = [forwardEvents, ...use] as ActionArray;
196
- export let open = false;
197
- export let title = 'Your Account';
198
- export let clientConfig: ClientConfig | undefined = undefined;
199
- export let User: typeof UserClass;
200
- export let user: (UserClass & CurrentUserData) | undefined = undefined;
281
+ /**
282
+ * A spot for additional content.
283
+ */
284
+ additional?: Snippet;
285
+ };
286
+ let {
287
+ use = [],
288
+ open = $bindable(false),
289
+ title = 'Your Account',
290
+ clientConfig = $bindable(writable(false as unknown as undefined)),
291
+ User,
292
+ user = $bindable(writable(false as unknown as undefined)),
293
+ additional,
294
+ ...restProps
295
+ }: OwnProps & {
296
+ [
297
+ k in keyof ComponentProps<typeof Textfield> as `username\$${k}`
298
+ ]?: ComponentProps<typeof Textfield>[k];
299
+ } & {
300
+ [
301
+ k in keyof ComponentProps<typeof Textfield> as `email\$${k}`
302
+ ]?: ComponentProps<typeof Textfield>[k];
303
+ } & {
304
+ [
305
+ k in keyof ComponentProps<typeof Textfield> as `nameFirst\$${k}`
306
+ ]?: ComponentProps<typeof Textfield>[k];
307
+ } & {
308
+ [
309
+ k in keyof ComponentProps<typeof Textfield> as `nameMiddle\$${k}`
310
+ ]?: ComponentProps<typeof Textfield>[k];
311
+ } & {
312
+ [
313
+ k in keyof ComponentProps<typeof Textfield> as `nameLast\$${k}`
314
+ ]?: ComponentProps<typeof Textfield>[k];
315
+ } & {
316
+ [
317
+ k in keyof ComponentProps<typeof Textfield> as `phone\$${k}`
318
+ ]?: ComponentProps<typeof Textfield>[k];
319
+ } & {
320
+ [
321
+ k in keyof SmuiElementPropMap['a'] as `changePasswordLink\$${k}`
322
+ ]?: SmuiElementPropMap['a'][k];
323
+ } & {
324
+ [
325
+ k in keyof SmuiElementPropMap['a'] as `revokeTokensLink\$${k}`
326
+ ]?: SmuiElementPropMap['a'][k];
327
+ } & {
328
+ [
329
+ k in keyof ComponentProps<typeof CircularProgress> as `progress\$${k}`
330
+ ]?: ComponentProps<typeof CircularProgress>[k];
331
+ } & {
332
+ [
333
+ k in keyof ComponentProps<
334
+ typeof Button<undefined, 'button'>
335
+ > as `closeButton\$${k}`
336
+ ]?: ComponentProps<typeof Button<undefined, 'button'>>[k];
337
+ } & {
338
+ [
339
+ k in keyof ComponentProps<
340
+ typeof Button<undefined, 'button'>
341
+ > as `saveButton\$${k}`
342
+ ]?: ComponentProps<typeof Button<undefined, 'button'>>[k];
343
+ } & {
344
+ [
345
+ k in keyof ComponentProps<typeof ChangePassword> as `changePassword\$${k}`
346
+ ]?: ComponentProps<typeof ChangePassword>[k];
347
+ } & {
348
+ [
349
+ k in keyof ComponentProps<typeof RevokeTokens> as `revokeTokens\$${k}`
350
+ ]?: ComponentProps<typeof RevokeTokens>[k];
351
+ } & {
352
+ [
353
+ k in keyof ComponentProps<typeof TwoFactor> as `twoFactor\$${k}`
354
+ ]?: ComponentProps<typeof TwoFactor>[k];
355
+ } = $props();
201
356
 
202
- let saving = false;
203
- let originalUsername: string | undefined = undefined;
204
- let originalEmail: string | undefined = undefined;
205
- let failureMessage: string | undefined = undefined;
206
- let usernameTimer: NodeJS.Timeout | undefined = undefined;
207
- let usernameVerified: boolean | undefined = undefined;
208
- let usernameVerifiedMessage: string | undefined = undefined;
209
- let emailTimer: NodeJS.Timeout | undefined = undefined;
210
- let emailVerified: boolean | undefined = undefined;
211
- let emailVerifiedMessage: string | undefined = undefined;
212
- let changePasswordOpen = false;
357
+ let loading = $state(false);
358
+ let originalUsername: string | undefined = $user?.username;
359
+ let originalEmail: string | undefined = $user?.email;
360
+ let failureMessage: string | undefined = $state();
361
+ let usernameTimer: number | undefined = undefined;
362
+ let usernameVerified: boolean | undefined = $state();
363
+ let usernameVerifiedMessage: string | undefined = $state();
364
+ let emailTimer: number | undefined = undefined;
365
+ let emailVerified: boolean | undefined = $state();
366
+ let emailVerifiedMessage: string | undefined = $state();
367
+ let hasTOTPSecret: boolean | null = $state(null);
368
+ let changePasswordOpen = $state(false);
369
+ let revokeTokensOpen = $state(false);
370
+ let twoFactorOpen = $state(false);
213
371
 
214
372
  const onLogin = (currentUser: UserClass & CurrentUserData) => {
215
- user = currentUser;
216
- originalUsername = user?.username;
217
- originalEmail = user?.email;
373
+ $user = currentUser;
374
+ originalUsername = $user?.username;
375
+ originalEmail = $user?.email;
218
376
  };
219
377
  const onLogout = () => {
220
- user = undefined;
378
+ $user = null;
221
379
  originalUsername = undefined;
222
380
  originalEmail = undefined;
223
381
  };
224
382
 
225
- $: if (user && user.username !== originalUsername) {
226
- checkUsername();
227
- } else {
228
- if (usernameTimer) {
229
- clearTimeout(usernameTimer);
383
+ $effect(() => {
384
+ if ($user && $user.username !== originalUsername) {
385
+ if ($user.$isDirty('username')) {
386
+ checkUsername();
387
+ } else {
388
+ originalUsername = $user.username;
389
+ }
390
+ } else {
391
+ if (usernameTimer) {
392
+ clearTimeout(usernameTimer);
393
+ }
394
+ usernameVerified = true;
395
+ usernameVerifiedMessage = undefined;
230
396
  }
231
- usernameVerified = true;
232
- usernameVerifiedMessage = undefined;
233
- }
397
+ });
234
398
 
235
- $: if (user && user.email !== originalEmail) {
236
- checkEmail();
237
- } else {
238
- if (emailTimer) {
239
- clearTimeout(emailTimer);
399
+ $effect(() => {
400
+ if ($user && $user.email !== originalEmail) {
401
+ if ($user.$isDirty('email')) {
402
+ checkEmail();
403
+ } else {
404
+ originalEmail = $user.email;
405
+ }
406
+ } else {
407
+ if (emailTimer) {
408
+ clearTimeout(emailTimer);
409
+ }
410
+ emailVerified = true;
411
+ emailVerifiedMessage = undefined;
240
412
  }
241
- emailVerified = true;
242
- emailVerifiedMessage = undefined;
243
- }
413
+ });
244
414
 
245
415
  onMount(async () => {
246
416
  User.on('login', onLogin);
247
417
  User.on('logout', onLogout);
248
- if (user === undefined) {
249
- user = (await User.current()) ?? undefined;
418
+ if ($user === (false as unknown as undefined)) {
419
+ $user = undefined;
420
+ $user = await User.current();
250
421
  }
251
- originalUsername = user?.username;
252
- originalEmail = user?.email;
422
+ originalUsername = $user?.username;
423
+ originalEmail = $user?.email;
253
424
  });
254
425
  onMount(async () => {
255
- if (clientConfig === undefined) {
256
- clientConfig = await User.getClientConfig();
426
+ if ($clientConfig === (false as unknown as undefined)) {
427
+ $clientConfig = undefined;
428
+ $clientConfig = await User.getClientConfig();
257
429
  }
258
430
  });
259
431
 
@@ -263,30 +435,27 @@
263
435
  });
264
436
 
265
437
  async function save() {
266
- if (user == null) {
438
+ if ($user == null) {
267
439
  return;
268
440
  }
269
441
 
270
442
  failureMessage = undefined;
271
- saving = true;
443
+ loading = true;
272
444
 
273
- if (clientConfig?.emailUsernames) {
274
- user.username = user.email;
445
+ if ($clientConfig?.emailUsernames) {
446
+ $user.username = $user.email;
275
447
  }
276
448
 
277
449
  try {
278
- if (await user.$save()) {
279
- originalEmail = user.email;
280
- open = false;
281
- usernameVerifiedMessage = undefined;
282
- emailVerifiedMessage = undefined;
283
- } else {
284
- failureMessage = 'Error saving account changes.';
285
- }
450
+ await $user.$save();
451
+ originalEmail = $user.email;
452
+ open = false;
453
+ usernameVerifiedMessage = undefined;
454
+ emailVerifiedMessage = undefined;
286
455
  } catch (e: any) {
287
456
  failureMessage = e?.message;
288
457
  }
289
- saving = false;
458
+ loading = false;
290
459
  }
291
460
 
292
461
  function checkUsername() {
@@ -298,7 +467,7 @@
298
467
  }
299
468
  usernameTimer = setTimeout(async () => {
300
469
  try {
301
- const data = await user?.$checkUsername();
470
+ const data = await $user?.$checkUsername();
302
471
  usernameVerified = data?.result ?? false;
303
472
  usernameVerifiedMessage =
304
473
  data?.message ?? 'Error getting verification.';
@@ -318,7 +487,7 @@
318
487
  }
319
488
  emailTimer = setTimeout(async () => {
320
489
  try {
321
- const data = await user?.$checkEmail();
490
+ const data = await $user?.$checkEmail();
322
491
  emailVerified = data?.result ?? false;
323
492
  emailVerifiedMessage = data?.message ?? 'Error getting verification.';
324
493
  } catch (e: any) {