@nexys/user-management-web 0.3.1

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.
Files changed (78) hide show
  1. package/README.md +61 -0
  2. package/components.json +21 -0
  3. package/package.json +66 -0
  4. package/src/components/audit/audit-log-table.test.tsx +98 -0
  5. package/src/components/audit/audit-log-table.tsx +227 -0
  6. package/src/components/auth/accept-invitation-card.tsx +104 -0
  7. package/src/components/auth/forgot-password-form.tsx +93 -0
  8. package/src/components/auth/reset-password-form.tsx +144 -0
  9. package/src/components/auth/sign-in-form.test.tsx +164 -0
  10. package/src/components/auth/sign-in-form.tsx +504 -0
  11. package/src/components/auth/sign-up-form.tsx +124 -0
  12. package/src/components/layout/app-shell.tsx +189 -0
  13. package/src/components/layout/auth-shell.tsx +28 -0
  14. package/src/components/layout/theme-toggle.tsx +55 -0
  15. package/src/components/layout/user-menu.tsx +84 -0
  16. package/src/components/primitives/empty-state.tsx +40 -0
  17. package/src/components/primitives/page-header.tsx +27 -0
  18. package/src/components/primitives/spinner.tsx +28 -0
  19. package/src/components/profile/api-tokens-card.tsx +238 -0
  20. package/src/components/profile/change-password-form.tsx +159 -0
  21. package/src/components/profile/passkeys-card.tsx +186 -0
  22. package/src/components/profile/profile-card.tsx +107 -0
  23. package/src/components/profile/refresh-tokens-list.tsx +176 -0
  24. package/src/components/profile/two-factor-card.tsx +230 -0
  25. package/src/components/projects/project-consents.tsx +577 -0
  26. package/src/components/projects/project-create-dialog.tsx +193 -0
  27. package/src/components/projects/project-detail-tabs.tsx +64 -0
  28. package/src/components/projects/project-email-templates.tsx +285 -0
  29. package/src/components/projects/project-notices.tsx +461 -0
  30. package/src/components/projects/project-settings.tsx +1059 -0
  31. package/src/components/projects/project-tenants.tsx +108 -0
  32. package/src/components/projects/projects-table.test.tsx +109 -0
  33. package/src/components/projects/projects-table.tsx +240 -0
  34. package/src/components/roles/role-badge.tsx +91 -0
  35. package/src/components/roles/role-select.tsx +76 -0
  36. package/src/components/tenants/tenant-create-dialog.tsx +164 -0
  37. package/src/components/tenants/tenant-detail-tabs.tsx +51 -0
  38. package/src/components/tenants/tenant-members.tsx +587 -0
  39. package/src/components/tenants/tenant-roles.test.tsx +107 -0
  40. package/src/components/tenants/tenant-roles.tsx +418 -0
  41. package/src/components/tenants/tenant-settings.tsx +392 -0
  42. package/src/components/tenants/tenants-table.test.tsx +81 -0
  43. package/src/components/tenants/tenants-table.tsx +247 -0
  44. package/src/components/ui/alert.tsx +52 -0
  45. package/src/components/ui/avatar.tsx +46 -0
  46. package/src/components/ui/badge.tsx +32 -0
  47. package/src/components/ui/button.tsx +64 -0
  48. package/src/components/ui/card.tsx +65 -0
  49. package/src/components/ui/dialog.tsx +99 -0
  50. package/src/components/ui/dropdown-menu.tsx +97 -0
  51. package/src/components/ui/field.tsx +44 -0
  52. package/src/components/ui/input.tsx +23 -0
  53. package/src/components/ui/label.tsx +19 -0
  54. package/src/components/ui/select.tsx +87 -0
  55. package/src/components/ui/separator.tsx +22 -0
  56. package/src/components/ui/skeleton.tsx +13 -0
  57. package/src/components/ui/switch.tsx +29 -0
  58. package/src/components/ui/table.tsx +87 -0
  59. package/src/components/ui/tabs.tsx +51 -0
  60. package/src/components/ui/textarea.tsx +22 -0
  61. package/src/components/ui/tooltip.tsx +27 -0
  62. package/src/components/users/user-create-dialog.tsx +229 -0
  63. package/src/components/users/user-delete-dialog.tsx +88 -0
  64. package/src/components/users/user-detail.tsx +818 -0
  65. package/src/components/users/users-table.test.tsx +107 -0
  66. package/src/components/users/users-table.tsx +386 -0
  67. package/src/context/auth-context.tsx +127 -0
  68. package/src/context/theme-context.test.tsx +78 -0
  69. package/src/context/theme-context.tsx +110 -0
  70. package/src/hooks/use-call.ts +60 -0
  71. package/src/index.ts +160 -0
  72. package/src/lib/error-message.ts +16 -0
  73. package/src/lib/utils.test.ts +61 -0
  74. package/src/lib/utils.ts +79 -0
  75. package/src/lib/webauthn.ts +36 -0
  76. package/src/styles.css +107 -0
  77. package/src/test-setup.ts +5 -0
  78. package/tailwind.preset.cjs +15 -0
@@ -0,0 +1,577 @@
1
+ import {
2
+ deleteConsentDocument,
3
+ listConsentAcceptances,
4
+ listConsentDocuments,
5
+ publishConsentDocument,
6
+ updateConsentDocument,
7
+ type ConsentDocumentRow,
8
+ } from "@nexys/user-management-client";
9
+ import { AlertCircle, Trash2, Users } from "lucide-react";
10
+ import { useState, type FormEvent } from "react";
11
+
12
+ import { useUmClient } from "../../context/auth-context.js";
13
+ import { useCall } from "../../hooks/use-call.js";
14
+ import { errorMessage } from "../../lib/error-message.js";
15
+ import { Alert, AlertDescription } from "../ui/alert.js";
16
+ import { Badge } from "../ui/badge.js";
17
+ import { Button } from "../ui/button.js";
18
+ import {
19
+ Card,
20
+ CardContent,
21
+ CardDescription,
22
+ CardHeader,
23
+ CardTitle,
24
+ } from "../ui/card.js";
25
+ import {
26
+ Dialog,
27
+ DialogContent,
28
+ DialogDescription,
29
+ DialogFooter,
30
+ DialogHeader,
31
+ DialogTitle,
32
+ DialogTrigger,
33
+ } from "../ui/dialog.js";
34
+ import { Field } from "../ui/field.js";
35
+ import { Input } from "../ui/input.js";
36
+ import { Label } from "../ui/label.js";
37
+ import {
38
+ Select,
39
+ SelectContent,
40
+ SelectItem,
41
+ SelectTrigger,
42
+ SelectValue,
43
+ } from "../ui/select.js";
44
+ import { Skeleton } from "../ui/skeleton.js";
45
+ import { Switch } from "../ui/switch.js";
46
+ import {
47
+ Table,
48
+ TableBody,
49
+ TableCell,
50
+ TableEmpty,
51
+ TableHead,
52
+ TableHeader,
53
+ TableRow,
54
+ } from "../ui/table.js";
55
+ import { Textarea } from "../ui/textarea.js";
56
+
57
+ export interface ProjectConsentsProps {
58
+ projectId: string;
59
+ canManage?: boolean;
60
+ }
61
+
62
+ /** Newest version first within each key; keys alphabetical. */
63
+ const groupByKey = (docs: ConsentDocumentRow[]) => {
64
+ const byKey = new Map<string, ConsentDocumentRow[]>();
65
+ for (const d of docs) {
66
+ const list = byKey.get(d.key) ?? [];
67
+ list.push(d);
68
+ byKey.set(d.key, list);
69
+ }
70
+ return [...byKey.entries()]
71
+ .map(([key, versions]) => ({
72
+ key,
73
+ versions: [...versions].sort((a, b) => b.version - a.version),
74
+ }))
75
+ .sort((a, b) => a.key.localeCompare(b.key));
76
+ };
77
+
78
+ export const ProjectConsents = ({
79
+ projectId,
80
+ canManage = false,
81
+ }: ProjectConsentsProps) => {
82
+ const { data, loading, error, refresh } = useCall(listConsentDocuments, {
83
+ projectId,
84
+ });
85
+ const groups = groupByKey(data?.documents ?? []);
86
+
87
+ if (error) {
88
+ return (
89
+ <Alert variant="destructive">
90
+ <AlertCircle />
91
+ <AlertDescription>{errorMessage(error)}</AlertDescription>
92
+ </Alert>
93
+ );
94
+ }
95
+
96
+ return (
97
+ <div className="grid gap-6">
98
+ <Card>
99
+ <CardHeader>
100
+ <CardTitle>Agreements</CardTitle>
101
+ <CardDescription>
102
+ Terms, privacy notices and opt-ins users accept. Publishing new
103
+ wording adds a version — the old text is kept so past acceptances
104
+ still say what was agreed to.
105
+ </CardDescription>
106
+ </CardHeader>
107
+ <CardContent className="grid gap-4">
108
+ {loading ? (
109
+ <Skeleton className="h-24 w-full" />
110
+ ) : groups.length === 0 ? (
111
+ <p className="text-muted-foreground text-sm">
112
+ No agreements yet. Publish one to start collecting consent.
113
+ </p>
114
+ ) : (
115
+ groups.map((g) => (
116
+ <DocumentGroup
117
+ key={g.key}
118
+ projectId={projectId}
119
+ docKey={g.key}
120
+ versions={g.versions}
121
+ canManage={canManage}
122
+ onChanged={refresh}
123
+ />
124
+ ))
125
+ )}
126
+ {canManage ? (
127
+ <PublishDialog
128
+ projectId={projectId}
129
+ existingKeys={groups.map((g) => g.key)}
130
+ onPublished={refresh}
131
+ />
132
+ ) : null}
133
+ </CardContent>
134
+ </Card>
135
+ </div>
136
+ );
137
+ };
138
+
139
+ const DocumentGroup = ({
140
+ projectId,
141
+ docKey,
142
+ versions,
143
+ canManage,
144
+ onChanged,
145
+ }: {
146
+ projectId: string;
147
+ docKey: string;
148
+ versions: ConsentDocumentRow[];
149
+ canManage: boolean;
150
+ onChanged: () => void;
151
+ }) => {
152
+ const client = useUmClient();
153
+ const [busy, setBusy] = useState<string | null>(null);
154
+ // Groups are only built from non-empty version lists.
155
+ const current = versions[0];
156
+ if (!current) return null;
157
+
158
+ const togglePublished = async (d: ConsentDocumentRow) => {
159
+ setBusy(d.id);
160
+ await client.call(updateConsentDocument, {
161
+ projectId,
162
+ documentId: d.id,
163
+ published: !d.published,
164
+ });
165
+ setBusy(null);
166
+ onChanged();
167
+ };
168
+
169
+ const remove = async (d: ConsentDocumentRow) => {
170
+ setBusy(d.id);
171
+ await client.call(deleteConsentDocument, { projectId, documentId: d.id });
172
+ setBusy(null);
173
+ onChanged();
174
+ };
175
+
176
+ return (
177
+ <div className="rounded-lg border bg-card">
178
+ <div className="flex flex-wrap items-center justify-between gap-2 border-b p-3">
179
+ <div className="grid gap-0.5">
180
+ <div className="flex items-center gap-2">
181
+ <span className="text-sm font-medium">{current.title}</span>
182
+ <Badge
183
+ variant={
184
+ current.enforcement === "blocking" ? "default" : "muted"
185
+ }
186
+ >
187
+ {current.enforcement === "blocking"
188
+ ? "blocks sign-in"
189
+ : "report only"}
190
+ </Badge>
191
+ </div>
192
+ <code className="text-muted-foreground font-mono text-xs">
193
+ {docKey} · {versions.length} version
194
+ {versions.length === 1 ? "" : "s"}
195
+ </code>
196
+ </div>
197
+ </div>
198
+ <Table>
199
+ <TableHeader>
200
+ <TableRow>
201
+ <TableHead>Version</TableHead>
202
+ <TableHead>Status</TableHead>
203
+ <TableHead>Re-accept</TableHead>
204
+ <TableHead className="text-right">Acceptances</TableHead>
205
+ {canManage ? <TableHead /> : null}
206
+ </TableRow>
207
+ </TableHeader>
208
+ <TableBody>
209
+ {versions.map((d) => (
210
+ <TableRow key={d.id}>
211
+ <TableCell className="font-mono text-xs">v{d.version}</TableCell>
212
+ <TableCell>
213
+ {canManage ? (
214
+ <div className="flex items-center gap-2">
215
+ <Switch
216
+ checked={d.published}
217
+ onCheckedChange={() => void togglePublished(d)}
218
+ disabled={busy === d.id}
219
+ aria-label="Published"
220
+ />
221
+ <span className="text-muted-foreground text-xs">
222
+ {d.published ? "published" : "draft"}
223
+ </span>
224
+ </div>
225
+ ) : (
226
+ <Badge variant={d.published ? "default" : "muted"}>
227
+ {d.published ? "published" : "draft"}
228
+ </Badge>
229
+ )}
230
+ </TableCell>
231
+ <TableCell className="text-muted-foreground text-xs">
232
+ {d.requiresReacceptance ? "required" : "not required"}
233
+ </TableCell>
234
+ <TableCell className="text-right">
235
+ <AcceptancesDialog projectId={projectId} document={d} />
236
+ </TableCell>
237
+ {canManage ? (
238
+ <TableCell className="text-right">
239
+ <Button
240
+ variant="ghost"
241
+ size="icon"
242
+ className="size-8"
243
+ onClick={() => void remove(d)}
244
+ disabled={busy === d.id}
245
+ aria-label="Delete version"
246
+ >
247
+ <Trash2 className="size-4" />
248
+ </Button>
249
+ </TableCell>
250
+ ) : null}
251
+ </TableRow>
252
+ ))}
253
+ </TableBody>
254
+ </Table>
255
+ </div>
256
+ );
257
+ };
258
+
259
+ /// Who accepted one version — the compliance view.
260
+ const AcceptancesDialog = ({
261
+ projectId,
262
+ document,
263
+ }: {
264
+ projectId: string;
265
+ document: ConsentDocumentRow;
266
+ }) => {
267
+ const [open, setOpen] = useState(false);
268
+
269
+ return (
270
+ <Dialog open={open} onOpenChange={setOpen}>
271
+ <DialogTrigger asChild>
272
+ <Button variant="ghost" size="sm">
273
+ <Users className="size-3.5" /> View
274
+ </Button>
275
+ </DialogTrigger>
276
+ <DialogContent className="max-w-2xl">
277
+ <DialogHeader>
278
+ <DialogTitle>
279
+ {document.title} — v{document.version}
280
+ </DialogTitle>
281
+ <DialogDescription>
282
+ Everyone who accepted this exact version.
283
+ </DialogDescription>
284
+ </DialogHeader>
285
+ {open ? (
286
+ <AcceptancesTable projectId={projectId} documentId={document.id} />
287
+ ) : null}
288
+ </DialogContent>
289
+ </Dialog>
290
+ );
291
+ };
292
+
293
+ const AcceptancesTable = ({
294
+ projectId,
295
+ documentId,
296
+ }: {
297
+ projectId: string;
298
+ documentId: string;
299
+ }) => {
300
+ const { data, loading, error } = useCall(listConsentAcceptances, {
301
+ projectId,
302
+ documentId,
303
+ });
304
+
305
+ if (error) {
306
+ return (
307
+ <Alert variant="destructive">
308
+ <AlertCircle />
309
+ <AlertDescription>{errorMessage(error)}</AlertDescription>
310
+ </Alert>
311
+ );
312
+ }
313
+ if (loading) {
314
+ return <Skeleton className="h-24 w-full" />;
315
+ }
316
+
317
+ const rows = data?.acceptances ?? [];
318
+ return (
319
+ <div className="max-h-96 overflow-y-auto">
320
+ <Table>
321
+ <TableHeader>
322
+ <TableRow>
323
+ <TableHead>User</TableHead>
324
+ <TableHead>Accepted</TableHead>
325
+ <TableHead>IP</TableHead>
326
+ </TableRow>
327
+ </TableHeader>
328
+ <TableBody>
329
+ {rows.length === 0 ? (
330
+ <TableEmpty>Nobody has accepted this version yet.</TableEmpty>
331
+ ) : (
332
+ rows.map((a) => (
333
+ <TableRow key={a.userId}>
334
+ <TableCell>
335
+ <div className="grid gap-0.5">
336
+ <span className="text-sm">{a.userName}</span>
337
+ <span className="text-muted-foreground text-xs">
338
+ {a.userEmail}
339
+ </span>
340
+ </div>
341
+ </TableCell>
342
+ <TableCell className="text-xs">
343
+ {new Date(a.acceptedAt).toLocaleString()}
344
+ </TableCell>
345
+ <TableCell className="font-mono text-xs">
346
+ {a.ipAddress ?? "—"}
347
+ </TableCell>
348
+ </TableRow>
349
+ ))
350
+ )}
351
+ </TableBody>
352
+ </Table>
353
+ {data ? (
354
+ <p className="text-muted-foreground mt-2 text-xs">
355
+ {rows.length} of {data.total}
356
+ </p>
357
+ ) : null}
358
+ </div>
359
+ );
360
+ };
361
+
362
+ const PublishDialog = ({
363
+ projectId,
364
+ existingKeys,
365
+ onPublished,
366
+ }: {
367
+ projectId: string;
368
+ existingKeys: string[];
369
+ onPublished: () => void;
370
+ }) => {
371
+ const client = useUmClient();
372
+ const [open, setOpen] = useState(false);
373
+ const [key, setKey] = useState("");
374
+ const [title, setTitle] = useState("");
375
+ const [body, setBody] = useState("");
376
+ const [url, setUrl] = useState("");
377
+ const [enforcement, setEnforcement] = useState<"blocking" | "report_only">(
378
+ "blocking"
379
+ );
380
+ const [requiresReacceptance, setRequiresReacceptance] = useState(true);
381
+ const [published, setPublished] = useState(true);
382
+ const [loading, setLoading] = useState(false);
383
+ const [error, setError] = useState<string | null>(null);
384
+
385
+ const isNewVersion = existingKeys.includes(key.trim().toLowerCase());
386
+
387
+ const reset = () => {
388
+ setKey("");
389
+ setTitle("");
390
+ setBody("");
391
+ setUrl("");
392
+ setEnforcement("blocking");
393
+ setRequiresReacceptance(true);
394
+ setPublished(true);
395
+ setError(null);
396
+ setLoading(false);
397
+ };
398
+
399
+ const submit = async (e: FormEvent) => {
400
+ e.preventDefault();
401
+ setLoading(true);
402
+ setError(null);
403
+ const res = await client.call(publishConsentDocument, {
404
+ projectId,
405
+ key: key.trim().toLowerCase(),
406
+ title: title.trim(),
407
+ body,
408
+ url: url.trim() || null,
409
+ enforcement,
410
+ requiresReacceptance,
411
+ published,
412
+ });
413
+ setLoading(false);
414
+ if (res.ok) {
415
+ onPublished();
416
+ reset();
417
+ setOpen(false);
418
+ } else {
419
+ setError(errorMessage(res.error, "Couldn't publish this agreement."));
420
+ }
421
+ };
422
+
423
+ return (
424
+ <Dialog
425
+ open={open}
426
+ onOpenChange={(o) => {
427
+ setOpen(o);
428
+ if (!o) reset();
429
+ }}
430
+ >
431
+ <DialogTrigger asChild>
432
+ <Button variant="outline" size="sm">
433
+ Publish agreement
434
+ </Button>
435
+ </DialogTrigger>
436
+ <DialogContent className="max-w-xl">
437
+ <DialogHeader>
438
+ <DialogTitle>Publish an agreement</DialogTitle>
439
+ <DialogDescription>
440
+ Reusing an existing key publishes the next version of it. Text is
441
+ never edited in place, so past acceptances stay meaningful.
442
+ </DialogDescription>
443
+ </DialogHeader>
444
+ <form onSubmit={submit} className="grid gap-4" noValidate>
445
+ <div className="grid gap-4 sm:grid-cols-2">
446
+ <Field
447
+ label="Key"
448
+ htmlFor="consent-key"
449
+ description="Stable id across versions, e.g. terms."
450
+ required
451
+ >
452
+ <Input
453
+ id="consent-key"
454
+ value={key}
455
+ onChange={(e) => setKey(e.target.value)}
456
+ placeholder="terms"
457
+ className="font-mono"
458
+ list="consent-key-options"
459
+ />
460
+ <datalist id="consent-key-options">
461
+ {existingKeys.map((k) => (
462
+ <option key={k} value={k} />
463
+ ))}
464
+ </datalist>
465
+ </Field>
466
+ <Field label="Title" htmlFor="consent-title" required>
467
+ <Input
468
+ id="consent-title"
469
+ value={title}
470
+ onChange={(e) => setTitle(e.target.value)}
471
+ placeholder="Terms of Service"
472
+ />
473
+ </Field>
474
+ </div>
475
+ {isNewVersion ? (
476
+ <p className="text-muted-foreground text-xs">
477
+ <strong className="font-mono">{key.trim().toLowerCase()}</strong>{" "}
478
+ already exists — this publishes its next version.
479
+ </p>
480
+ ) : null}
481
+ <Field label="Body" htmlFor="consent-body" required>
482
+ <Textarea
483
+ id="consent-body"
484
+ value={body}
485
+ onChange={(e) => setBody(e.target.value)}
486
+ rows={8}
487
+ className="text-xs"
488
+ />
489
+ </Field>
490
+ <Field
491
+ label="Link"
492
+ htmlFor="consent-url"
493
+ description="Optional link to the canonical hosted document."
494
+ >
495
+ <Input
496
+ id="consent-url"
497
+ value={url}
498
+ onChange={(e) => setUrl(e.target.value)}
499
+ placeholder="https://example.com/terms"
500
+ className="font-mono"
501
+ />
502
+ </Field>
503
+ <Field
504
+ label="Enforcement"
505
+ htmlFor="consent-enforcement"
506
+ description="Blocking withholds sign-in until the user accepts."
507
+ >
508
+ <Select
509
+ value={enforcement}
510
+ onValueChange={(v) =>
511
+ setEnforcement(v as "blocking" | "report_only")
512
+ }
513
+ >
514
+ <SelectTrigger id="consent-enforcement">
515
+ <SelectValue />
516
+ </SelectTrigger>
517
+ <SelectContent>
518
+ <SelectItem value="blocking">Blocks sign-in</SelectItem>
519
+ <SelectItem value="report_only">Report only</SelectItem>
520
+ </SelectContent>
521
+ </Select>
522
+ </Field>
523
+ <div className="flex items-center justify-between gap-4">
524
+ <div className="grid gap-0.5">
525
+ <Label htmlFor="consent-reaccept">Requires re-acceptance</Label>
526
+ <p className="text-muted-foreground text-xs">
527
+ On for a material change — everyone who accepted an earlier
528
+ version is asked again. Off for a typo fix.
529
+ </p>
530
+ </div>
531
+ <Switch
532
+ id="consent-reaccept"
533
+ checked={requiresReacceptance}
534
+ onCheckedChange={setRequiresReacceptance}
535
+ />
536
+ </div>
537
+ <div className="flex items-center justify-between gap-4">
538
+ <div className="grid gap-0.5">
539
+ <Label htmlFor="consent-published">Publish now</Label>
540
+ <p className="text-muted-foreground text-xs">
541
+ Off saves it as a draft: never shown, never enforced.
542
+ </p>
543
+ </div>
544
+ <Switch
545
+ id="consent-published"
546
+ checked={published}
547
+ onCheckedChange={setPublished}
548
+ />
549
+ </div>
550
+ {error ? (
551
+ <Alert variant="destructive">
552
+ <AlertCircle />
553
+ <AlertDescription>{error}</AlertDescription>
554
+ </Alert>
555
+ ) : null}
556
+ <DialogFooter>
557
+ <Button
558
+ type="button"
559
+ variant="ghost"
560
+ onClick={() => setOpen(false)}
561
+ disabled={loading}
562
+ >
563
+ Cancel
564
+ </Button>
565
+ <Button
566
+ type="submit"
567
+ loading={loading}
568
+ disabled={!key.trim() || !title.trim() || !body.trim()}
569
+ >
570
+ Publish
571
+ </Button>
572
+ </DialogFooter>
573
+ </form>
574
+ </DialogContent>
575
+ </Dialog>
576
+ );
577
+ };