| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | getViewURL, |
| 5 | isPasswordCompromisedError, |
| 6 | validateStringLength, |
| 7 | } from "@better-auth-ui/core"; |
| 8 | import { |
| 9 | useAuth, |
| 10 | useChangePassword, |
| 11 | useFetchOptions, |
| 12 | useListAccounts, |
| 13 | useRequestPasswordReset, |
| 14 | useSession, |
| 15 | } from "@better-auth-ui/react"; |
| 16 | import { useState } |
| 17 | import { toast } |
| 18 | import { Button } |
| 19 | import { Card, CardContent |
| 20 | import { Spinner } |
| 21 | import { useAuthForm, usePasswordValidator |
| 22 | import { OpenEmailButton } |
| 23 | import { PasswordStrengthMeter } |
| 24 | |
| 25 | export function ChangePassword() |
| 26 | const { authClient |
| 27 | const { data: |
| 28 | const { data: |
| 29 | useListAccounts(authClient); |
| 30 | |
| 31 | const hasCredentialAccount = |
| 32 | (account) => |
| 33 | ); |
| 34 | |
| 35 | if (!isAccountsPending && |
| 36 | return <SetPassword /> |
| 37 | } |
| 38 | |
| 39 | return ( |
| 40 | <ChangePasswordForm session={ |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | function SetPassword() { |
| 45 | const { authClient, |
| 46 | const { data: |
| 47 | const { fetchOptions, |
| 48 | const [sentEmail, |
| 49 | |
| 50 | const { mutate: |
| 51 | authClient, |
| 52 | { |
| 53 | onError: () => |
| 54 | resetFetchOptions(); |
| 55 | }, |
| 56 | onSuccess: (_data, |
| 57 | setSentEmail(email); |
| 58 | }, |
| 59 | }, |
| 60 | ); |
| 61 | |
| 62 | const handleSetPassword = |
| 63 | if (!session) |
| 64 | |
| 65 | requestPasswordReset({ |
| 66 | email: session.user |
| 67 | redirectTo: getViewURL( |
| 68 | baseURL, |
| 69 | basePaths.auth, |
| 70 | viewPaths.auth.resetPassword |
| 71 | ), |
| 72 | fetchOptions, |
| 73 | }); |
| 74 | }; |
| 75 | |
| 76 | return ( |
| 77 | <div> |
| 78 | <h2 className="text-sm font-semibold mb-3 |
| 79 | {localization.settings.changePassword |
| 80 | </h2> |
| 81 | |
| 82 | <Card> |
| 83 | <CardContent className=" |
| 84 | <div> |
| 85 | <p className="text-sm font-medium leading-tight |
| 86 | {localization.settings.setPassword |
| 87 | </p> |
| 88 | |
| 89 | <p className="text-muted-foreground text-xs mt-0.5 |
| 90 | {localization.settings.setPasswordDescription |
| 91 | </p> |
| 92 | </div> |
| 93 | |
| 94 | {sentEmail ? ( |
| 95 | <div className="flex flex-col gap-3 items-start sm:items-end |
| 96 | <p className="text-sm |
| 97 | {localization.auth.resetLinkSentTo |
| 98 | "{{email}}", |
| 99 | sentEmail, |
| 100 | )} |
| 101 | </p> |
| 102 | |
| 103 | <OpenEmailButton email={ |
| 104 | </div> |
| 105 | ) : ( |
| 106 | <div className="flex flex-col gap-3 items-start sm:items-end |
| 107 | <Button |
| 108 | size="sm" |
| 109 | disabled={isPending || |
| 110 | onClick={handleSetPassword} |
| 111 | > |
| 112 | {isPending && <Spinner |
| 113 | |
| 114 | {localization.auth.sendResetLink |
| 115 | </Button> |
| 116 | </div> |
| 117 | )} |
| 118 | </CardContent> |
| 119 | </Card> |
| 120 | </div> |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | function ChangePasswordForm({ |
| 125 | session, |
| 126 | }: { |
| 127 | session: ReturnType |
| 128 | }) { |
| 129 | const { authClient, |
| 130 | const passwordValidator = |
| 131 | const { mutateAsync: |
| 132 | authClient, |
| 133 | { |
| 134 | onError: (error) |
| 135 | // The haveIBeenPwned plugin rejects on the password itself, so it belongs against the field rather than in a toast. |
| 136 | setIsCompromised(isPasswordCompromisedError(error |
| 137 | }, |
| 138 | onSuccess: () => |
| 139 | form.reset(); |
| 140 | toast.success(localization |
| 141 | }, |
| 142 | }, |
| 143 | ); |
| 144 | |
| 145 | const [isCompromised, |
| 146 | |
| 147 | const form = |
| 148 | defaultValues: { |
| 149 | currentPassword: "", |
| 150 | newPassword: "", |
| 151 | }, |
| 152 | onSubmit: async ( |
| 153 | try { |
| 154 | await changePassword({ |
| 155 | currentPassword: value.currentPassword |
| 156 | newPassword: value.newPassword |
| 157 | revokeOtherSessions: true, |
| 158 | }); |
| 159 | } catch { |
| 160 | // The mutation reports the error through its configured handler. |
| 161 | } |
| 162 | }, |
| 163 | }); |
| 164 | |
| 165 | return ( |
| 166 | <div> |
| 167 | <h2 className="text-sm font-semibold mb-3 |
| 168 | {localization.settings.changePassword |
| 169 | </h2> |
| 170 | |
| 171 | <form.AppForm> |
| 172 | <form.AuthFormRoot> |
| 173 | <Card> |
| 174 | <CardContent className=" |
| 175 | <form.AppField |
| 176 | name="currentPassword" |
| 177 | validators={{ |
| 178 | onChange: ({ value |
| 179 | validateStringLength(value, { |
| 180 | requiredMessage: localization.auth |
| 181 | }), |
| 182 | }} |
| 183 | > |
| 184 | {(field) => |
| 185 | <field.AuthFormPasswordField |
| 186 | label={localization.settings |
| 187 | autoComplete="current-password" |
| 188 | placeholder={ |
| 189 | localization.settings.currentPasswordPlaceholder |
| 190 | } |
| 191 | disabled={isPending} |
| 192 | isLoading={!session} |
| 193 | /> |
| 194 | )} |
| 195 | </form.AppField> |
| 196 | |
| 197 | <form.AppField |
| 198 | name="newPassword" |
| 199 | validators={{ onChange: |
| 200 | > |
| 201 | {(field) => |
| 202 | <field.AuthFormPasswordField |
| 203 | label={localization.auth |
| 204 | autoComplete="new-password" |
| 205 | placeholder={localization.auth |
| 206 | disabled={isPending} |
| 207 | isLoading={!session} |
| 208 | isCompromised={isCompromised} |
| 209 | onValueChange={() => |
| 210 | > |
| 211 | <PasswordStrengthMeter password={ |
| 212 | </field.AuthFormPasswordField> |
| 213 | )} |
| 214 | </form.AppField> |
| 215 | </CardContent> |
| 216 | |
| 217 | <CardFooter> |
| 218 | <form.AuthFormSubmitButton |
| 219 | disabled={isPending || |
| 220 | size="sm" |
| 221 | > |
| 222 | {localization.settings.updatePassword |
| 223 | </form.AuthFormSubmitButton> |
| 224 | </CardFooter> |
| 225 | </Card> |
| 226 | </form.AuthFormRoot> |
| 227 | </form.AppForm> |
| 228 | </div> |
| 229 | ); |
| 230 | } |