| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | authMutationKeys, |
| 5 | getAdditionalFieldDefaultValues, |
| 6 | getAdditionalFieldSubmitValues, |
| 7 | getAuthLinkURL, |
| 8 | isPasswordCompromisedError, |
| 9 | validateEmailAddress, |
| 10 | validateStringLength, |
| 11 | } from "@better-auth-ui/core"; |
| 12 | import { |
| 13 | AuthPrompts, |
| 14 | useAuth, |
| 15 | useFetchOptions, |
| 16 | useSignUpEmail, |
| 17 | } from "@better-auth-ui/react"; |
| 18 | import { useIsMutating } |
| 19 | import { useMemo, useState |
| 20 | import { Card, CardContent |
| 21 | import { |
| 22 | FieldDescription, |
| 23 | FieldGroup, |
| 24 | FieldSeparator, |
| 25 | } from "@/components/ui/field"; |
| 26 | import { |
| 27 | getAuthAdditionalFieldValidators, |
| 28 | useAuthForm, |
| 29 | usePasswordValidator, |
| 30 | } from "./auth-form"; |
| 31 | import { PasswordStrengthMeter } |
| 32 | import { ProviderButtons } |
| 33 | |
| 34 | export function SignUp() |
| 35 | const { |
| 36 | additionalFields, |
| 37 | authClient, |
| 38 | basePaths, |
| 39 | localization, |
| 40 | redirectTo, |
| 41 | socialProviders, |
| 42 | viewPaths, |
| 43 | navigate, |
| 44 | Link, |
| 45 | } = useAuth(); |
| 46 | |
| 47 | const { fetchOptions, |
| 48 | const passwordValidator = |
| 49 | |
| 50 | const { mutateAsync: |
| 51 | onError: (error) |
| 52 | // The haveIBeenPwned plugin rejects on the password itself, so it belongs against the field rather than in a toast. |
| 53 | if (isPasswordCompromisedError(error |
| 54 | setIsCompromised(true); |
| 55 | } |
| 56 | |
| 57 | form.setFieldValue("password |
| 58 | resetFetchOptions(); |
| 59 | }, |
| 60 | onSuccess: () => |
| 61 | }); |
| 62 | |
| 63 | const signInMutating = |
| 64 | mutationKey: authMutationKeys.signIn |
| 65 | }); |
| 66 | const signUpMutating = |
| 67 | mutationKey: authMutationKeys.signUp |
| 68 | }); |
| 69 | const isPending = |
| 70 | |
| 71 | const [isCompromised, |
| 72 | const signUpFields = |
| 73 | () => additionalFields?. |
| 74 | [additionalFields], |
| 75 | ); |
| 76 | const form = |
| 77 | defaultValues: { |
| 78 | additionalFields: getAdditionalFieldDefaultValues(signUpFields |
| 79 | email: "", |
| 80 | name: "", |
| 81 | password: "", |
| 82 | }, |
| 83 | onSubmit: async ( |
| 84 | try { |
| 85 | await signUpEmail({ |
| 86 | name: value.name |
| 87 | email: value.email |
| 88 | password: value.password |
| 89 | ...getAdditionalFieldSubmitValues( |
| 90 | signUpFields, |
| 91 | value.additionalFields, |
| 92 | ), |
| 93 | fetchOptions, |
| 94 | }); |
| 95 | } catch { |
| 96 | // The mutation reports the error through its configured handler. |
| 97 | } |
| 98 | }, |
| 99 | }); |
| 100 | |
| 101 | return ( |
| 102 | <Card className=" |
| 103 | <AuthPrompts view=" |
| 104 | <CardHeader> |
| 105 | <CardTitle className=" |
| 106 | {localization.auth.signUp |
| 107 | </CardTitle> |
| 108 | </CardHeader> |
| 109 | |
| 110 | <CardContent> |
| 111 | <div className="flex flex-col gap-6 |
| 112 | <form.AppForm> |
| 113 | <form.AuthFormRoot> |
| 114 | <FieldGroup> |
| 115 | <form.AppField |
| 116 | name="name" |
| 117 | validators={{ |
| 118 | onChange: ({ value |
| 119 | validateStringLength(value, { |
| 120 | requiredMessage: localization.auth |
| 121 | trim: true, |
| 122 | }), |
| 123 | }} |
| 124 | > |
| 125 | {(field) => |
| 126 | <field.AuthFormTextField |
| 127 | label={localization.auth |
| 128 | type="text" |
| 129 | autoComplete="name" |
| 130 | placeholder={localization.auth |
| 131 | required |
| 132 | disabled={isPending} |
| 133 | /> |
| 134 | )} |
| 135 | </form.AppField> |
| 136 | |
| 137 | <form.AppField |
| 138 | name="email" |
| 139 | validators={{ |
| 140 | onChange: ({ value |
| 141 | validateEmailAddress(value, { |
| 142 | invalidMessage: localization.auth |
| 143 | requiredMessage: localization.auth |
| 144 | }), |
| 145 | }} |
| 146 | > |
| 147 | {(field) => |
| 148 | <field.AuthFormTextField |
| 149 | label={localization.auth |
| 150 | type="email" |
| 151 | autoComplete="email" |
| 152 | placeholder={localization.auth |
| 153 | required |
| 154 | disabled={isPending} |
| 155 | /> |
| 156 | )} |
| 157 | </form.AppField> |
| 158 | |
| 159 | {signUpFields.map(( |
| 160 | <form.AppField |
| 161 | key={configuredField.name |
| 162 | name={`additionalFields.${configuredField |
| 163 | validators={getAuthAdditionalFieldValidators( |
| 164 | configuredField, |
| 165 | localization.auth.fieldRequired |
| 166 | )} |
| 167 | > |
| 168 | {(field) => |
| 169 | <field.AuthFormAdditionalField |
| 170 | field={configuredField} |
| 171 | isPending={isPending} |
| 172 | optionalLabel={localization.auth |
| 173 | /> |
| 174 | )} |
| 175 | </form.AppField> |
| 176 | ))} |
| 177 | |
| 178 | <form.AppField |
| 179 | name="password" |
| 180 | validators={{ onChange: |
| 181 | > |
| 182 | {(field) => |
| 183 | <field.AuthFormPasswordField |
| 184 | label={localization.auth |
| 185 | autoComplete="new-password" |
| 186 | disabled={isPending} |
| 187 | isCompromised={isCompromised} |
| 188 | onValueChange={() => |
| 189 | > |
| 190 | <PasswordStrengthMeter password={ |
| 191 | </field.AuthFormPasswordField> |
| 192 | )} |
| 193 | </form.AppField> |
| 194 | |
| 195 | <form.AuthFormSubmitButton disabled={ |
| 196 | {localization.auth.signUp |
| 197 | </form.AuthFormSubmitButton> |
| 198 | </FieldGroup> |
| 199 | </form.AuthFormRoot> |
| 200 | </form.AppForm> |
| 201 | |
| 202 | {socialProviders && socialProviders |
| 203 | <> |
| 204 | <FieldSeparator className=" |
| 205 | {localization.auth.or |
| 206 | </FieldSeparator> |
| 207 | <ProviderButtons view=" |
| 208 | </> |
| 209 | )} |
| 210 | </div> |
| 211 | |
| 212 | <div className="flex flex-col gap-3 items-center w-full mt-4 |
| 213 | <FieldDescription className=" |
| 214 | {localization.auth.alreadyHaveAnAccount |
| 215 | <Link |
| 216 | href={getAuthLinkURL( |
| 217 | `${basePaths.auth} |
| 218 | redirectTo, |
| 219 | )} |
| 220 | className="underline underline-offset-4" |
| 221 | > |
| 222 | {localization.auth.signIn |
| 223 | </Link> |
| 224 | </FieldDescription> |
| 225 | </div> |
| 226 | </CardContent> |
| 227 | </Card> |
| 228 | ); |
| 229 | } |