@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
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # `@nexys/user-management-web`
2
+
3
+ Modern React + Tailwind v4 + shadcn (new-york / stone) UI kit for the
4
+ `user-management-rs` service. Depends on `@nexys/user-management-client`
5
+ via the workspace protocol — both packages live in this repo.
6
+
7
+ ## Surface (six core domains)
8
+
9
+ - **Auth**: `<SignInForm />`, `<SignUpForm />`, `<ForgotPasswordForm />`
10
+ - **Profile**: `<ProfileCard />`, `<ChangePasswordForm />`, `<RefreshTokensList />`
11
+ - **Users (admin)**: `<UsersTable />` — search, status flips, role select, token revoke
12
+ - **Tenants**: `<TenantsTable />`, `<TenantCreateDialog />`, `<TenantMembers />` (invite + role)
13
+ - **Roles**: `<UserRoleBadge />`, `<TenantRoleBadge />`, `<StatusBadge />`, `<UserRoleSelect />`, `<TenantRoleSelect />`
14
+ - **Layout**: `<AppShell />` (sidebar + topbar), `<AuthShell />` (centred), `<UserMenu />`
15
+
16
+ Plus the shadcn primitives the kit composes from (button, input, card,
17
+ dialog, select, dropdown-menu, tabs, alert, badge, avatar, table,
18
+ skeleton, switch, tooltip, separator, label, field).
19
+
20
+ ## Usage
21
+
22
+ ```tsx
23
+ import {
24
+ AppShell,
25
+ AuthProvider,
26
+ UmClient,
27
+ UserMenu,
28
+ UsersTable,
29
+ } from "@nexys/user-management-web";
30
+ import "@nexys/user-management-web/styles.css";
31
+
32
+ const client = new UmClient({ baseUrl: import.meta.env.VITE_API_URL });
33
+
34
+ export const App = () => (
35
+ <AuthProvider client={client}>
36
+ <AppShell
37
+ brand="Acme"
38
+ sidebarFooter={<UserMenu />}
39
+ sections={[
40
+ { items: [{ key: "profile", label: "Profile", href: "/profile" }] },
41
+ { title: "Admin", items: [{ key: "users", label: "Users", href: "/admin/users" }] },
42
+ ]}
43
+ >
44
+ <UsersTable />
45
+ </AppShell>
46
+ </AuthProvider>
47
+ );
48
+ ```
49
+
50
+ ## Tailwind v4
51
+
52
+ Design tokens ship in `src/styles.css` (`@theme` block). Import the
53
+ stylesheet, then either include the package source in your build's
54
+ `@source` glob or use the exported preset (`./tailwind-preset`).
55
+
56
+ ## Tests
57
+
58
+ ```bash
59
+ bun test # bun + happy-dom (preloaded via bunfig.toml)
60
+ bun run typecheck
61
+ ```
@@ -0,0 +1,21 @@
1
+ {
2
+ "$schema": "https://ui.shadcn.com/schema.json",
3
+ "style": "new-york",
4
+ "rsc": false,
5
+ "tsx": true,
6
+ "tailwind": {
7
+ "config": "",
8
+ "css": "src/styles.css",
9
+ "baseColor": "stone",
10
+ "cssVariables": true,
11
+ "prefix": ""
12
+ },
13
+ "aliases": {
14
+ "components": "@/components",
15
+ "utils": "@/lib/utils",
16
+ "ui": "@/components/ui",
17
+ "lib": "@/lib",
18
+ "hooks": "@/hooks"
19
+ },
20
+ "iconLibrary": "lucide"
21
+ }
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@nexys/user-management-web",
3
+ "version": "0.3.1",
4
+ "description": "Modern, slick React + Tailwind + shadcn UI kit for the Nexys user-management service. Built on @nexys/user-management-client.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "sideEffects": [
8
+ "**/*.css"
9
+ ],
10
+ "main": "./src/index.ts",
11
+ "types": "./src/index.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./src/index.ts",
15
+ "default": "./src/index.ts"
16
+ },
17
+ "./styles.css": "./src/styles.css",
18
+ "./tailwind-preset": "./tailwind.preset.cjs"
19
+ },
20
+ "files": [
21
+ "src",
22
+ "tailwind.preset.cjs",
23
+ "components.json",
24
+ "README.md"
25
+ ],
26
+ "scripts": {
27
+ "test": "bun test",
28
+ "typecheck": "tsc --noEmit"
29
+ },
30
+ "peerDependencies": {
31
+ "react": ">=18",
32
+ "react-dom": ">=18"
33
+ },
34
+ "dependencies": {
35
+ "@hookform/resolvers": "^3.10.0",
36
+ "@nexys/user-management-client": "^0.3.1",
37
+ "@radix-ui/react-avatar": "^1.1.4",
38
+ "@radix-ui/react-dialog": "^1.1.14",
39
+ "@radix-ui/react-dropdown-menu": "^2.1.7",
40
+ "@radix-ui/react-label": "^2.1.2",
41
+ "@radix-ui/react-select": "^2.1.6",
42
+ "@radix-ui/react-separator": "^1.1.2",
43
+ "@radix-ui/react-slot": "^1.2.0",
44
+ "@radix-ui/react-switch": "^1.1.3",
45
+ "@radix-ui/react-tabs": "^1.1.3",
46
+ "@radix-ui/react-tooltip": "^1.2.0",
47
+ "class-variance-authority": "^0.7.1",
48
+ "clsx": "^2.1.1",
49
+ "lucide-react": "^0.487.0",
50
+ "qrcode.react": "^4.2.0",
51
+ "react-hook-form": "^7.54.2",
52
+ "tailwind-merge": "^3.2.0",
53
+ "zod": "^3.24.1"
54
+ },
55
+ "devDependencies": {
56
+ "@happy-dom/global-registrator": "^15.11.6",
57
+ "@testing-library/react": "^16.1.0",
58
+ "@types/bun": "^1.2.2",
59
+ "@types/react": "^18.3.5",
60
+ "@types/react-dom": "^18.3.0",
61
+ "react": "^18.3.1",
62
+ "react-dom": "^18.3.1",
63
+ "tailwindcss": "^4.1.3",
64
+ "typescript": "^5.2.2"
65
+ }
66
+ }
@@ -0,0 +1,98 @@
1
+ /// <reference lib="dom" />
2
+ import { afterEach, describe, expect, mock, test } from "bun:test";
3
+
4
+ import { render, screen, waitFor } from "@testing-library/react";
5
+
6
+ import { createClient } from "@nexys/user-management-client";
7
+
8
+ import { AuthProvider } from "../../context/auth-context.js";
9
+ import { AuditLogTable } from "./audit-log-table.js";
10
+
11
+ const ROWS = [
12
+ {
13
+ id: "a1",
14
+ action: "auth.sign_in",
15
+ actorId: "u1",
16
+ actorEmail: "johan@example.com",
17
+ actorType: "user",
18
+ resourceType: "user",
19
+ resourceId: "u1",
20
+ organizationId: null,
21
+ ipAddress: "203.0.113.7",
22
+ userAgent: "curl",
23
+ metadata: null,
24
+ success: true,
25
+ createdAt: "2026-08-01T10:00:00Z",
26
+ },
27
+ {
28
+ id: "a2",
29
+ action: "auth.sign_in",
30
+ actorId: null,
31
+ actorEmail: "intruder@example.com",
32
+ actorType: "user",
33
+ resourceType: null,
34
+ resourceId: null,
35
+ organizationId: null,
36
+ ipAddress: "198.51.100.9",
37
+ userAgent: "curl",
38
+ metadata: null,
39
+ success: false,
40
+ createdAt: "2026-08-01T09:00:00Z",
41
+ },
42
+ ];
43
+
44
+ afterEach(() => {
45
+ document.body.innerHTML = "";
46
+ });
47
+
48
+ const clientFor = (rows: unknown[], total: number) => {
49
+ const fetcher = mock(async (input: RequestInfo | URL) => {
50
+ const url = input.toString();
51
+ if (url.endsWith("/api/me")) {
52
+ return new Response(JSON.stringify({ code: "unauth" }), { status: 401 });
53
+ }
54
+ if (url.includes("/api/admin/audit-logs")) {
55
+ return new Response(JSON.stringify({ total, rows }), {
56
+ status: 200,
57
+ headers: { "content-type": "application/json" },
58
+ });
59
+ }
60
+ return new Response("nope", { status: 404 });
61
+ });
62
+ return createClient({
63
+ baseUrl: "https://auth.example.com",
64
+ fetch: fetcher as unknown as typeof fetch,
65
+ });
66
+ };
67
+
68
+ describe("<AuditLogTable />", () => {
69
+ test("renders log rows with actor, ip and result", async () => {
70
+ render(
71
+ <AuthProvider client={clientFor(ROWS, 2)} refreshIntervalMs={0}>
72
+ <AuditLogTable />
73
+ </AuthProvider>
74
+ );
75
+
76
+ await waitFor(() => {
77
+ expect(screen.getByText("johan@example.com")).toBeTruthy();
78
+ });
79
+ expect(screen.getAllByText("auth.sign_in")).toHaveLength(2);
80
+ expect(screen.getByText("203.0.113.7")).toBeTruthy();
81
+ expect(screen.getByText("ok")).toBeTruthy();
82
+ expect(screen.getByText("failed")).toBeTruthy();
83
+ expect(screen.getByText("1–2 of 2")).toBeTruthy();
84
+ });
85
+
86
+ test("shows the empty state when there are no entries", async () => {
87
+ render(
88
+ <AuthProvider client={clientFor([], 0)} refreshIntervalMs={0}>
89
+ <AuditLogTable />
90
+ </AuthProvider>
91
+ );
92
+
93
+ await waitFor(() => {
94
+ expect(screen.getByText("No log entries yet")).toBeTruthy();
95
+ });
96
+ expect(screen.getByText("0 entries")).toBeTruthy();
97
+ });
98
+ });
@@ -0,0 +1,227 @@
1
+ import { adminListAuditLogs } from "@nexys/user-management-client";
2
+ import {
3
+ AlertCircle,
4
+ ChevronLeft,
5
+ ChevronRight,
6
+ RefreshCcw,
7
+ ScrollText,
8
+ Search,
9
+ } from "lucide-react";
10
+ import { useState } from "react";
11
+
12
+ import { useCall } from "../../hooks/use-call.js";
13
+ import { errorMessage } from "../../lib/error-message.js";
14
+ import { formatRelative } from "../../lib/utils.js";
15
+ import { EmptyState } from "../primitives/empty-state.js";
16
+ import { Alert, AlertDescription } from "../ui/alert.js";
17
+ import { Badge } from "../ui/badge.js";
18
+ import { Button } from "../ui/button.js";
19
+ import { Input } from "../ui/input.js";
20
+ import { Skeleton } from "../ui/skeleton.js";
21
+ import {
22
+ Table,
23
+ TableBody,
24
+ TableCell,
25
+ TableHead,
26
+ TableHeader,
27
+ TableRow,
28
+ } from "../ui/table.js";
29
+
30
+ const PAGE_SIZE = 50;
31
+
32
+ /** What sign-in rows carry in `metadata` (see the server's auth routes). */
33
+ interface AuthMetadata {
34
+ method?: string;
35
+ secondFactor?: string;
36
+ provider?: string;
37
+ }
38
+
39
+ const METHOD_LABELS: Record<string, string> = {
40
+ password: "Password",
41
+ passkey: "Passkey",
42
+ social: "Social",
43
+ magic_link: "Magic link",
44
+ };
45
+
46
+ /**
47
+ * How the account authenticated, for rows that record it — sign-ins
48
+ * carry `{ method, secondFactor?, provider? }`. Returns null for the
49
+ * many actions that aren't authentications, so the cell stays quiet
50
+ * rather than showing a dash for every admin action.
51
+ */
52
+ const authMethod = (metadata: unknown): string | null => {
53
+ if (typeof metadata !== "object" || metadata === null) return null;
54
+ const m = metadata as AuthMetadata;
55
+ if (typeof m.method !== "string") return null;
56
+
57
+ const base = METHOD_LABELS[m.method] ?? m.method;
58
+ if (m.method === "social" && m.provider) {
59
+ return `${base} · ${m.provider}`;
60
+ }
61
+ if (m.secondFactor) {
62
+ // The second factor is the interesting part of a 2FA sign-in.
63
+ const factor = m.secondFactor === "backup_code" ? "backup code" : m.secondFactor;
64
+ return `${base} + ${factor}`;
65
+ }
66
+ return base;
67
+ };
68
+
69
+ /**
70
+ * System-wide access log (superadmin). Server-side filtering by action
71
+ * prefix (e.g. `auth.`, `user.`, `tenant.`) plus offset pagination.
72
+ */
73
+ export const AuditLogTable = () => {
74
+ // The input is applied on Enter/blur via `filter` so typing doesn't
75
+ // refetch on every keystroke.
76
+ const [query, setQuery] = useState("");
77
+ const [filter, setFilter] = useState("");
78
+ const [offset, setOffset] = useState(0);
79
+ const { data, error, loading, refresh } = useCall(adminListAuditLogs, {
80
+ actionPrefix: filter || undefined,
81
+ limit: PAGE_SIZE,
82
+ offset,
83
+ });
84
+
85
+ const rows = data?.rows ?? [];
86
+ const total = data?.total ?? 0;
87
+ const applyFilter = () => {
88
+ setOffset(0);
89
+ setFilter(query.trim());
90
+ };
91
+
92
+ return (
93
+ <div className="grid gap-3">
94
+ <div className="flex flex-wrap items-center justify-between gap-2">
95
+ <div className="relative min-w-[12rem] max-w-sm flex-1">
96
+ <Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
97
+ <Input
98
+ value={query}
99
+ onChange={(e) => setQuery(e.target.value)}
100
+ onBlur={applyFilter}
101
+ onKeyDown={(e) => {
102
+ if (e.key === "Enter") applyFilter();
103
+ }}
104
+ placeholder="Filter by action prefix (auth., user., …)"
105
+ className="pl-9"
106
+ />
107
+ </div>
108
+ <Button variant="outline" size="sm" onClick={refresh}>
109
+ <RefreshCcw />
110
+ Refresh
111
+ </Button>
112
+ </div>
113
+
114
+ {error ? (
115
+ <Alert variant="destructive">
116
+ <AlertCircle />
117
+ <AlertDescription>{errorMessage(error)}</AlertDescription>
118
+ </Alert>
119
+ ) : null}
120
+
121
+ <div className="overflow-hidden rounded-xl border bg-card">
122
+ <Table>
123
+ <TableHeader>
124
+ <TableRow>
125
+ <TableHead>When</TableHead>
126
+ <TableHead>Action</TableHead>
127
+ <TableHead>Method</TableHead>
128
+ <TableHead>Actor</TableHead>
129
+ <TableHead>Resource</TableHead>
130
+ <TableHead>IP</TableHead>
131
+ <TableHead>Result</TableHead>
132
+ </TableRow>
133
+ </TableHeader>
134
+ <TableBody>
135
+ {loading ? (
136
+ Array.from({ length: 6 }).map((_, i) => (
137
+ <TableRow key={`sk-${i}`}>
138
+ <TableCell><Skeleton className="h-4 w-20" /></TableCell>
139
+ <TableCell><Skeleton className="h-4 w-32" /></TableCell>
140
+ <TableCell><Skeleton className="h-4 w-24" /></TableCell>
141
+ <TableCell><Skeleton className="h-4 w-40" /></TableCell>
142
+ <TableCell><Skeleton className="h-4 w-28" /></TableCell>
143
+ <TableCell><Skeleton className="h-4 w-24" /></TableCell>
144
+ <TableCell><Skeleton className="h-4 w-12" /></TableCell>
145
+ </TableRow>
146
+ ))
147
+ ) : rows.length === 0 ? (
148
+ <TableRow>
149
+ <TableCell colSpan={7} className="p-0">
150
+ <EmptyState
151
+ icon={ScrollText}
152
+ title={filter ? "No matching entries" : "No log entries yet"}
153
+ description={
154
+ filter
155
+ ? "Try a different action prefix."
156
+ : "Authentication and admin activity will appear here."
157
+ }
158
+ className="border-0 rounded-none bg-transparent"
159
+ />
160
+ </TableCell>
161
+ </TableRow>
162
+ ) : (
163
+ rows.map((r) => (
164
+ <TableRow key={r.id}>
165
+ <TableCell
166
+ title={r.createdAt}
167
+ className="whitespace-nowrap text-sm text-muted-foreground"
168
+ >
169
+ {formatRelative(r.createdAt)}
170
+ </TableCell>
171
+ <TableCell className="font-mono text-xs">{r.action}</TableCell>
172
+ <TableCell className="whitespace-nowrap text-xs text-muted-foreground">
173
+ {authMethod(r.metadata) ?? ""}
174
+ </TableCell>
175
+ <TableCell className="max-w-[14rem] truncate text-sm">
176
+ {r.actorEmail ?? r.actorId ?? "—"}
177
+ </TableCell>
178
+ <TableCell className="max-w-[12rem] truncate font-mono text-xs text-muted-foreground">
179
+ {r.resourceType
180
+ ? `${r.resourceType}${r.resourceId ? `:${r.resourceId}` : ""}`
181
+ : "—"}
182
+ </TableCell>
183
+ <TableCell className="whitespace-nowrap font-mono text-xs text-muted-foreground">
184
+ {r.ipAddress ?? "—"}
185
+ </TableCell>
186
+ <TableCell>
187
+ <Badge variant={r.success ? "muted" : "destructive"}>
188
+ {r.success ? "ok" : "failed"}
189
+ </Badge>
190
+ </TableCell>
191
+ </TableRow>
192
+ ))
193
+ )}
194
+ </TableBody>
195
+ </Table>
196
+ </div>
197
+
198
+ <div className="flex items-center justify-between gap-2">
199
+ <p className="text-xs text-muted-foreground">
200
+ {total > 0
201
+ ? `${offset + 1}–${Math.min(offset + PAGE_SIZE, total)} of ${total}`
202
+ : "0 entries"}
203
+ </p>
204
+ <div className="flex items-center gap-2">
205
+ <Button
206
+ variant="outline"
207
+ size="sm"
208
+ disabled={offset === 0 || loading}
209
+ onClick={() => setOffset(Math.max(0, offset - PAGE_SIZE))}
210
+ >
211
+ <ChevronLeft />
212
+ Newer
213
+ </Button>
214
+ <Button
215
+ variant="outline"
216
+ size="sm"
217
+ disabled={offset + PAGE_SIZE >= total || loading}
218
+ onClick={() => setOffset(offset + PAGE_SIZE)}
219
+ >
220
+ Older
221
+ <ChevronRight />
222
+ </Button>
223
+ </div>
224
+ </div>
225
+ </div>
226
+ );
227
+ };
@@ -0,0 +1,104 @@
1
+ import {
2
+ acceptInvitation,
3
+ setActiveTenant,
4
+ } from "@nexys/user-management-client";
5
+ import { AlertCircle, Building2, Check } from "lucide-react";
6
+ import { useState } from "react";
7
+
8
+ import { useUmClient } from "../../context/auth-context.js";
9
+ import { errorMessage } from "../../lib/error-message.js";
10
+ import { Alert, AlertDescription } from "../ui/alert.js";
11
+ import { Button } from "../ui/button.js";
12
+ import {
13
+ Card,
14
+ CardContent,
15
+ CardDescription,
16
+ CardHeader,
17
+ CardTitle,
18
+ } from "../ui/card.js";
19
+
20
+ export interface AcceptInvitationCardProps {
21
+ invitationId: string;
22
+ /** Called after the invitation is accepted and the tenant activated. */
23
+ onAccepted?: (organizationId: string) => void;
24
+ }
25
+
26
+ /**
27
+ * Accepts a tenant invitation from an emailed link. Requires the
28
+ * viewer to be signed in with the invited email — the server enforces
29
+ * the match. On success the new tenant is made active.
30
+ */
31
+ export const AcceptInvitationCard = ({
32
+ invitationId,
33
+ onAccepted,
34
+ }: AcceptInvitationCardProps) => {
35
+ const client = useUmClient();
36
+ const [loading, setLoading] = useState(false);
37
+ const [error, setError] = useState<string | null>(null);
38
+ const [accepted, setAccepted] = useState(false);
39
+
40
+ const accept = async () => {
41
+ setLoading(true);
42
+ setError(null);
43
+ const res = await client.call(acceptInvitation, { invitationId });
44
+ if (!res.ok) {
45
+ setLoading(false);
46
+ setError(
47
+ errorMessage(
48
+ res.error,
49
+ "This invitation is invalid, expired, or was sent to a different email address."
50
+ )
51
+ );
52
+ return;
53
+ }
54
+ // Make the new tenant active right away so the user lands in it.
55
+ await client.call(setActiveTenant, {
56
+ organizationId: res.data.organizationId,
57
+ });
58
+ setLoading(false);
59
+ setAccepted(true);
60
+ onAccepted?.(res.data.organizationId);
61
+ };
62
+
63
+ if (accepted) {
64
+ return (
65
+ <Card className="w-full max-w-md">
66
+ <CardHeader className="space-y-1.5">
67
+ <CardTitle className="flex items-center gap-2 text-2xl">
68
+ <Check className="size-5 text-green-600" />
69
+ You're in
70
+ </CardTitle>
71
+ <CardDescription>
72
+ The invitation was accepted and the tenant is now active.
73
+ </CardDescription>
74
+ </CardHeader>
75
+ </Card>
76
+ );
77
+ }
78
+
79
+ return (
80
+ <Card className="w-full max-w-md">
81
+ <CardHeader className="space-y-1.5">
82
+ <CardTitle className="flex items-center gap-2 text-2xl">
83
+ <Building2 className="size-5 text-muted-foreground" />
84
+ Tenant invitation
85
+ </CardTitle>
86
+ <CardDescription>
87
+ You've been invited to join a tenant. Accepting adds this account as
88
+ a member.
89
+ </CardDescription>
90
+ </CardHeader>
91
+ <CardContent className="grid gap-4">
92
+ {error ? (
93
+ <Alert variant="destructive">
94
+ <AlertCircle />
95
+ <AlertDescription>{error}</AlertDescription>
96
+ </Alert>
97
+ ) : null}
98
+ <Button onClick={accept} loading={loading} className="w-full">
99
+ Accept invitation
100
+ </Button>
101
+ </CardContent>
102
+ </Card>
103
+ );
104
+ };
@@ -0,0 +1,93 @@
1
+ import { requestPasswordReset } from "@nexys/user-management-client";
2
+ import { AlertCircle, MailCheck } from "lucide-react";
3
+ import { useState, type FormEvent, type ReactNode } from "react";
4
+
5
+ import { useUmClient } from "../../context/auth-context.js";
6
+ import { errorMessage } from "../../lib/error-message.js";
7
+ import { Alert, AlertDescription } from "../ui/alert.js";
8
+ import { Button } from "../ui/button.js";
9
+ import {
10
+ Card,
11
+ CardContent,
12
+ CardDescription,
13
+ CardHeader,
14
+ CardTitle,
15
+ } from "../ui/card.js";
16
+ import { Field } from "../ui/field.js";
17
+ import { Input } from "../ui/input.js";
18
+
19
+ export interface ForgotPasswordFormProps {
20
+ onSent?: (email: string) => void;
21
+ signInSlot?: ReactNode;
22
+ }
23
+
24
+ export const ForgotPasswordForm = ({ onSent, signInSlot }: ForgotPasswordFormProps) => {
25
+ const client = useUmClient();
26
+ const [email, setEmail] = useState("");
27
+ const [loading, setLoading] = useState(false);
28
+ const [sent, setSent] = useState(false);
29
+ const [error, setError] = useState<string | null>(null);
30
+
31
+ const submit = async (e: FormEvent) => {
32
+ e.preventDefault();
33
+ setLoading(true);
34
+ setError(null);
35
+ const trimmed = email.trim();
36
+ const res = await client.call(requestPasswordReset, { email: trimmed });
37
+ setLoading(false);
38
+ if (res.ok) {
39
+ setSent(true);
40
+ onSent?.(trimmed);
41
+ } else {
42
+ setError(errorMessage(res.error, "Could not send reset email."));
43
+ }
44
+ };
45
+
46
+ return (
47
+ <Card className="w-full max-w-md">
48
+ <CardHeader className="space-y-1.5">
49
+ <CardTitle className="text-2xl">Reset your password</CardTitle>
50
+ <CardDescription>
51
+ Enter your email and we'll send you a link to choose a new password.
52
+ </CardDescription>
53
+ </CardHeader>
54
+ <CardContent>
55
+ {sent ? (
56
+ <Alert variant="success">
57
+ <MailCheck />
58
+ <AlertDescription>
59
+ If an account exists for <strong>{email}</strong>, we've just sent it a
60
+ reset link. Check your inbox.
61
+ </AlertDescription>
62
+ </Alert>
63
+ ) : (
64
+ <form onSubmit={submit} className="grid gap-4" noValidate>
65
+ <Field label="Email" htmlFor="email" required>
66
+ <Input
67
+ id="email"
68
+ type="email"
69
+ required
70
+ autoComplete="email"
71
+ value={email}
72
+ onChange={(e) => setEmail(e.target.value)}
73
+ placeholder="you@example.com"
74
+ />
75
+ </Field>
76
+ {error ? (
77
+ <Alert variant="destructive">
78
+ <AlertCircle />
79
+ <AlertDescription>{error}</AlertDescription>
80
+ </Alert>
81
+ ) : null}
82
+ <Button type="submit" className="w-full" loading={loading}>
83
+ Send reset link
84
+ </Button>
85
+ {signInSlot ? (
86
+ <p className="text-center text-sm text-muted-foreground">{signInSlot}</p>
87
+ ) : null}
88
+ </form>
89
+ )}
90
+ </CardContent>
91
+ </Card>
92
+ );
93
+ };