@nexys/user-management-web 0.0.26

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 (77) 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 +91 -0
  5. package/src/components/audit/audit-log-table.tsx +226 -0
  6. package/src/components/auth/accept-invitation-card.tsx +100 -0
  7. package/src/components/auth/forgot-password-form.tsx +92 -0
  8. package/src/components/auth/reset-password-form.tsx +143 -0
  9. package/src/components/auth/sign-in-form.test.tsx +140 -0
  10. package/src/components/auth/sign-in-form.tsx +509 -0
  11. package/src/components/auth/sign-up-form.tsx +123 -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 +235 -0
  20. package/src/components/profile/change-password-form.tsx +158 -0
  21. package/src/components/profile/passkeys-card.tsx +180 -0
  22. package/src/components/profile/profile-card.tsx +33 -0
  23. package/src/components/profile/two-factor-card.tsx +226 -0
  24. package/src/components/projects/project-consents.tsx +572 -0
  25. package/src/components/projects/project-create-dialog.tsx +209 -0
  26. package/src/components/projects/project-detail-tabs.tsx +63 -0
  27. package/src/components/projects/project-email-templates.tsx +282 -0
  28. package/src/components/projects/project-notices.tsx +458 -0
  29. package/src/components/projects/project-settings.tsx +1133 -0
  30. package/src/components/projects/project-tenants.tsx +107 -0
  31. package/src/components/projects/projects-table.test.tsx +95 -0
  32. package/src/components/projects/projects-table.tsx +240 -0
  33. package/src/components/roles/role-badge.tsx +91 -0
  34. package/src/components/roles/role-select.tsx +76 -0
  35. package/src/components/tenants/tenant-create-dialog.tsx +163 -0
  36. package/src/components/tenants/tenant-detail-tabs.tsx +51 -0
  37. package/src/components/tenants/tenant-members.tsx +578 -0
  38. package/src/components/tenants/tenant-roles.test.tsx +91 -0
  39. package/src/components/tenants/tenant-roles.tsx +414 -0
  40. package/src/components/tenants/tenant-settings.tsx +385 -0
  41. package/src/components/tenants/tenants-table.test.tsx +71 -0
  42. package/src/components/tenants/tenants-table.tsx +245 -0
  43. package/src/components/ui/alert.tsx +52 -0
  44. package/src/components/ui/avatar.tsx +46 -0
  45. package/src/components/ui/badge.tsx +32 -0
  46. package/src/components/ui/button.tsx +64 -0
  47. package/src/components/ui/card.tsx +65 -0
  48. package/src/components/ui/dialog.tsx +99 -0
  49. package/src/components/ui/dropdown-menu.tsx +97 -0
  50. package/src/components/ui/field.tsx +44 -0
  51. package/src/components/ui/input.tsx +23 -0
  52. package/src/components/ui/label.tsx +19 -0
  53. package/src/components/ui/select.tsx +87 -0
  54. package/src/components/ui/separator.tsx +22 -0
  55. package/src/components/ui/skeleton.tsx +13 -0
  56. package/src/components/ui/switch.tsx +29 -0
  57. package/src/components/ui/table.tsx +87 -0
  58. package/src/components/ui/tabs.tsx +51 -0
  59. package/src/components/ui/textarea.tsx +22 -0
  60. package/src/components/ui/tooltip.tsx +27 -0
  61. package/src/components/users/user-create-dialog.tsx +267 -0
  62. package/src/components/users/user-delete-dialog.tsx +88 -0
  63. package/src/components/users/user-detail.tsx +843 -0
  64. package/src/components/users/users-table.test.tsx +97 -0
  65. package/src/components/users/users-table.tsx +382 -0
  66. package/src/context/auth-context.tsx +124 -0
  67. package/src/context/theme-context.test.tsx +78 -0
  68. package/src/context/theme-context.tsx +110 -0
  69. package/src/hooks/use-call.ts +64 -0
  70. package/src/index.ts +159 -0
  71. package/src/lib/error-message.ts +16 -0
  72. package/src/lib/utils.test.ts +61 -0
  73. package/src/lib/utils.ts +79 -0
  74. package/src/lib/webauthn.ts +36 -0
  75. package/src/styles.css +107 -0
  76. package/src/test-setup.ts +5 -0
  77. package/tailwind.preset.cjs +15 -0
@@ -0,0 +1,158 @@
1
+ import { AlertCircle, KeyRound, ShieldCheck } from "lucide-react";
2
+ import { useState, type FormEvent } from "react";
3
+
4
+ import { useUmClient } from "../../context/auth-context.js";
5
+ import { errorMessage } from "../../lib/error-message.js";
6
+ import { Alert, AlertDescription } from "../ui/alert.js";
7
+ import { Button } from "../ui/button.js";
8
+ import {
9
+ Card,
10
+ CardContent,
11
+ CardDescription,
12
+ CardHeader,
13
+ CardTitle,
14
+ } from "../ui/card.js";
15
+ import { Field } from "../ui/field.js";
16
+ import { Input } from "../ui/input.js";
17
+
18
+ export const ChangePasswordForm = ({
19
+ minLength = 8,
20
+ }: {
21
+ minLength?: number;
22
+ }) => {
23
+ const client = useUmClient();
24
+ const [current, setCurrent] = useState("");
25
+ const [next, setNext] = useState("");
26
+ const [confirm, setConfirm] = useState("");
27
+ const [loading, setLoading] = useState(false);
28
+ const [error, setError] = useState<string | null>(null);
29
+ const [success, setSuccess] = useState(false);
30
+ const [noPassword, setNoPassword] = useState(false);
31
+
32
+ const mismatched = confirm.length > 0 && confirm !== next;
33
+ const tooShort = next.length > 0 && next.length < minLength;
34
+
35
+ const submit = async (e: FormEvent) => {
36
+ e.preventDefault();
37
+ if (mismatched || tooShort) return;
38
+ setLoading(true);
39
+ setError(null);
40
+ setSuccess(false);
41
+ const res = await client.changePassword({
42
+ currentPassword: current,
43
+ newPassword: next,
44
+ });
45
+ setLoading(false);
46
+ if (res.ok) {
47
+ setSuccess(true);
48
+ setCurrent("");
49
+ setNext("");
50
+ setConfirm("");
51
+ } else if (res.error.kind === "http" && res.error.code === "no_password_set") {
52
+ // Social/passkey-only account — there is no password to change.
53
+ setNoPassword(true);
54
+ } else {
55
+ setError(errorMessage(res.error, "Couldn't change password."));
56
+ }
57
+ };
58
+
59
+ if (noPassword) {
60
+ return (
61
+ <Card>
62
+ <CardHeader>
63
+ <CardTitle className="flex items-center gap-2">
64
+ <KeyRound className="size-4 text-muted-foreground" />
65
+ Change password
66
+ </CardTitle>
67
+ <CardDescription>
68
+ This account has no password — you sign in with a social provider
69
+ or passkey. Ask an administrator to email you a set-password link
70
+ if you'd like one.
71
+ </CardDescription>
72
+ </CardHeader>
73
+ </Card>
74
+ );
75
+ }
76
+
77
+ return (
78
+ <Card>
79
+ <CardHeader>
80
+ <CardTitle className="flex items-center gap-2">
81
+ <KeyRound className="size-4 text-muted-foreground" />
82
+ Change password
83
+ </CardTitle>
84
+ <CardDescription>
85
+ Use a long, unique password. We'll keep you signed in on this device.
86
+ </CardDescription>
87
+ </CardHeader>
88
+ <CardContent>
89
+ <form onSubmit={submit} className="grid gap-4">
90
+ <Field label="Current password" htmlFor="current-pw" required>
91
+ <Input
92
+ id="current-pw"
93
+ type="password"
94
+ autoComplete="current-password"
95
+ required
96
+ value={current}
97
+ onChange={(e) => setCurrent(e.target.value)}
98
+ />
99
+ </Field>
100
+ <Field
101
+ label="New password"
102
+ htmlFor="next-pw"
103
+ required
104
+ description={`At least ${minLength} characters.`}
105
+ error={tooShort ? `New password is too short.` : undefined}
106
+ >
107
+ <Input
108
+ id="next-pw"
109
+ type="password"
110
+ autoComplete="new-password"
111
+ required
112
+ value={next}
113
+ onChange={(e) => setNext(e.target.value)}
114
+ aria-invalid={tooShort || undefined}
115
+ />
116
+ </Field>
117
+ <Field
118
+ label="Confirm new password"
119
+ htmlFor="confirm-pw"
120
+ required
121
+ error={mismatched ? "Passwords don't match." : undefined}
122
+ >
123
+ <Input
124
+ id="confirm-pw"
125
+ type="password"
126
+ autoComplete="new-password"
127
+ required
128
+ value={confirm}
129
+ onChange={(e) => setConfirm(e.target.value)}
130
+ aria-invalid={mismatched || undefined}
131
+ />
132
+ </Field>
133
+ {success ? (
134
+ <Alert variant="success">
135
+ <ShieldCheck />
136
+ <AlertDescription>Your password has been updated.</AlertDescription>
137
+ </Alert>
138
+ ) : null}
139
+ {error ? (
140
+ <Alert variant="destructive">
141
+ <AlertCircle />
142
+ <AlertDescription>{error}</AlertDescription>
143
+ </Alert>
144
+ ) : null}
145
+ <div className="flex justify-end">
146
+ <Button
147
+ type="submit"
148
+ loading={loading}
149
+ disabled={mismatched || tooShort || !current || !next}
150
+ >
151
+ Update password
152
+ </Button>
153
+ </div>
154
+ </form>
155
+ </CardContent>
156
+ </Card>
157
+ );
158
+ };
@@ -0,0 +1,180 @@
1
+ import { AlertCircle, Fingerprint, Plus, Trash2 } from "lucide-react";
2
+ import { useState, type FormEvent } from "react";
3
+
4
+ import { useUmClient } from "../../context/auth-context.js";
5
+ import { useCall } from "../../hooks/use-call.js";
6
+ import { errorMessage } from "../../lib/error-message.js";
7
+ import { formatRelative } from "../../lib/utils.js";
8
+ import {
9
+ createPasskey,
10
+ webauthnErrorMessage,
11
+ webauthnSupported,
12
+ } from "../../lib/webauthn.js";
13
+ import { EmptyState } from "../primitives/empty-state.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 { Input } from "../ui/input.js";
24
+ import {
25
+ Table,
26
+ TableBody,
27
+ TableCell,
28
+ TableRow,
29
+ } from "../ui/table.js";
30
+
31
+ /** List, add and remove the signed-in user's passkeys. */
32
+ export const PasskeysCard = () => {
33
+ const client = useUmClient();
34
+ const { data, loading, error, refresh } = useCall("listPasskeys");
35
+ const [name, setName] = useState("");
36
+ const [adding, setAdding] = useState(false);
37
+ const [busy, setBusy] = useState<string | null>(null);
38
+ const [actionError, setActionError] = useState<string | null>(null);
39
+
40
+ const supported = webauthnSupported();
41
+ const passkeys = data?.passkeys ?? [];
42
+
43
+ const add = async (e: FormEvent) => {
44
+ e.preventDefault();
45
+ setAdding(true);
46
+ setActionError(null);
47
+ try {
48
+ const start = await client.passkeyRegisterStart();
49
+ if (!start.ok) {
50
+ setActionError(errorMessage(start.error, "Couldn't start passkey setup."));
51
+ return;
52
+ }
53
+ const credential = await createPasskey(start.data.options);
54
+ const finish = await client.passkeyRegisterFinish({
55
+ challenge: start.data.challenge,
56
+ response: credential,
57
+ name: name.trim() || undefined,
58
+ });
59
+ if (!finish.ok) {
60
+ setActionError(errorMessage(finish.error, "Couldn't save the passkey."));
61
+ return;
62
+ }
63
+ setName("");
64
+ await refresh();
65
+ } catch (err) {
66
+ // The browser throws when the user cancels the dialog — and with
67
+ // a cryptic InvalidStateError when this device already holds a
68
+ // passkey for the site; translate those into plain language.
69
+ setActionError(webauthnErrorMessage(err, "Passkey setup failed."));
70
+ } finally {
71
+ setAdding(false);
72
+ }
73
+ };
74
+
75
+ const remove = async (id: string) => {
76
+ setBusy(id);
77
+ setActionError(null);
78
+ const res = await client.deleteOwnPasskey({ id });
79
+ setBusy(null);
80
+ if (res.ok) {
81
+ await refresh();
82
+ } else {
83
+ setActionError(errorMessage(res.error, "Couldn't remove the passkey."));
84
+ }
85
+ };
86
+
87
+ return (
88
+ <Card>
89
+ <CardHeader>
90
+ <CardTitle className="flex items-center gap-2">
91
+ <Fingerprint className="size-4 text-muted-foreground" />
92
+ Passkeys
93
+ </CardTitle>
94
+ <CardDescription>
95
+ Sign in with your device's fingerprint, face or PIN instead of a
96
+ password.
97
+ </CardDescription>
98
+ </CardHeader>
99
+ <CardContent className="grid gap-4">
100
+ {error ? (
101
+ <Alert variant="destructive">
102
+ <AlertCircle />
103
+ <AlertDescription>{errorMessage(error)}</AlertDescription>
104
+ </Alert>
105
+ ) : null}
106
+ {actionError ? (
107
+ <Alert variant="destructive">
108
+ <AlertCircle />
109
+ <AlertDescription>{actionError}</AlertDescription>
110
+ </Alert>
111
+ ) : null}
112
+ {!supported ? (
113
+ <Alert>
114
+ <AlertCircle />
115
+ <AlertDescription>
116
+ This browser doesn't support passkeys.
117
+ </AlertDescription>
118
+ </Alert>
119
+ ) : (
120
+ <form onSubmit={add} className="flex items-end gap-3">
121
+ <div className="grid flex-1 gap-1.5">
122
+ <label
123
+ htmlFor="passkey-name"
124
+ className="text-sm font-medium leading-none"
125
+ >
126
+ Name (optional)
127
+ </label>
128
+ <Input
129
+ id="passkey-name"
130
+ value={name}
131
+ onChange={(e) => setName(e.target.value)}
132
+ placeholder="e.g. MacBook Touch ID"
133
+ />
134
+ </div>
135
+ <Button type="submit" loading={adding}>
136
+ <Plus />
137
+ Add passkey
138
+ </Button>
139
+ </form>
140
+ )}
141
+
142
+ {loading ? null : passkeys.length === 0 ? (
143
+ <EmptyState
144
+ icon={Fingerprint}
145
+ title="No passkeys yet"
146
+ description="Add one to sign in without a password."
147
+ />
148
+ ) : (
149
+ <div className="overflow-hidden rounded-lg border">
150
+ <Table>
151
+ <TableBody>
152
+ {passkeys.map((p) => (
153
+ <TableRow key={p.id}>
154
+ <TableCell className="font-medium">
155
+ {p.name || "Unnamed passkey"}
156
+ </TableCell>
157
+ <TableCell className="text-sm text-muted-foreground">
158
+ {p.createdAt ? `Added ${formatRelative(p.createdAt)}` : ""}
159
+ </TableCell>
160
+ <TableCell className="text-right">
161
+ <Button
162
+ variant="ghost"
163
+ size="icon"
164
+ onClick={() => remove(p.id)}
165
+ loading={busy === p.id}
166
+ aria-label={`Remove passkey ${p.name ?? p.id}`}
167
+ >
168
+ <Trash2 className="text-destructive" />
169
+ </Button>
170
+ </TableCell>
171
+ </TableRow>
172
+ ))}
173
+ </TableBody>
174
+ </Table>
175
+ </div>
176
+ )}
177
+ </CardContent>
178
+ </Card>
179
+ );
180
+ };
@@ -0,0 +1,33 @@
1
+ import { useAuth } from "../../context/auth-context.js";
2
+ import { initialsOf } from "../../lib/utils.js";
3
+ import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar.js";
4
+ import { Badge } from "../ui/badge.js";
5
+ import { Card, CardDescription, CardHeader, CardTitle } from "../ui/card.js";
6
+
7
+ /**
8
+ * The signed-in user's identity. Read-only: the API has no self-service
9
+ * profile update (names are changed through the admin `adminUpdateUser`).
10
+ */
11
+ export const ProfileCard = ({ image }: { image?: string | null }) => {
12
+ const { data } = useAuth();
13
+ if (!data) return null;
14
+ const { user } = data;
15
+
16
+ return (
17
+ <Card>
18
+ <CardHeader className="flex flex-row items-start gap-4 space-y-0">
19
+ <Avatar className="size-14 ring-2 ring-background">
20
+ {image ? <AvatarImage src={image} alt={user.name ?? user.email} /> : null}
21
+ <AvatarFallback>{initialsOf(user.name, user.email)}</AvatarFallback>
22
+ </Avatar>
23
+ <div className="flex flex-1 flex-col gap-1">
24
+ <CardTitle className="leading-tight">{user.name ?? user.email}</CardTitle>
25
+ <CardDescription className="flex flex-wrap items-center gap-2 text-xs">
26
+ <span>{user.email}</span>
27
+ <Badge variant="muted">{user.role}</Badge>
28
+ </CardDescription>
29
+ </div>
30
+ </CardHeader>
31
+ </Card>
32
+ );
33
+ };
@@ -0,0 +1,226 @@
1
+ import {
2
+ type TwoFactorEnableResponse,
3
+ } from "@nexys/user-management-client";
4
+ import { AlertCircle, Check, Copy, ShieldCheck, ShieldOff } from "lucide-react";
5
+ import { QRCodeSVG } from "qrcode.react";
6
+ import { useState, type FormEvent } from "react";
7
+
8
+ import { useUmClient } from "../../context/auth-context.js";
9
+ import { useCall } from "../../hooks/use-call.js";
10
+ import { errorMessage } from "../../lib/error-message.js";
11
+ import { Alert, AlertDescription } from "../ui/alert.js";
12
+ import { Badge } from "../ui/badge.js";
13
+ import { Button } from "../ui/button.js";
14
+ import {
15
+ Card,
16
+ CardContent,
17
+ CardDescription,
18
+ CardHeader,
19
+ CardTitle,
20
+ } from "../ui/card.js";
21
+ import { Field } from "../ui/field.js";
22
+ import { Input } from "../ui/input.js";
23
+
24
+ type Step = "idle" | "scan" | "codes";
25
+
26
+ /** Enable / disable TOTP two-factor authentication. */
27
+ export const TwoFactorCard = () => {
28
+ const client = useUmClient();
29
+ const session = useCall("getSession");
30
+ const enabled = session.data?.user.twoFactorEnabled ?? false;
31
+
32
+ const [step, setStep] = useState<Step>("idle");
33
+ const [enrolment, setEnrolment] = useState<TwoFactorEnableResponse | null>(null);
34
+ const [code, setCode] = useState("");
35
+ const [busy, setBusy] = useState(false);
36
+ const [error, setError] = useState<string | null>(null);
37
+ const [copied, setCopied] = useState(false);
38
+
39
+ const begin = async () => {
40
+ setBusy(true);
41
+ setError(null);
42
+ const res = await client.twoFactorEnable();
43
+ setBusy(false);
44
+ if (res.ok) {
45
+ setEnrolment(res.data);
46
+ setStep("scan");
47
+ } else {
48
+ setError(errorMessage(res.error, "Couldn't start 2FA setup."));
49
+ }
50
+ };
51
+
52
+ const confirm = async (e: FormEvent) => {
53
+ e.preventDefault();
54
+ setBusy(true);
55
+ setError(null);
56
+ const res = await client.twoFactorConfirm({ code: code.trim() });
57
+ setBusy(false);
58
+ if (res.ok) {
59
+ setCode("");
60
+ setStep("codes");
61
+ await session.refresh();
62
+ } else {
63
+ setError(errorMessage(res.error, "That code didn't match — try again."));
64
+ }
65
+ };
66
+
67
+ const disable = async (e: FormEvent) => {
68
+ e.preventDefault();
69
+ setBusy(true);
70
+ setError(null);
71
+ const res = await client.twoFactorDisable({ code: code.trim() });
72
+ setBusy(false);
73
+ if (res.ok) {
74
+ setCode("");
75
+ setStep("idle");
76
+ setEnrolment(null);
77
+ await session.refresh();
78
+ } else {
79
+ setError(errorMessage(res.error, "That code didn't match — try again."));
80
+ }
81
+ };
82
+
83
+ const copyCodes = async () => {
84
+ if (!enrolment) return;
85
+ await navigator.clipboard.writeText(enrolment.backupCodes.join("\n"));
86
+ setCopied(true);
87
+ setTimeout(() => setCopied(false), 2000);
88
+ };
89
+
90
+ return (
91
+ <Card>
92
+ <CardHeader>
93
+ <CardTitle className="flex items-center gap-2">
94
+ <ShieldCheck className="size-4 text-muted-foreground" />
95
+ Two-factor authentication
96
+ {enabled ? <Badge variant="secondary">Enabled</Badge> : null}
97
+ </CardTitle>
98
+ <CardDescription>
99
+ Require a one-time code from an authenticator app when signing in.
100
+ </CardDescription>
101
+ </CardHeader>
102
+ <CardContent className="grid gap-4">
103
+ {error ? (
104
+ <Alert variant="destructive">
105
+ <AlertCircle />
106
+ <AlertDescription>{error}</AlertDescription>
107
+ </Alert>
108
+ ) : null}
109
+
110
+ {enabled && step === "idle" ? (
111
+ <form onSubmit={disable} className="grid gap-3">
112
+ <Field
113
+ label="Enter a current code to turn off 2FA"
114
+ htmlFor="totp-disable"
115
+ >
116
+ <Input
117
+ id="totp-disable"
118
+ inputMode="numeric"
119
+ autoComplete="one-time-code"
120
+ placeholder="123456"
121
+ value={code}
122
+ onChange={(e) => setCode(e.target.value)}
123
+ className="max-w-40 font-mono"
124
+ />
125
+ </Field>
126
+ <div>
127
+ <Button
128
+ type="submit"
129
+ variant="destructive"
130
+ loading={busy}
131
+ disabled={code.trim().length === 0}
132
+ >
133
+ <ShieldOff />
134
+ Disable 2FA
135
+ </Button>
136
+ </div>
137
+ </form>
138
+ ) : null}
139
+
140
+ {!enabled && step === "idle" ? (
141
+ <div>
142
+ <Button onClick={begin} loading={busy}>
143
+ <ShieldCheck />
144
+ Enable 2FA
145
+ </Button>
146
+ </div>
147
+ ) : null}
148
+
149
+ {step === "scan" && enrolment ? (
150
+ <div className="grid gap-4">
151
+ <p className="text-sm text-muted-foreground">
152
+ Scan this QR code with your authenticator app, then enter the
153
+ 6-digit code it shows.
154
+ </p>
155
+ <div className="flex items-start gap-6">
156
+ <div className="rounded-lg border bg-white p-3">
157
+ <QRCodeSVG value={enrolment.uri} size={144} />
158
+ </div>
159
+ <div className="grid gap-1 text-sm">
160
+ <span className="text-muted-foreground text-xs">
161
+ Or enter the secret manually:
162
+ </span>
163
+ <code
164
+ data-testid="totp-secret"
165
+ className="break-all rounded bg-muted px-2 py-1 font-mono text-xs"
166
+ >
167
+ {enrolment.secret}
168
+ </code>
169
+ </div>
170
+ </div>
171
+ <form onSubmit={confirm} className="flex items-end gap-3">
172
+ <Field label="Verification code" htmlFor="totp-confirm">
173
+ <Input
174
+ id="totp-confirm"
175
+ inputMode="numeric"
176
+ autoComplete="one-time-code"
177
+ placeholder="123456"
178
+ required
179
+ value={code}
180
+ onChange={(e) => setCode(e.target.value)}
181
+ className="max-w-40 font-mono"
182
+ />
183
+ </Field>
184
+ <Button type="submit" loading={busy} disabled={code.trim().length === 0}>
185
+ Verify & enable
186
+ </Button>
187
+ </form>
188
+ </div>
189
+ ) : null}
190
+
191
+ {step === "codes" && enrolment ? (
192
+ <div className="grid gap-3">
193
+ <Alert variant="success">
194
+ <ShieldCheck />
195
+ <AlertDescription>
196
+ Two-factor authentication is on. Save these backup codes now —
197
+ they are shown only once and each works a single time.
198
+ </AlertDescription>
199
+ </Alert>
200
+ <div className="grid grid-cols-2 gap-2 rounded-lg border bg-muted/30 p-4 sm:grid-cols-5">
201
+ {enrolment.backupCodes.map((c) => (
202
+ <code key={c} className="font-mono text-xs">
203
+ {c}
204
+ </code>
205
+ ))}
206
+ </div>
207
+ <div className="flex gap-2">
208
+ <Button variant="outline" onClick={copyCodes}>
209
+ {copied ? <Check className="text-green-600" /> : <Copy />}
210
+ {copied ? "Copied" : "Copy codes"}
211
+ </Button>
212
+ <Button
213
+ onClick={() => {
214
+ setStep("idle");
215
+ setEnrolment(null);
216
+ }}
217
+ >
218
+ Done
219
+ </Button>
220
+ </div>
221
+ </div>
222
+ ) : null}
223
+ </CardContent>
224
+ </Card>
225
+ );
226
+ };