@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,392 @@
1
+ import {
2
+ adminChangeTenantId,
3
+ adminDeleteTenant,
4
+ getFullTenant,
5
+ listProjects,
6
+ updateTenant,
7
+ } from "@nexys/user-management-client";
8
+ import { AlertCircle, Check, Copy, Save, Trash2 } from "lucide-react";
9
+ import { useEffect, useState, type FormEvent } from "react";
10
+
11
+ import { useUmClient } from "../../context/auth-context.js";
12
+ import { useCall } from "../../hooks/use-call.js";
13
+ import { errorMessage } from "../../lib/error-message.js";
14
+ import { Alert, AlertDescription } from "../ui/alert.js";
15
+ import { Button } from "../ui/button.js";
16
+ import {
17
+ Card,
18
+ CardContent,
19
+ CardDescription,
20
+ CardHeader,
21
+ CardTitle,
22
+ } from "../ui/card.js";
23
+ import {
24
+ Dialog,
25
+ DialogClose,
26
+ DialogContent,
27
+ DialogDescription,
28
+ DialogFooter,
29
+ DialogHeader,
30
+ DialogTitle,
31
+ DialogTrigger,
32
+ } from "../ui/dialog.js";
33
+ import { Field } from "../ui/field.js";
34
+ import { Input } from "../ui/input.js";
35
+ import { Label } from "../ui/label.js";
36
+ import { Skeleton } from "../ui/skeleton.js";
37
+
38
+ export interface TenantSettingsProps {
39
+ organizationId: string;
40
+ /** Allow editing name/slug (org owner/admin or superadmin). */
41
+ canManage?: boolean;
42
+ canDelete?: boolean;
43
+ onDeleted?: () => void;
44
+ /** Called with the new uuid after a successful id change. */
45
+ onIdChanged?: (newId: string) => void;
46
+ }
47
+
48
+ export const TenantSettings = ({
49
+ organizationId,
50
+ canManage = false,
51
+ canDelete = false,
52
+ onDeleted,
53
+ onIdChanged,
54
+ }: TenantSettingsProps) => {
55
+ const client = useUmClient();
56
+ const { data, loading, error, refresh } = useCall(getFullTenant, {
57
+ organizationId,
58
+ });
59
+ // Resolve projectId → product name (superadmin sees all projects;
60
+ // for others this may miss, in which case the raw id is shown).
61
+ const projects = useCall(listProjects);
62
+ const productName = data?.projectId
63
+ ? (projects.data?.projects ?? []).find((p) => p.id === data.projectId)?.name
64
+ : undefined;
65
+ const [copied, setCopied] = useState(false);
66
+ const [deleting, setDeleting] = useState(false);
67
+ const [deleteError, setDeleteError] = useState<string | null>(null);
68
+
69
+ const copyId = async () => {
70
+ await navigator.clipboard.writeText(organizationId);
71
+ setCopied(true);
72
+ setTimeout(() => setCopied(false), 2000);
73
+ };
74
+
75
+ const handleDelete = async () => {
76
+ setDeleting(true);
77
+ setDeleteError(null);
78
+ const res = await client.call(adminDeleteTenant, { id: organizationId });
79
+ setDeleting(false);
80
+ if (res.ok) {
81
+ onDeleted?.();
82
+ } else {
83
+ setDeleteError(errorMessage(res.error, "Failed to delete tenant."));
84
+ }
85
+ };
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>General</CardTitle>
101
+ <CardDescription>
102
+ {canManage ? "Update tenant information" : "Tenant information"}
103
+ </CardDescription>
104
+ </CardHeader>
105
+ <CardContent className="grid gap-4">
106
+ {loading ? (
107
+ <>
108
+ <Skeleton className="h-5 w-48" />
109
+ <Skeleton className="h-5 w-32" />
110
+ <Skeleton className="h-5 w-64" />
111
+ </>
112
+ ) : data ? (
113
+ <>
114
+ {canManage ? (
115
+ <GeneralForm
116
+ organizationId={organizationId}
117
+ name={data.name}
118
+ slug={data.slug}
119
+ onSaved={refresh}
120
+ />
121
+ ) : (
122
+ <>
123
+ <div className="grid gap-1">
124
+ <Label className="text-muted-foreground text-xs">Name</Label>
125
+ <p className="text-sm font-medium">{data.name}</p>
126
+ </div>
127
+ <div className="grid gap-1">
128
+ <Label className="text-muted-foreground text-xs">Slug</Label>
129
+ <p className="font-mono text-sm">{data.slug}</p>
130
+ </div>
131
+ </>
132
+ )}
133
+ {data.projectId ? (
134
+ <div className="grid gap-1">
135
+ <Label className="text-muted-foreground text-xs">Product</Label>
136
+ <p className="text-sm font-medium">
137
+ {productName ?? <code className="font-mono text-xs">{data.projectId}</code>}
138
+ </p>
139
+ </div>
140
+ ) : null}
141
+ <div className="grid gap-1">
142
+ <Label className="text-muted-foreground text-xs">UUID</Label>
143
+ <div className="flex items-center gap-2">
144
+ <code className="rounded bg-muted px-2 py-1 font-mono text-xs">
145
+ {organizationId}
146
+ </code>
147
+ <Button
148
+ variant="ghost"
149
+ size="icon"
150
+ className="size-7"
151
+ onClick={copyId}
152
+ aria-label="Copy UUID"
153
+ >
154
+ {copied ? (
155
+ <Check className="size-3.5 text-green-600" />
156
+ ) : (
157
+ <Copy className="size-3.5" />
158
+ )}
159
+ </Button>
160
+ </div>
161
+ </div>
162
+ </>
163
+ ) : null}
164
+ </CardContent>
165
+ </Card>
166
+
167
+ {canDelete && (
168
+ <Card className="border-destructive/50">
169
+ <CardHeader>
170
+ <CardTitle className="text-destructive">Danger zone</CardTitle>
171
+ <CardDescription>
172
+ Deleting a tenant removes all its members, roles and data. This
173
+ action cannot be undone.
174
+ </CardDescription>
175
+ </CardHeader>
176
+ <CardContent className="grid gap-6">
177
+ <ChangeIdForm
178
+ organizationId={organizationId}
179
+ onChanged={onIdChanged}
180
+ />
181
+
182
+ {deleteError && (
183
+ <Alert variant="destructive" className="mb-4">
184
+ <AlertCircle />
185
+ <AlertDescription>{deleteError}</AlertDescription>
186
+ </Alert>
187
+ )}
188
+ <Dialog>
189
+ <DialogTrigger asChild>
190
+ <Button variant="destructive">
191
+ <Trash2 /> Delete tenant
192
+ </Button>
193
+ </DialogTrigger>
194
+ <DialogContent>
195
+ <DialogHeader>
196
+ <DialogTitle>Delete tenant?</DialogTitle>
197
+ <DialogDescription>
198
+ This will permanently delete{" "}
199
+ <strong>{data?.name ?? "this tenant"}</strong> and all
200
+ associated data. This action cannot be undone.
201
+ </DialogDescription>
202
+ </DialogHeader>
203
+ <DialogFooter>
204
+ <DialogClose asChild>
205
+ <Button variant="outline">Cancel</Button>
206
+ </DialogClose>
207
+ <Button
208
+ variant="destructive"
209
+ onClick={handleDelete}
210
+ loading={deleting}
211
+ >
212
+ Delete permanently
213
+ </Button>
214
+ </DialogFooter>
215
+ </DialogContent>
216
+ </Dialog>
217
+ </CardContent>
218
+ </Card>
219
+ )}
220
+ </div>
221
+ );
222
+ };
223
+
224
+ const GeneralForm = ({
225
+ organizationId,
226
+ name: initialName,
227
+ slug: initialSlug,
228
+ onSaved,
229
+ }: {
230
+ organizationId: string;
231
+ name: string;
232
+ slug: string;
233
+ onSaved: () => void;
234
+ }) => {
235
+ const client = useUmClient();
236
+ const [name, setName] = useState(initialName);
237
+ const [slug, setSlug] = useState(initialSlug);
238
+ const [saving, setSaving] = useState(false);
239
+ const [saved, setSaved] = useState(false);
240
+ const [error, setError] = useState<string | null>(null);
241
+
242
+ // Server data refreshed underneath us (e.g. another tab saved) —
243
+ // resync untouched fields.
244
+ useEffect(() => setName(initialName), [initialName]);
245
+ useEffect(() => setSlug(initialSlug), [initialSlug]);
246
+
247
+ const dirty = name.trim() !== initialName || slug.trim() !== initialSlug;
248
+
249
+ const submit = async (e: FormEvent) => {
250
+ e.preventDefault();
251
+ setSaving(true);
252
+ setError(null);
253
+ setSaved(false);
254
+ const res = await client.call(updateTenant, {
255
+ organizationId,
256
+ data: { name: name.trim(), slug: slug.trim() },
257
+ });
258
+ setSaving(false);
259
+ if (res.ok) {
260
+ setSaved(true);
261
+ setTimeout(() => setSaved(false), 2000);
262
+ onSaved();
263
+ } else {
264
+ setError(errorMessage(res.error, "Couldn't save tenant settings."));
265
+ }
266
+ };
267
+
268
+ return (
269
+ <form onSubmit={submit} className="grid gap-4">
270
+ {error ? (
271
+ <Alert variant="destructive">
272
+ <AlertCircle />
273
+ <AlertDescription>{error}</AlertDescription>
274
+ </Alert>
275
+ ) : null}
276
+ <div className="grid gap-4 sm:grid-cols-2">
277
+ <Field label="Name" htmlFor="tenant-name">
278
+ <Input
279
+ id="tenant-name"
280
+ required
281
+ value={name}
282
+ onChange={(e) => setName(e.target.value)}
283
+ />
284
+ </Field>
285
+ <Field label="Slug" htmlFor="tenant-slug">
286
+ <Input
287
+ id="tenant-slug"
288
+ required
289
+ value={slug}
290
+ onChange={(e) => setSlug(e.target.value)}
291
+ className="font-mono"
292
+ />
293
+ </Field>
294
+ </div>
295
+ <div className="flex items-center gap-3">
296
+ <Button type="submit" loading={saving} disabled={!dirty || !name.trim() || !slug.trim()}>
297
+ {saved ? <Check /> : <Save />}
298
+ {saved ? "Saved" : "Save changes"}
299
+ </Button>
300
+ </div>
301
+ </form>
302
+ );
303
+ };
304
+
305
+ // Superadmin-only uuid rewrite — members, invitations, roles and
306
+ // live sessions are repointed server-side in one transaction. Meant
307
+ // for aligning the tenant uuid with an external system's key.
308
+ const ChangeIdForm = ({
309
+ organizationId,
310
+ onChanged,
311
+ }: {
312
+ organizationId: string;
313
+ onChanged?: (newId: string) => void;
314
+ }) => {
315
+ const client = useUmClient();
316
+ const [newId, setNewId] = useState("");
317
+ const [busy, setBusy] = useState(false);
318
+ const [error, setError] = useState<string | null>(null);
319
+
320
+ const valid =
321
+ /^[A-Za-z0-9_-]{1,64}$/.test(newId.trim()) && newId.trim() !== organizationId;
322
+
323
+ const submit = async () => {
324
+ setBusy(true);
325
+ setError(null);
326
+ const res = await client.call(adminChangeTenantId, {
327
+ id: organizationId,
328
+ newId: newId.trim(),
329
+ });
330
+ setBusy(false);
331
+ if (res.ok) {
332
+ onChanged?.(res.data.id);
333
+ } else {
334
+ setError(errorMessage(res.error, "Couldn't change the tenant UUID."));
335
+ }
336
+ };
337
+
338
+ return (
339
+ <div className="grid gap-2">
340
+ <Label htmlFor="tenant-new-id">Change UUID</Label>
341
+ <p className="text-muted-foreground text-xs">
342
+ Rewrites the tenant's uuid and repoints members, roles and live
343
+ sessions. Use it to match an external system's key. Signed-in
344
+ users of this tenant may see errors until their next token
345
+ refresh.
346
+ </p>
347
+ {error ? (
348
+ <Alert variant="destructive">
349
+ <AlertCircle />
350
+ <AlertDescription>{error}</AlertDescription>
351
+ </Alert>
352
+ ) : null}
353
+ <div className="flex items-center gap-2">
354
+ <Input
355
+ id="tenant-new-id"
356
+ value={newId}
357
+ onChange={(e) => setNewId(e.target.value)}
358
+ placeholder="new-uuid"
359
+ className="max-w-sm font-mono"
360
+ />
361
+ <Dialog>
362
+ <DialogTrigger asChild>
363
+ <Button variant="destructive" disabled={!valid || busy}>
364
+ Change UUID
365
+ </Button>
366
+ </DialogTrigger>
367
+ <DialogContent>
368
+ <DialogHeader>
369
+ <DialogTitle>Change tenant UUID?</DialogTitle>
370
+ <DialogDescription>
371
+ The tenant id becomes{" "}
372
+ <code className="font-mono text-xs">{newId.trim()}</code>. Every
373
+ reference is repointed; external systems keyed on the old uuid
374
+ must be updated in the same maintenance window.
375
+ </DialogDescription>
376
+ </DialogHeader>
377
+ <DialogFooter>
378
+ <DialogClose asChild>
379
+ <Button variant="outline">Cancel</Button>
380
+ </DialogClose>
381
+ <DialogClose asChild>
382
+ <Button variant="destructive" onClick={() => void submit()} loading={busy}>
383
+ Change UUID
384
+ </Button>
385
+ </DialogClose>
386
+ </DialogFooter>
387
+ </DialogContent>
388
+ </Dialog>
389
+ </div>
390
+ </div>
391
+ );
392
+ };
@@ -0,0 +1,81 @@
1
+ /// <reference lib="dom" />
2
+ import { afterEach, describe, expect, mock, test } from "bun:test";
3
+
4
+ import { render, screen, waitFor, within } from "@testing-library/react";
5
+
6
+ import { createClient } from "@nexys/user-management-client";
7
+
8
+ import { AuthProvider } from "../../context/auth-context.js";
9
+ import { TenantsTable } from "./tenants-table.js";
10
+
11
+ // Deliberately out of order and mixed case: the table must render them
12
+ // alphabetically A–Z regardless of what the server sends.
13
+ const TENANTS = [
14
+ { id: "t1", name: "zeta", slug: "zeta", projectId: null, createdAt: "2026-03-01T00:00:00Z", memberCount: 4 },
15
+ { id: "t2", name: "Alpha", slug: "alpha", projectId: null, createdAt: "2026-01-01T00:00:00Z", memberCount: 1 },
16
+ { id: "t3", name: "beta", slug: "beta", projectId: null, createdAt: "2026-02-01T00:00:00Z", memberCount: 9 },
17
+ ];
18
+
19
+ afterEach(() => {
20
+ document.body.innerHTML = "";
21
+ });
22
+
23
+ const clientFor = () => {
24
+ const fetcher = mock(async (input: RequestInfo | URL) => {
25
+ const url = input.toString();
26
+ if (url.endsWith("/api/me")) {
27
+ return new Response(JSON.stringify({ code: "unauth" }), { status: 401 });
28
+ }
29
+ if (url.includes("/api/admin/tenants")) {
30
+ return new Response(JSON.stringify({ tenants: TENANTS }), {
31
+ status: 200,
32
+ headers: { "content-type": "application/json" },
33
+ });
34
+ }
35
+ if (url.endsWith("/api/projects")) {
36
+ return new Response(JSON.stringify({ projects: [] }), {
37
+ status: 200,
38
+ headers: { "content-type": "application/json" },
39
+ });
40
+ }
41
+ return new Response("nope", { status: 404 });
42
+ });
43
+ return createClient({
44
+ baseUrl: "https://auth.example.com",
45
+ fetch: fetcher as unknown as typeof fetch,
46
+ });
47
+ };
48
+
49
+ describe("<TenantsTable />", () => {
50
+ test("renders tenants alphabetically A–Z by default", async () => {
51
+ render(
52
+ <AuthProvider client={clientFor()} refreshIntervalMs={0}>
53
+ <TenantsTable allowCreate={false} />
54
+ </AuthProvider>
55
+ );
56
+
57
+ await waitFor(() => {
58
+ expect(screen.getByText("Alpha")).toBeTruthy();
59
+ });
60
+
61
+ const rows = screen.getAllByRole("row").slice(1); // drop the header
62
+ const rowText = rows.map((r) => r.textContent ?? "");
63
+ // Case-insensitive: "beta" sits between "Alpha" and "zeta".
64
+ expect(rowText[0]).toContain("Alpha");
65
+ expect(rowText[1]).toContain("beta");
66
+ expect(rowText[2]).toContain("zeta");
67
+ });
68
+
69
+ test("shows the product filter and sort controls in the toolbar", async () => {
70
+ render(
71
+ <AuthProvider client={clientFor()} refreshIntervalMs={0}>
72
+ <TenantsTable allowCreate={false} />
73
+ </AuthProvider>
74
+ );
75
+
76
+ await waitFor(() => {
77
+ expect(screen.getByLabelText("Sort by")).toBeTruthy();
78
+ });
79
+ expect(screen.getByLabelText("Filter by product")).toBeTruthy();
80
+ });
81
+ });