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