@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,509 @@
1
+ import {
2
+ isConsentChallenge,
3
+ isTwoFactorChallenge,
4
+ } from "@nexys/user-management-client";
5
+ import { AlertCircle, Fingerprint, Mail, MailCheck, ShieldCheck, Wand2 } from "lucide-react";
6
+ import { useState, type FormEvent, type ReactNode } from "react";
7
+
8
+ import { useUmClient } from "../../context/auth-context.js";
9
+ import { errorMessage } from "../../lib/error-message.js";
10
+ import {
11
+ getPasskeyAssertion,
12
+ webauthnErrorMessage,
13
+ webauthnSupported,
14
+ } from "../../lib/webauthn.js";
15
+ import { Alert, AlertDescription } from "../ui/alert.js";
16
+ import { Button } from "../ui/button.js";
17
+ import {
18
+ Card,
19
+ CardContent,
20
+ CardDescription,
21
+ CardHeader,
22
+ CardTitle,
23
+ } from "../ui/card.js";
24
+ import { Field } from "../ui/field.js";
25
+ import { Input } from "../ui/input.js";
26
+ import { Separator } from "../ui/separator.js";
27
+
28
+ const providerLabel: Record<string, string> = {
29
+ google: "Google",
30
+ github: "GitHub",
31
+ microsoft: "Microsoft",
32
+ discord: "Discord",
33
+ facebook: "Facebook",
34
+ };
35
+
36
+ const ProviderIcon = ({ provider }: { provider: string }) => {
37
+ switch (provider) {
38
+ case "google":
39
+ return (
40
+ <svg className="size-4" viewBox="0 0 24 24">
41
+ <path
42
+ fill="currentColor"
43
+ d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z"
44
+ />
45
+ <path
46
+ fill="currentColor"
47
+ d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
48
+ />
49
+ <path
50
+ fill="currentColor"
51
+ d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
52
+ />
53
+ <path
54
+ fill="currentColor"
55
+ d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
56
+ />
57
+ </svg>
58
+ );
59
+ case "github":
60
+ return (
61
+ <svg className="size-4" viewBox="0 0 24 24" fill="currentColor">
62
+ <path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0 0 24 12c0-6.63-5.37-12-12-12z" />
63
+ </svg>
64
+ );
65
+ default:
66
+ return null;
67
+ }
68
+ };
69
+
70
+ export interface SignInFormProps {
71
+ onSuccess?: (user: { id: string; email: string }) => void;
72
+ forgotPasswordSlot?: ReactNode;
73
+ signUpSlot?: ReactNode;
74
+ title?: ReactNode;
75
+ description?: ReactNode;
76
+ /**
77
+ * Social providers to offer as "Continue with …" buttons. Social
78
+ * sign-in only works for providers a product has an enabled
79
+ * connection for, so pass the `/api/providers?projectId=…` list (and
80
+ * `projectId` below) — there is no default: without this the form is
81
+ * email/passkey/magic-link only.
82
+ */
83
+ socialProviders?: string[];
84
+ /** Product whose social connections back the buttons above. */
85
+ projectId?: string;
86
+ }
87
+
88
+ export const SignInForm = ({
89
+ onSuccess,
90
+ forgotPasswordSlot,
91
+ signUpSlot,
92
+ title = "Welcome back",
93
+ description = "Sign in to your account to continue.",
94
+ socialProviders,
95
+ projectId,
96
+ }: SignInFormProps) => {
97
+ const client = useUmClient();
98
+ const [email, setEmail] = useState("");
99
+ const [password, setPassword] = useState("");
100
+ const [loading, setLoading] = useState(false);
101
+ const [error, setError] = useState<string | null>(null);
102
+ const providers = socialProviders ?? [];
103
+ const [socialLoading, setSocialLoading] = useState<string | null>(null);
104
+ const [passkeyLoading, setPasskeyLoading] = useState(false);
105
+ // Non-null once a password sign-in came back `twoFactorRequired` —
106
+ // the form switches to the one-time-code step.
107
+ const [challengeToken, setChallengeToken] = useState<string | null>(null);
108
+ const [otp, setOtp] = useState("");
109
+ const [useBackupCode, setUseBackupCode] = useState(false);
110
+ // Magic-link flow: "form" is the email-only step, "sent" the
111
+ // check-your-inbox confirmation.
112
+ const [magicStep, setMagicStep] = useState<"off" | "form" | "sent">("off");
113
+
114
+ const submit = async (e: FormEvent) => {
115
+ e.preventDefault();
116
+ setLoading(true);
117
+ setError(null);
118
+ const res = await client.signIn({ email: email.trim(), password });
119
+ setLoading(false);
120
+ if (res.ok) {
121
+ if (isTwoFactorChallenge(res.data)) {
122
+ setChallengeToken(res.data.twoFactorToken);
123
+ } else if (isConsentChallenge(res.data)) {
124
+ // A blocking agreement is outstanding, so no session was issued.
125
+ // The console isn't the place to accept product terms — point the
126
+ // user at the app that asked for them.
127
+ setError(
128
+ `You need to accept "${res.data.documents[0]?.title ?? "an agreement"}" before signing in.`
129
+ );
130
+ } else {
131
+ onSuccess?.({ id: res.data.user.id, email: res.data.user.email });
132
+ }
133
+ } else {
134
+ setError(errorMessage(res.error, "Sign-in failed. Please try again."));
135
+ }
136
+ };
137
+
138
+ const submitOtp = async (e: FormEvent) => {
139
+ e.preventDefault();
140
+ if (!challengeToken) return;
141
+ setLoading(true);
142
+ setError(null);
143
+ const verify = useBackupCode ? client.verifyBackupCode : client.verifyTotp;
144
+ const res = await verify({
145
+ token: challengeToken,
146
+ code: otp.trim(),
147
+ });
148
+ setLoading(false);
149
+ if (res.ok) {
150
+ if (isConsentChallenge(res.data)) {
151
+ // Same gate as password sign-in — no session was issued.
152
+ setError(
153
+ `You need to accept "${res.data.documents[0]?.title ?? "an agreement"}" before signing in.`
154
+ );
155
+ } else {
156
+ onSuccess?.({ id: res.data.user.id, email: res.data.user.email });
157
+ }
158
+ } else {
159
+ setError(errorMessage(res.error, "That code didn't match — try again."));
160
+ }
161
+ };
162
+
163
+ const handleSocial = async (provider: string) => {
164
+ setSocialLoading(provider);
165
+ setError(null);
166
+ const callbackURL = window.location.origin + "/";
167
+ const res = await client.socialSignIn({ provider, callbackURL, projectId });
168
+ if (res.ok) {
169
+ window.location.href = res.data.url;
170
+ } else {
171
+ setSocialLoading(null);
172
+ setError(errorMessage(res.error, `Sign-in with ${providerLabel[provider] ?? provider} failed.`));
173
+ }
174
+ };
175
+
176
+ const handlePasskey = async () => {
177
+ setPasskeyLoading(true);
178
+ setError(null);
179
+ try {
180
+ const start = await client.passkeySignInStart({
181
+ email: email.trim() || undefined,
182
+ });
183
+ if (!start.ok) {
184
+ setError(errorMessage(start.error, "Couldn't start passkey sign-in."));
185
+ return;
186
+ }
187
+ const assertion = await getPasskeyAssertion(start.data.options);
188
+ const finish = await client.passkeySignInFinish({
189
+ challenge: start.data.challenge,
190
+ response: assertion,
191
+ });
192
+ if (finish.ok) {
193
+ if (isConsentChallenge(finish.data)) {
194
+ // Same gate as password sign-in — no session was issued.
195
+ setError(
196
+ `You need to accept "${finish.data.documents[0]?.title ?? "an agreement"}" before signing in.`
197
+ );
198
+ } else {
199
+ onSuccess?.({ id: finish.data.user.id, email: finish.data.user.email });
200
+ }
201
+ } else {
202
+ setError(errorMessage(finish.error, "Passkey sign-in failed."));
203
+ }
204
+ } catch (err) {
205
+ // The browser throws when the user dismisses the platform dialog.
206
+ setError(webauthnErrorMessage(err, "Passkey sign-in failed."));
207
+ } finally {
208
+ setPasskeyLoading(false);
209
+ }
210
+ };
211
+
212
+ const submitMagicLink = async (e: FormEvent) => {
213
+ e.preventDefault();
214
+ setLoading(true);
215
+ setError(null);
216
+ const res = await client.requestMagicLink({ email: email.trim() });
217
+ setLoading(false);
218
+ if (res.ok) {
219
+ setMagicStep("sent");
220
+ } else {
221
+ setError(errorMessage(res.error, "Couldn't send the sign-in link."));
222
+ }
223
+ };
224
+
225
+ // ── Magic-link steps ──────────────────────────────────────────────
226
+ if (magicStep === "sent") {
227
+ return (
228
+ <Card className="w-full max-w-md">
229
+ <CardHeader className="space-y-1.5">
230
+ <CardTitle className="flex items-center gap-2 text-2xl">
231
+ <MailCheck className="size-5 text-muted-foreground" />
232
+ Check your inbox
233
+ </CardTitle>
234
+ <CardDescription>
235
+ If an account exists for <span className="font-medium">{email.trim()}</span>,
236
+ a sign-in link is on its way. It expires in 15 minutes and works once.
237
+ </CardDescription>
238
+ </CardHeader>
239
+ <CardContent>
240
+ <Button
241
+ type="button"
242
+ variant="outline"
243
+ className="w-full"
244
+ onClick={() => {
245
+ setMagicStep("off");
246
+ setError(null);
247
+ }}
248
+ >
249
+ Back to sign-in
250
+ </Button>
251
+ </CardContent>
252
+ </Card>
253
+ );
254
+ }
255
+
256
+ if (magicStep === "form") {
257
+ return (
258
+ <Card className="w-full max-w-md">
259
+ <CardHeader className="space-y-1.5">
260
+ <CardTitle className="flex items-center gap-2 text-2xl">
261
+ <Wand2 className="size-5 text-muted-foreground" />
262
+ Email me a sign-in link
263
+ </CardTitle>
264
+ <CardDescription>
265
+ No password needed — we&apos;ll send a one-time link to your inbox.
266
+ </CardDescription>
267
+ </CardHeader>
268
+ <CardContent>
269
+ <form onSubmit={submitMagicLink} className="grid gap-4" noValidate>
270
+ <Field label="Email" htmlFor="magic-email" required>
271
+ <div className="relative">
272
+ <Mail
273
+ className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground"
274
+ aria-hidden
275
+ />
276
+ <Input
277
+ id="magic-email"
278
+ type="email"
279
+ autoComplete="email"
280
+ autoFocus
281
+ required
282
+ value={email}
283
+ onChange={(e) => setEmail(e.target.value)}
284
+ className="pl-9"
285
+ placeholder="you@example.com"
286
+ />
287
+ </div>
288
+ </Field>
289
+ {error ? (
290
+ <Alert variant="destructive">
291
+ <AlertCircle />
292
+ <AlertDescription>{error}</AlertDescription>
293
+ </Alert>
294
+ ) : null}
295
+ <Button
296
+ type="submit"
297
+ className="w-full"
298
+ loading={loading}
299
+ disabled={!email.trim()}
300
+ >
301
+ Send sign-in link
302
+ </Button>
303
+ <button
304
+ type="button"
305
+ className="text-center text-sm text-muted-foreground underline-offset-4 hover:underline"
306
+ onClick={() => {
307
+ setMagicStep("off");
308
+ setError(null);
309
+ }}
310
+ >
311
+ Back to sign-in
312
+ </button>
313
+ </form>
314
+ </CardContent>
315
+ </Card>
316
+ );
317
+ }
318
+
319
+ // ── 2FA challenge step ────────────────────────────────────────────
320
+ if (challengeToken) {
321
+ return (
322
+ <Card className="w-full max-w-md">
323
+ <CardHeader className="space-y-1.5">
324
+ <CardTitle className="flex items-center gap-2 text-2xl">
325
+ <ShieldCheck className="size-5 text-muted-foreground" />
326
+ Two-factor check
327
+ </CardTitle>
328
+ <CardDescription>
329
+ {useBackupCode
330
+ ? "Enter one of your single-use backup codes."
331
+ : "Enter the 6-digit code from your authenticator app."}
332
+ </CardDescription>
333
+ </CardHeader>
334
+ <CardContent>
335
+ <form onSubmit={submitOtp} className="grid gap-4" noValidate>
336
+ <Field
337
+ label={useBackupCode ? "Backup code" : "Verification code"}
338
+ htmlFor="otp"
339
+ required
340
+ >
341
+ <Input
342
+ id="otp"
343
+ inputMode={useBackupCode ? "text" : "numeric"}
344
+ autoComplete="one-time-code"
345
+ autoFocus
346
+ required
347
+ placeholder={useBackupCode ? "XXXX-XXXX" : "123456"}
348
+ value={otp}
349
+ onChange={(e) => setOtp(e.target.value)}
350
+ className="font-mono"
351
+ />
352
+ </Field>
353
+ {error ? (
354
+ <Alert variant="destructive">
355
+ <AlertCircle />
356
+ <AlertDescription>{error}</AlertDescription>
357
+ </Alert>
358
+ ) : null}
359
+ <Button type="submit" className="w-full" loading={loading}>
360
+ Verify
361
+ </Button>
362
+ <div className="flex items-center justify-between text-sm">
363
+ <button
364
+ type="button"
365
+ className="text-muted-foreground underline-offset-4 hover:underline"
366
+ onClick={() => {
367
+ setUseBackupCode((v) => !v);
368
+ setOtp("");
369
+ setError(null);
370
+ }}
371
+ >
372
+ {useBackupCode ? "Use authenticator code" : "Use a backup code"}
373
+ </button>
374
+ <button
375
+ type="button"
376
+ className="text-muted-foreground underline-offset-4 hover:underline"
377
+ onClick={() => {
378
+ setChallengeToken(null);
379
+ setOtp("");
380
+ setError(null);
381
+ }}
382
+ >
383
+ Back to sign-in
384
+ </button>
385
+ </div>
386
+ </form>
387
+ </CardContent>
388
+ </Card>
389
+ );
390
+ }
391
+
392
+ return (
393
+ <Card className="w-full max-w-md">
394
+ <CardHeader className="space-y-1.5">
395
+ <CardTitle className="text-2xl">{title}</CardTitle>
396
+ {description ? <CardDescription>{description}</CardDescription> : null}
397
+ </CardHeader>
398
+ <CardContent>
399
+ <div className="grid gap-4">
400
+ {providers.length > 0 && (
401
+ <>
402
+ <div className="grid gap-2">
403
+ {providers.map((p) => (
404
+ <Button
405
+ key={p}
406
+ type="button"
407
+ variant="outline"
408
+ className="w-full"
409
+ loading={socialLoading === p}
410
+ disabled={!!socialLoading || loading}
411
+ onClick={() => handleSocial(p)}
412
+ >
413
+ <ProviderIcon provider={p} />
414
+ Continue with {providerLabel[p] ?? p}
415
+ </Button>
416
+ ))}
417
+ </div>
418
+ <div className="relative">
419
+ <div className="absolute inset-0 flex items-center">
420
+ <Separator className="w-full" />
421
+ </div>
422
+ <div className="relative flex justify-center text-xs uppercase">
423
+ <span className="bg-card px-2 text-muted-foreground">or</span>
424
+ </div>
425
+ </div>
426
+ </>
427
+ )}
428
+ <form onSubmit={submit} className="grid gap-4" noValidate>
429
+ <Field label="Email" htmlFor="email" required>
430
+ <div className="relative">
431
+ <Mail
432
+ className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground"
433
+ aria-hidden
434
+ />
435
+ <Input
436
+ id="email"
437
+ type="email"
438
+ autoComplete="email"
439
+ required
440
+ value={email}
441
+ onChange={(e) => setEmail(e.target.value)}
442
+ className="pl-9"
443
+ placeholder="you@example.com"
444
+ />
445
+ </div>
446
+ </Field>
447
+ <Field
448
+ label={
449
+ <span className="flex w-full items-center justify-between">
450
+ <span>Password</span>
451
+ {forgotPasswordSlot}
452
+ </span>
453
+ }
454
+ htmlFor="password"
455
+ required
456
+ >
457
+ <Input
458
+ id="password"
459
+ type="password"
460
+ autoComplete="current-password"
461
+ required
462
+ value={password}
463
+ onChange={(e) => setPassword(e.target.value)}
464
+ />
465
+ </Field>
466
+ {error ? (
467
+ <Alert variant="destructive">
468
+ <AlertCircle />
469
+ <AlertDescription>{error}</AlertDescription>
470
+ </Alert>
471
+ ) : null}
472
+ <Button type="submit" className="w-full" loading={loading}>
473
+ {loading ? "Signing in…" : "Sign in"}
474
+ </Button>
475
+ {webauthnSupported() ? (
476
+ <Button
477
+ type="button"
478
+ variant="outline"
479
+ className="w-full"
480
+ loading={passkeyLoading}
481
+ disabled={loading || !!socialLoading}
482
+ onClick={handlePasskey}
483
+ >
484
+ <Fingerprint />
485
+ Sign in with a passkey
486
+ </Button>
487
+ ) : null}
488
+ <Button
489
+ type="button"
490
+ variant="outline"
491
+ className="w-full"
492
+ disabled={loading || !!socialLoading || passkeyLoading}
493
+ onClick={() => {
494
+ setMagicStep("form");
495
+ setError(null);
496
+ }}
497
+ >
498
+ <Wand2 />
499
+ Email me a sign-in link
500
+ </Button>
501
+ {signUpSlot ? (
502
+ <p className="text-center text-sm text-muted-foreground">{signUpSlot}</p>
503
+ ) : null}
504
+ </form>
505
+ </div>
506
+ </CardContent>
507
+ </Card>
508
+ );
509
+ };
@@ -0,0 +1,123 @@
1
+ import { AlertCircle } from "lucide-react";
2
+ import { useState, type FormEvent, type ReactNode } 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 interface SignUpFormProps {
19
+ onSuccess?: (user: { id: string; email: string }) => void;
20
+ signInSlot?: ReactNode;
21
+ title?: ReactNode;
22
+ description?: ReactNode;
23
+ /** Server enforces ≥8 chars; this is the client-side hint. */
24
+ minPasswordLength?: number;
25
+ }
26
+
27
+ export const SignUpForm = ({
28
+ onSuccess,
29
+ signInSlot,
30
+ title = "Create your account",
31
+ description = "Just a few details to get started.",
32
+ minPasswordLength = 8,
33
+ }: SignUpFormProps) => {
34
+ const client = useUmClient();
35
+ const [name, setName] = useState("");
36
+ const [email, setEmail] = useState("");
37
+ const [password, setPassword] = useState("");
38
+ const [loading, setLoading] = useState(false);
39
+ const [error, setError] = useState<string | null>(null);
40
+
41
+ const tooShort = password.length > 0 && password.length < minPasswordLength;
42
+
43
+ const submit = async (e: FormEvent) => {
44
+ e.preventDefault();
45
+ if (tooShort) return;
46
+ setLoading(true);
47
+ setError(null);
48
+ const res = await client.signUp({
49
+ name: name.trim(),
50
+ email: email.trim(),
51
+ password,
52
+ });
53
+ setLoading(false);
54
+ if (res.ok) {
55
+ onSuccess?.({ id: res.data.user.id, email: res.data.user.email });
56
+ } else {
57
+ setError(errorMessage(res.error, "Sign-up failed. Please try again."));
58
+ }
59
+ };
60
+
61
+ return (
62
+ <Card className="w-full max-w-md">
63
+ <CardHeader className="space-y-1.5">
64
+ <CardTitle className="text-2xl">{title}</CardTitle>
65
+ {description ? <CardDescription>{description}</CardDescription> : null}
66
+ </CardHeader>
67
+ <CardContent>
68
+ <form onSubmit={submit} className="grid gap-4" noValidate>
69
+ <Field label="Name" htmlFor="name" required>
70
+ <Input
71
+ id="name"
72
+ autoComplete="name"
73
+ required
74
+ value={name}
75
+ onChange={(e) => setName(e.target.value)}
76
+ placeholder="Ada Lovelace"
77
+ />
78
+ </Field>
79
+ <Field label="Email" htmlFor="email" required>
80
+ <Input
81
+ id="email"
82
+ type="email"
83
+ autoComplete="email"
84
+ required
85
+ value={email}
86
+ onChange={(e) => setEmail(e.target.value)}
87
+ placeholder="you@example.com"
88
+ />
89
+ </Field>
90
+ <Field
91
+ label="Password"
92
+ htmlFor="password"
93
+ required
94
+ description={`At least ${minPasswordLength} characters.`}
95
+ error={tooShort ? `Password must be at least ${minPasswordLength} characters.` : undefined}
96
+ >
97
+ <Input
98
+ id="password"
99
+ type="password"
100
+ autoComplete="new-password"
101
+ required
102
+ value={password}
103
+ onChange={(e) => setPassword(e.target.value)}
104
+ aria-invalid={tooShort || undefined}
105
+ />
106
+ </Field>
107
+ {error ? (
108
+ <Alert variant="destructive">
109
+ <AlertCircle />
110
+ <AlertDescription>{error}</AlertDescription>
111
+ </Alert>
112
+ ) : null}
113
+ <Button type="submit" className="w-full" loading={loading} disabled={tooShort}>
114
+ {loading ? "Creating account…" : "Create account"}
115
+ </Button>
116
+ {signInSlot ? (
117
+ <p className="text-center text-sm text-muted-foreground">{signInSlot}</p>
118
+ ) : null}
119
+ </form>
120
+ </CardContent>
121
+ </Card>
122
+ );
123
+ };