| 1 | "use client"; |
| 2 | |
| 3 | import { getViewURL, validateEmailAddress } from "@better-auth-ui/core"; |
| 4 | import { |
| 5 | useAuth, |
| 6 | useFetchOptions, |
| 7 | useRequestPasswordReset, |
| 8 | } from "@better-auth-ui/react"; |
| 9 | |
| 10 | import { Card, CardContent |
| 11 | import { |
| 12 | Field, |
| 13 | FieldDescription, |
| 14 | FieldGroup, |
| 15 | FieldLabel, |
| 16 | } from "@/components/ui/field"; |
| 17 | import { Input } |
| 18 | import { Spinner } |
| 19 | import { cn } |
| 20 | import { isAuthFormFieldInvalid, useAuthForm |
| 21 | import { RESET_LINK_SENT_STORAGE_KEY } |
| 22 | |
| 23 | export type ForgotPasswordProps |
| 24 | className?: string |
| 25 | }; |
| 26 | |
| 27 | /** Render a card-based "Forgot Password" form that sends a password-reset email. */ |
| 28 | export function ForgotPassword({ |
| 29 | const { |
| 30 | authClient, |
| 31 | baseURL, |
| 32 | basePaths, |
| 33 | localization, |
| 34 | navigate, |
| 35 | plugins, |
| 36 | viewPaths, |
| 37 | Link, |
| 38 | } = useAuth(); |
| 39 | |
| 40 | const { fetchOptions, |
| 41 | |
| 42 | const { mutate: |
| 43 | authClient, |
| 44 | { |
| 45 | onError: () => |
| 46 | resetFetchOptions(); |
| 47 | }, |
| 48 | onSuccess: (_data, |
| 49 | sessionStorage.setItem(RESET_LINK_SENT_STORAGE_KEY |
| 50 | navigate({ to: |
| 51 | }, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | const form = |
| 56 | defaultValues: { email: "" |
| 57 | onSubmit: ({ value |
| 58 | requestPasswordReset({ |
| 59 | email: value.email |
| 60 | redirectTo: getViewURL( |
| 61 | baseURL, |
| 62 | basePaths.auth, |
| 63 | viewPaths.auth.resetPassword |
| 64 | ), |
| 65 | fetchOptions, |
| 66 | }), |
| 67 | }); |
| 68 | |
| 69 | const Captcha = |
| 70 | (plugin) => |
| 71 | )?.captchaComponent; |
| 72 | |
| 73 | return ( |
| 74 | <Card className={ |
| 75 | <CardHeader> |
| 76 | <CardTitle className=" |
| 77 | {localization.auth.forgotPassword |
| 78 | </CardTitle> |
| 79 | </CardHeader> |
| 80 | |
| 81 | <CardContent> |
| 82 | <form.AppForm> |
| 83 | <form.AuthFormRoot> |
| 84 | <FieldGroup> |
| 85 | <form.AppField |
| 86 | name="email" |
| 87 | validators={{ |
| 88 | onChange: ({ value |
| 89 | validateEmailAddress(value, { |
| 90 | invalidMessage: localization.auth |
| 91 | requiredMessage: localization.auth |
| 92 | }), |
| 93 | }} |
| 94 | > |
| 95 | {(field) => |
| 96 | const isInvalid = |
| 97 | return ( |
| 98 | <Field data-invalid={ |
| 99 | <FieldLabel htmlFor=" |
| 100 | {localization.auth.email |
| 101 | </FieldLabel> |
| 102 | <Input |
| 103 | id="email" |
| 104 | name={field.name |
| 105 | type="email" |
| 106 | autoComplete="email" |
| 107 | placeholder={localization.auth |
| 108 | required |
| 109 | disabled={isPending} |
| 110 | value={field.state |
| 111 | onBlur={field.handleBlur |
| 112 | onChange={(event) |
| 113 | field.handleChange(event |
| 114 | } |
| 115 | aria-invalid={isInvalid} |
| 116 | /> |
| 117 | <field.AuthFormFieldError /> |
| 118 | </Field> |
| 119 | ); |
| 120 | }} |
| 121 | </form.AppField> |
| 122 | |
| 123 | {Captcha && <div |
| 124 | |
| 125 | <div className="flex flex-col gap-3 |
| 126 | <form.AuthFormSubmitButton disabled={ |
| 127 | {isPending && <Spinner |
| 128 | {localization.auth.sendResetLink |
| 129 | </form.AuthFormSubmitButton> |
| 130 | </div> |
| 131 | </FieldGroup> |
| 132 | </form.AuthFormRoot> |
| 133 | </form.AppForm> |
| 134 | |
| 135 | <div className="flex flex-col gap-3 items-center w-full mt-4 |
| 136 | <FieldDescription className=" |
| 137 | {localization.auth.rememberYourPassword |
| 138 | <Link |
| 139 | href={`${basePaths |
| 140 | className="underline underline-offset-4" |
| 141 | > |
| 142 | {localization.auth.signIn |
| 143 | </Link> |
| 144 | </FieldDescription> |
| 145 | </div> |
| 146 | </CardContent> |
| 147 | </Card> |
| 148 | ); |
| 149 | } |