@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,230 @@
1
+ import {
2
+ getSession,
3
+ twoFactorConfirm,
4
+ twoFactorDisable,
5
+ twoFactorEnable,
6
+ type TwoFactorEnableResponse,
7
+ } from "@nexys/user-management-client";
8
+ import { AlertCircle, Check, Copy, ShieldCheck, ShieldOff } from "lucide-react";
9
+ import { QRCodeSVG } from "qrcode.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 { Field } from "../ui/field.js";
26
+ import { Input } from "../ui/input.js";
27
+
28
+ type Step = "idle" | "scan" | "codes";
29
+
30
+ /** Enable / disable TOTP two-factor authentication. */
31
+ export const TwoFactorCard = () => {
32
+ const client = useUmClient();
33
+ const session = useCall(getSession);
34
+ const enabled = session.data?.user.twoFactorEnabled ?? false;
35
+
36
+ const [step, setStep] = useState<Step>("idle");
37
+ const [enrolment, setEnrolment] = useState<TwoFactorEnableResponse | null>(null);
38
+ const [code, setCode] = useState("");
39
+ const [busy, setBusy] = useState(false);
40
+ const [error, setError] = useState<string | null>(null);
41
+ const [copied, setCopied] = useState(false);
42
+
43
+ const begin = async () => {
44
+ setBusy(true);
45
+ setError(null);
46
+ const res = await client.call(twoFactorEnable);
47
+ setBusy(false);
48
+ if (res.ok) {
49
+ setEnrolment(res.data);
50
+ setStep("scan");
51
+ } else {
52
+ setError(errorMessage(res.error, "Couldn't start 2FA setup."));
53
+ }
54
+ };
55
+
56
+ const confirm = async (e: FormEvent) => {
57
+ e.preventDefault();
58
+ setBusy(true);
59
+ setError(null);
60
+ const res = await client.call(twoFactorConfirm, { code: code.trim() });
61
+ setBusy(false);
62
+ if (res.ok) {
63
+ setCode("");
64
+ setStep("codes");
65
+ await session.refresh();
66
+ } else {
67
+ setError(errorMessage(res.error, "That code didn't match — try again."));
68
+ }
69
+ };
70
+
71
+ const disable = async (e: FormEvent) => {
72
+ e.preventDefault();
73
+ setBusy(true);
74
+ setError(null);
75
+ const res = await client.call(twoFactorDisable, { code: code.trim() });
76
+ setBusy(false);
77
+ if (res.ok) {
78
+ setCode("");
79
+ setStep("idle");
80
+ setEnrolment(null);
81
+ await session.refresh();
82
+ } else {
83
+ setError(errorMessage(res.error, "That code didn't match — try again."));
84
+ }
85
+ };
86
+
87
+ const copyCodes = async () => {
88
+ if (!enrolment) return;
89
+ await navigator.clipboard.writeText(enrolment.backupCodes.join("\n"));
90
+ setCopied(true);
91
+ setTimeout(() => setCopied(false), 2000);
92
+ };
93
+
94
+ return (
95
+ <Card>
96
+ <CardHeader>
97
+ <CardTitle className="flex items-center gap-2">
98
+ <ShieldCheck className="size-4 text-muted-foreground" />
99
+ Two-factor authentication
100
+ {enabled ? <Badge variant="secondary">Enabled</Badge> : null}
101
+ </CardTitle>
102
+ <CardDescription>
103
+ Require a one-time code from an authenticator app when signing in.
104
+ </CardDescription>
105
+ </CardHeader>
106
+ <CardContent className="grid gap-4">
107
+ {error ? (
108
+ <Alert variant="destructive">
109
+ <AlertCircle />
110
+ <AlertDescription>{error}</AlertDescription>
111
+ </Alert>
112
+ ) : null}
113
+
114
+ {enabled && step === "idle" ? (
115
+ <form onSubmit={disable} className="grid gap-3">
116
+ <Field
117
+ label="Enter a current code to turn off 2FA"
118
+ htmlFor="totp-disable"
119
+ >
120
+ <Input
121
+ id="totp-disable"
122
+ inputMode="numeric"
123
+ autoComplete="one-time-code"
124
+ placeholder="123456"
125
+ value={code}
126
+ onChange={(e) => setCode(e.target.value)}
127
+ className="max-w-40 font-mono"
128
+ />
129
+ </Field>
130
+ <div>
131
+ <Button
132
+ type="submit"
133
+ variant="destructive"
134
+ loading={busy}
135
+ disabled={code.trim().length === 0}
136
+ >
137
+ <ShieldOff />
138
+ Disable 2FA
139
+ </Button>
140
+ </div>
141
+ </form>
142
+ ) : null}
143
+
144
+ {!enabled && step === "idle" ? (
145
+ <div>
146
+ <Button onClick={begin} loading={busy}>
147
+ <ShieldCheck />
148
+ Enable 2FA
149
+ </Button>
150
+ </div>
151
+ ) : null}
152
+
153
+ {step === "scan" && enrolment ? (
154
+ <div className="grid gap-4">
155
+ <p className="text-sm text-muted-foreground">
156
+ Scan this QR code with your authenticator app, then enter the
157
+ 6-digit code it shows.
158
+ </p>
159
+ <div className="flex items-start gap-6">
160
+ <div className="rounded-lg border bg-white p-3">
161
+ <QRCodeSVG value={enrolment.uri} size={144} />
162
+ </div>
163
+ <div className="grid gap-1 text-sm">
164
+ <span className="text-muted-foreground text-xs">
165
+ Or enter the secret manually:
166
+ </span>
167
+ <code
168
+ data-testid="totp-secret"
169
+ className="break-all rounded bg-muted px-2 py-1 font-mono text-xs"
170
+ >
171
+ {enrolment.secret}
172
+ </code>
173
+ </div>
174
+ </div>
175
+ <form onSubmit={confirm} className="flex items-end gap-3">
176
+ <Field label="Verification code" htmlFor="totp-confirm">
177
+ <Input
178
+ id="totp-confirm"
179
+ inputMode="numeric"
180
+ autoComplete="one-time-code"
181
+ placeholder="123456"
182
+ required
183
+ value={code}
184
+ onChange={(e) => setCode(e.target.value)}
185
+ className="max-w-40 font-mono"
186
+ />
187
+ </Field>
188
+ <Button type="submit" loading={busy} disabled={code.trim().length === 0}>
189
+ Verify & enable
190
+ </Button>
191
+ </form>
192
+ </div>
193
+ ) : null}
194
+
195
+ {step === "codes" && enrolment ? (
196
+ <div className="grid gap-3">
197
+ <Alert variant="success">
198
+ <ShieldCheck />
199
+ <AlertDescription>
200
+ Two-factor authentication is on. Save these backup codes now —
201
+ they are shown only once and each works a single time.
202
+ </AlertDescription>
203
+ </Alert>
204
+ <div className="grid grid-cols-2 gap-2 rounded-lg border bg-muted/30 p-4 sm:grid-cols-5">
205
+ {enrolment.backupCodes.map((c) => (
206
+ <code key={c} className="font-mono text-xs">
207
+ {c}
208
+ </code>
209
+ ))}
210
+ </div>
211
+ <div className="flex gap-2">
212
+ <Button variant="outline" onClick={copyCodes}>
213
+ {copied ? <Check className="text-green-600" /> : <Copy />}
214
+ {copied ? "Copied" : "Copy codes"}
215
+ </Button>
216
+ <Button
217
+ onClick={() => {
218
+ setStep("idle");
219
+ setEnrolment(null);
220
+ }}
221
+ >
222
+ Done
223
+ </Button>
224
+ </div>
225
+ </div>
226
+ ) : null}
227
+ </CardContent>
228
+ </Card>
229
+ );
230
+ };