| 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 { useAuthSettings } |
| 14 | import { authClient } |
| 15 | import { syncLocaleFromDB } |
| 16 | |
| 17 | const emailPattern = / |
| 18 | |
| 19 | function FieldError({ children |
| 20 | return ( |
| 21 | <p className="flex items-center gap-1 text-xs text-destructive |
| 22 | <svg className="h-3 w-3 shrink-0 |
| 23 | <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 |
| 24 | </svg> |
| 25 | {children} |
| 26 | </p> |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | export default function LoginPage |
| 31 | const t = |
| 32 | const router = |
| 33 | const { smtpEnabled |
| 34 | const [email, |
| 35 | const [password, |
| 36 | const [error, |
| 37 | const [isSubmitting, |
| 38 | const [hasSubmitted, |
| 39 | |
| 40 | const normalizedEmail = |
| 41 | const emailError = |
| 42 | ? t("emailRequired" |
| 43 | : emailPattern.test( |
| 44 | ? null |
| 45 | : t("emailInvalid" |
| 46 | const passwordError = |
| 47 | const showEmailError = |
| 48 | const showPasswordError = |
| 49 | const isDisabled = |
| 50 | |
| 51 | const handleSubmit = |
| 52 | event.preventDefault(); |
| 53 | setHasSubmitted(true); |
| 54 | setError(null); |
| 55 | if (emailError || |
| 56 | setError(t("fixHighlighted |
| 57 | return; |
| 58 | } |
| 59 | setIsSubmitting(true); |
| 60 | const { error: |
| 61 | email: normalizedEmail, |
| 62 | password, |
| 63 | }); |
| 64 | setIsSubmitting(false); |
| 65 | if (signInError) { |
| 66 | setError(signInError.message |
| 67 | return; |
| 68 | } |
| 69 | await syncLocaleFromDB(); |
| 70 | router.push("/ |
| 71 | }; |
| 72 | |
| 73 | return ( |
| 74 | <AuthShell title={ |
| 75 | <form className="space-y-3.5 |
| 76 | <div className="grid gap-2 |
| 77 | <Label htmlFor=" |
| 78 | <Input |
| 79 | aria-invalid={showEmailError || |
| 80 | id="email" |
| 81 | name="email" |
| 82 | onChange={(event) |
| 83 | placeholder={t(" |
| 84 | type="email" |
| 85 | value={email} |
| 86 | /> |
| 87 | {showEmailError ? <FieldError |
| 88 | </div> |
| 89 | <div className="grid gap-2 |
| 90 | <div className="flex items-center justify-between |
| 91 | <Label htmlFor=" |
| 92 | {smtpEnabled ? ( |
| 93 | <a |
| 94 | className="text-xs text-muted-foreground transition-colors hover:text-foreground" |
| 95 | href="/forgot-password" |
| 96 | > |
| 97 | {t("forgotPassword") |
| 98 | </a> |
| 99 | ) : null} |
| 100 | </div> |
| 101 | <Input |
| 102 | aria-invalid={showPasswordError || |
| 103 | id="password" |
| 104 | name="password" |
| 105 | onChange={(event) |
| 106 | placeholder={t(" |
| 107 | type="password" |
| 108 | value={password} |
| 109 | /> |
| 110 | {showPasswordError ? <FieldError |
| 111 | </div> |
| 112 | <Button className=" |
| 113 | {isSubmitting ? t |
| 114 | </Button> |
| 115 | {error ? <FieldError |
| 116 | </form> |
| 117 | |
| 118 | <p className="text-center text-sm text-muted-foreground |
| 119 | {t("noAccount") |
| 120 | <a |
| 121 | className="font-medium text-foreground transition-colors hover:text-foreground/80" |
| 122 | href="/register" |
| 123 | > |
| 124 | {t("register") |
| 125 | </a> |
| 126 | </p> |
| 127 | </AuthShell> |
| 128 | ); |
| 129 | } |