| 1 | "use client"; |
| 2 | |
| 3 | import type * as React |
| 4 | import { useState } |
| 5 | import { useRouter } |
| 6 | import { useTranslations } |
| 7 | |
| 8 | import { Button } |
| 9 | import { Input } |
| 10 | import { Label } |
| 11 | |
| 12 | import AuthShell from "@/components/auth-shell |
| 13 | import { authClient } |
| 14 | import { syncLocaleFromDB } |
| 15 | |
| 16 | const emailPattern = / |
| 17 | |
| 18 | function FieldError({ children |
| 19 | return ( |
| 20 | <p className="flex items-center gap-1 text-xs text-destructive |
| 21 | <svg className="h-3 w-3 shrink-0 |
| 22 | <path d="M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1zm-.75 3.75a.75.75 0 0 1 1.5 0v3.5a.75.75 0 0 1-1.5 0v-3.5zm.75 6.5a.875.875 0 1 1 0-1.75.875.875 0 0 1 0 1.75z |
| 23 | </svg> |
| 24 | {children} |
| 25 | </p> |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | export default function RegisterPage |
| 30 | const t = |
| 31 | const router = |
| 32 | const [displayName, |
| 33 | const [email, |
| 34 | const [password, |
| 35 | const [error, |
| 36 | const [isSubmitting, |
| 37 | const [hasSubmitted, |
| 38 | const [touchedEmail, |
| 39 | const [touchedPassword, |
| 40 | |
| 41 | const normalizedEmail = |
| 42 | const emailError = |
| 43 | ? t("emailRequired" |
| 44 | : emailPattern.test( |
| 45 | ? null |
| 46 | : t("emailInvalid" |
| 47 | const passwordError = |
| 48 | const showEmailError = |
| 49 | const showPasswordError = |
| 50 | const isDisabled = |
| 51 | |
| 52 | const handleSubmit = |
| 53 | event.preventDefault(); |
| 54 | setHasSubmitted(true); |
| 55 | setError(null); |
| 56 | if (emailError || |
| 57 | setError(t("fixHighlighted |
| 58 | return; |
| 59 | } |
| 60 | setIsSubmitting(true); |
| 61 | const { error: |
| 62 | email: normalizedEmail, |
| 63 | password, |
| 64 | name: displayName.trim |
| 65 | }); |
| 66 | if (signUpError) { |
| 67 | setIsSubmitting(false); |
| 68 | setError(signUpError.message |
| 69 | return; |
| 70 | } |
| 71 | const { error: |
| 72 | email: normalizedEmail, |
| 73 | password, |
| 74 | }); |
| 75 | setIsSubmitting(false); |
| 76 | if (signInError) { |
| 77 | setError(signInError.message |
| 78 | return; |
| 79 | } |
| 80 | await syncLocaleFromDB(); |
| 81 | router.push("/ |
| 82 | }; |
| 83 | |
| 84 | return ( |
| 85 | <AuthShell title={ |
| 86 | <form className="space-y-3.5 |
| 87 | <div className="grid gap-2 |
| 88 | <Label htmlFor=" |
| 89 | <Input |
| 90 | id="displayName" |
| 91 | name="displayName" |
| 92 | onChange={(event) |
| 93 | placeholder={t(" |
| 94 | type="text" |
| 95 | value={displayName} |
| 96 | /> |
| 97 | </div> |
| 98 | <div className="grid gap-2 |
| 99 | <Label htmlFor=" |
| 100 | <Input |
| 101 | aria-invalid={showEmailError || |
| 102 | id="email" |
| 103 | name="email" |
| 104 | onChange={(event) |
| 105 | onBlur={() => |
| 106 | placeholder={t(" |
| 107 | type="email" |
| 108 | value={email} |
| 109 | /> |
| 110 | {showEmailError ? <FieldError |
| 111 | </div> |
| 112 | <div className="grid gap-2 |
| 113 | <Label htmlFor=" |
| 114 | <Input |
| 115 | aria-invalid={showPasswordError || |
| 116 | id="password" |
| 117 | name="password" |
| 118 | onChange={(event) |
| 119 | onBlur={() => |
| 120 | placeholder={t(" |
| 121 | type="password" |
| 122 | value={password} |
| 123 | /> |
| 124 | {showPasswordError ? <FieldError |
| 125 | </div> |
| 126 | <Button className=" |
| 127 | {isSubmitting ? t |
| 128 | </Button> |
| 129 | {error ? <FieldError |
| 130 | </form> |
| 131 | |
| 132 | <p className="text-center text-sm text-muted-foreground |
| 133 | {t("hasAccount") |
| 134 | <a |
| 135 | className="font-medium text-foreground transition-colors hover:text-foreground/80" |
| 136 | href="/login" |
| 137 | > |
| 138 | {t("signIn") |
| 139 | </a> |
| 140 | </p> |
| 141 | </AuthShell> |
| 142 | ); |
| 143 | } |