| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | authMutationKeys, |
| 5 | getAdditionalFieldDefaultValues, |
| 6 | getAdditionalFieldSubmitValues, |
| 7 | getAuthLinkURL, |
| 8 | isPasswordCompromisedError, |
| 9 | validateEmailAddress, |
| 10 | validateMatchingValue, |
| 11 | validateStringLength, |
| 12 | } from "@better-auth-ui/core"; |
| 13 | import { |
| 14 | AuthPrompts, |
| 15 | useAuth, |
| 16 | useFetchOptions, |
| 17 | useSignUpEmail, |
| 18 | } from "@better-auth-ui/react"; |
| 19 | import { useIsMutating } |
| 20 | import { Eye, EyeOff |
| 21 | import { useMemo, useState |
| 22 | import { Card, CardContent |
| 23 | import { |
| 24 | Field, |
| 25 | FieldDescription, |
| 26 | FieldError, |
| 27 | FieldGroup, |
| 28 | FieldLabel, |
| 29 | FieldSeparator, |
| 30 | } from "@/components/ui/field"; |
| 31 | import { Input } |
| 32 | import { |
| 33 | InputGroup, |
| 34 | InputGroupAddon, |
| 35 | InputGroupButton, |
| 36 | InputGroupInput, |
| 37 | } from "@/components/ui/input-group"; |
| 38 | import { cn } |
| 39 | import { |
| 40 | getAuthAdditionalFieldValidators, |
| 41 | isAuthFormFieldInvalid, |
| 42 | useAuthForm, |
| 43 | } from "./auth-form"; |
| 44 | import { PasswordStrengthMeter } |
| 45 | import { ProviderButtons, type |
| 46 | |
| 47 | export type SignUpProps |
| 48 | className?: string |
| 49 | socialLayout?: SocialLayout |
| 50 | socialPosition?: " |
| 51 | /** Runs instead of the post-sign-up redirect, but only when the sign-up created an immediately usable session. Email verification still takes priority, and social sign-ups are unaffected. */ |
| 52 | onSignUpSuccess?: () |
| 53 | }; |
| 54 | |
| 55 | /** Renders a sign-up form with name, email, and password fields, optional social provider buttons, and submission handling. */ |
| 56 | export function SignUp({ |
| 57 | className, |
| 58 | socialLayout, |
| 59 | socialPosition = "bottom" |
| 60 | onSignUpSuccess, |
| 61 | }: SignUpProps) |
| 62 | const { |
| 63 | additionalFields, |
| 64 | authClient, |
| 65 | basePaths, |
| 66 | emailAndPassword, |
| 67 | localization, |
| 68 | plugins, |
| 69 | redirectTo, |
| 70 | socialProviders, |
| 71 | viewPaths, |
| 72 | navigate, |
| 73 | Link, |
| 74 | } = useAuth(); |
| 75 | |
| 76 | const { fetchOptions, |
| 77 | |
| 78 | const { mutateAsync: |
| 79 | onError: (error) |
| 80 | // The haveIBeenPwned plugin rejects on the password itself, so it belongs against the field rather than in a toast. |
| 81 | if (isPasswordCompromisedError(error |
| 82 | setIsCompromised(true); |
| 83 | } |
| 84 | |
| 85 | form.setFieldValue("password |
| 86 | form.setFieldValue("confirmPassword |
| 87 | resetFetchOptions(); |
| 88 | }, |
| 89 | onSuccess: (_data, |
| 90 | if (emailAndPassword?.requireEmailVerification |
| 91 | sessionStorage.setItem("better-auth-ui.verify-email |
| 92 | navigate({ |
| 93 | to: getAuthLinkURL( |
| 94 | `${basePaths.auth} |
| 95 | redirectTo, |
| 96 | ), |
| 97 | }); |
| 98 | } else if (onSignUpSuccess |
| 99 | onSignUpSuccess(); |
| 100 | } else { |
| 101 | navigate({ to: |
| 102 | } |
| 103 | }, |
| 104 | }); |
| 105 | |
| 106 | const signInMutating = |
| 107 | mutationKey: authMutationKeys.signIn |
| 108 | }); |
| 109 | const signUpMutating = |
| 110 | mutationKey: authMutationKeys.signUp |
| 111 | }); |
| 112 | const isPending = |
| 113 | |
| 114 | const Captcha = |
| 115 | (plugin) => |
| 116 | )?.captchaComponent; |
| 117 | |
| 118 | const [isPasswordVisible, |
| 119 | const [isConfirmPasswordVisible, |
| 120 | useState(false); |
| 121 | |
| 122 | const [isCompromised, |
| 123 | const signUpFields = |
| 124 | () => additionalFields?. |
| 125 | [additionalFields], |
| 126 | ); |
| 127 | const form = |
| 128 | defaultValues: { |
| 129 | additionalFields: getAdditionalFieldDefaultValues(signUpFields |
| 130 | confirmPassword: "", |
| 131 | email: "", |
| 132 | name: "", |
| 133 | password: "", |
| 134 | }, |
| 135 | onSubmit: async ( |
| 136 | try { |
| 137 | await signUpEmail({ |
| 138 | name: emailAndPassword?.name |
| 139 | email: value.email |
| 140 | password: value.password |
| 141 | ...getAdditionalFieldSubmitValues( |
| 142 | signUpFields, |
| 143 | value.additionalFields, |
| 144 | ), |
| 145 | fetchOptions, |
| 146 | }); |
| 147 | } catch { |
| 148 | // The mutation reports the error through its configured handler. |
| 149 | } |
| 150 | }, |
| 151 | }); |
| 152 | |
| 153 | const showSeparator = |
| 154 | emailAndPassword?.enabled && |
| 155 | |
| 156 | return ( |
| 157 | <Card className={ |
| 158 | <AuthPrompts view=" |
| 159 | <CardHeader> |
| 160 | <CardTitle className=" |
| 161 | {localization.auth.signUp |
| 162 | </CardTitle> |
| 163 | </CardHeader> |
| 164 | |
| 165 | <CardContent> |
| 166 | <div className="flex flex-col gap-6 |
| 167 | {socialPosition === "top |
| 168 | <> |
| 169 | {socialProviders && socialProviders |
| 170 | <ProviderButtons socialLayout={ |
| 171 | )} |
| 172 | |
| 173 | {showSeparator && ( |
| 174 | <FieldSeparator className=" |
| 175 | {localization.auth.or |
| 176 | </FieldSeparator> |
| 177 | )} |
| 178 | </> |
| 179 | )} |
| 180 | |
| 181 | {emailAndPassword?.enabled && |
| 182 | <form.AppForm> |
| 183 | <form.AuthFormRoot> |
| 184 | <FieldGroup> |
| 185 | {emailAndPassword.name !== |
| 186 | <form.AppField |
| 187 | name="name" |
| 188 | validators={{ |
| 189 | onChange: ({ value |
| 190 | validateStringLength(value, { |
| 191 | requiredMessage: localization.auth |
| 192 | trim: true, |
| 193 | }), |
| 194 | }} |
| 195 | > |
| 196 | {(field) => |
| 197 | const isInvalid = |
| 198 | field.state.meta |
| 199 | ); |
| 200 | |
| 201 | return ( |
| 202 | <Field data-invalid={ |
| 203 | <FieldLabel htmlFor=" |
| 204 | {localization.auth.name |
| 205 | </FieldLabel> |
| 206 | |
| 207 | <Input |
| 208 | id="name" |
| 209 | name={field.name |
| 210 | type="text" |
| 211 | autoComplete="name" |
| 212 | placeholder={localization.auth |
| 213 | required |
| 214 | disabled={isPending} |
| 215 | value={field.state |
| 216 | onBlur={field.handleBlur |
| 217 | onChange={(event) |
| 218 | field.handleChange(event |
| 219 | } |
| 220 | aria-invalid={isInvalid} |
| 221 | /> |
| 222 | |
| 223 | <field.AuthFormFieldError /> |
| 224 | </Field> |
| 225 | ); |
| 226 | }} |
| 227 | </form.AppField> |
| 228 | )} |
| 229 | |
| 230 | <form.AppField |
| 231 | name="email" |
| 232 | validators={{ |
| 233 | onChange: ({ value |
| 234 | validateEmailAddress(value, { |
| 235 | invalidMessage: localization.auth |
| 236 | requiredMessage: localization.auth |
| 237 | }), |
| 238 | }} |
| 239 | > |
| 240 | {(field) => |
| 241 | const isInvalid = |
| 242 | field.state.meta |
| 243 | ); |
| 244 | |
| 245 | return ( |
| 246 | <Field data-invalid={ |
| 247 | <FieldLabel htmlFor=" |
| 248 | {localization.auth.email |
| 249 | </FieldLabel> |
| 250 | |
| 251 | <Input |
| 252 | id="email" |
| 253 | name={field.name |
| 254 | type="email" |
| 255 | autoComplete="email" |
| 256 | placeholder={localization.auth |
| 257 | required |
| 258 | disabled={isPending} |
| 259 | value={field.state |
| 260 | onBlur={field.handleBlur |
| 261 | onChange={(event) |
| 262 | field.handleChange(event |
| 263 | } |
| 264 | aria-invalid={isInvalid} |
| 265 | /> |
| 266 | |
| 267 | <field.AuthFormFieldError /> |
| 268 | </Field> |
| 269 | ); |
| 270 | }} |
| 271 | </form.AppField> |
| 272 | |
| 273 | {signUpFields.map( |
| 274 | (configuredField) => |
| 275 | configuredField.signUp === |
| 276 | <form.AppField |
| 277 | key={configuredField.name |
| 278 | name={`additionalFields.${configuredField |
| 279 | validators={getAuthAdditionalFieldValidators( |
| 280 | configuredField, |
| 281 | localization.auth.fieldRequired |
| 282 | )} |
| 283 | > |
| 284 | {(field) => |
| 285 | <field.AuthFormAdditionalField |
| 286 | field={configuredField} |
| 287 | isPending={isPending} |
| 288 | optionalLabel={localization.auth |
| 289 | /> |
| 290 | )} |
| 291 | </form.AppField> |
| 292 | ), |
| 293 | )} |
| 294 | |
| 295 | <form.AppField |
| 296 | name="password" |
| 297 | validators={{ |
| 298 | onChange: ({ value |
| 299 | validateStringLength(value, { |
| 300 | maxLength: emailAndPassword?.maxPasswordLength |
| 301 | maxLengthMessage: localization.auth |
| 302 | "{{max}}", |
| 303 | String(emailAndPassword?.maxPasswordLength |
| 304 | ), |
| 305 | minLength: emailAndPassword?.minPasswordLength |
| 306 | minLengthMessage: localization.auth |
| 307 | "{{min}}", |
| 308 | String(emailAndPassword?.minPasswordLength |
| 309 | ), |
| 310 | requiredMessage: localization.auth |
| 311 | }), |
| 312 | }} |
| 313 | > |
| 314 | {(field) => |
| 315 | const isInvalid = |
| 316 | isAuthFormFieldInvalid(field.state |
| 317 | isCompromised; |
| 318 | |
| 319 | return ( |
| 320 | <Field data-invalid={ |
| 321 | <FieldLabel htmlFor=" |
| 322 | {localization.auth.password |
| 323 | </FieldLabel> |
| 324 | |
| 325 | <InputGroup> |
| 326 | <InputGroupInput |
| 327 | id="password" |
| 328 | name={field.name |
| 329 | type={isPasswordVisible ? |
| 330 | autoComplete="new-password" |
| 331 | value={field.state |
| 332 | onBlur={field.handleBlur |
| 333 | onChange={(e) |
| 334 | field.handleChange(e |
| 335 | setIsCompromised(false); |
| 336 | }} |
| 337 | placeholder={ |
| 338 | localization.auth.passwordPlaceholder |
| 339 | } |
| 340 | required |
| 341 | minLength={emailAndPassword?.minPasswordLength |
| 342 | maxLength={emailAndPassword?.maxPasswordLength |
| 343 | disabled={isPending} |
| 344 | aria-invalid={isInvalid} |
| 345 | /> |
| 346 | |
| 347 | <InputGroupAddon align=" |
| 348 | <InputGroupButton |
| 349 | size="icon-xs" |
| 350 | aria-label={ |
| 351 | isPasswordVisible |
| 352 | ? localization.auth. |
| 353 | : localization.auth. |
| 354 | } |
| 355 | title={ |
| 356 | isPasswordVisible |
| 357 | ? localization.auth. |
| 358 | : localization.auth. |
| 359 | } |
| 360 | onClick={() => |
| 361 | setIsPasswordVisible((visible) |
| 362 | }} |
| 363 | > |
| 364 | {isPasswordVisible ? <EyeOff |
| 365 | </InputGroupButton> |
| 366 | </InputGroupAddon> |
| 367 | </InputGroup> |
| 368 | |
| 369 | {isCompromised ? ( |
| 370 | <FieldError> |
| 371 | {localization.auth.passwordCompromised |
| 372 | </FieldError> |
| 373 | ) : ( |
| 374 | <field.AuthFormFieldError /> |
| 375 | )} |
| 376 | |
| 377 | <PasswordStrengthMeter password={ |
| 378 | </Field> |
| 379 | ); |
| 380 | }} |
| 381 | </form.AppField> |
| 382 | |
| 383 | {emailAndPassword?.confirmPassword && |
| 384 | <form.AppField |
| 385 | name="confirmPassword" |
| 386 | validators={{ |
| 387 | onChangeListenTo: ["password"], |
| 388 | onChange: ({ fieldApi |
| 389 | validateStringLength(value, { |
| 390 | maxLength: emailAndPassword?.maxPasswordLength |
| 391 | maxLengthMessage: localization.auth |
| 392 | "{{max}}", |
| 393 | String(emailAndPassword?.maxPasswordLength |
| 394 | ), |
| 395 | minLength: emailAndPassword?.minPasswordLength |
| 396 | minLengthMessage: |
| 397 | localization.auth.tooShort |
| 398 | "{{min}}", |
| 399 | String(emailAndPassword?.minPasswordLength |
| 400 | ), |
| 401 | requiredMessage: localization.auth |
| 402 | }) ?? |
| 403 | validateMatchingValue( |
| 404 | value, |
| 405 | fieldApi.form.getFieldValue |
| 406 | localization.auth.passwordsDoNotMatch |
| 407 | ), |
| 408 | }} |
| 409 | > |
| 410 | {(field) => |
| 411 | const isInvalid = |
| 412 | field.state.meta |
| 413 | ); |
| 414 | |
| 415 | return ( |
| 416 | <Field data-invalid={ |
| 417 | <FieldLabel htmlFor=" |
| 418 | {localization.auth.confirmPassword |
| 419 | </FieldLabel> |
| 420 | |
| 421 | <InputGroup> |
| 422 | <InputGroupInput |
| 423 | id="confirmPassword" |
| 424 | name={field.name |
| 425 | type={ |
| 426 | isConfirmPasswordVisible ? "text" |
| 427 | } |
| 428 | autoComplete="new-password" |
| 429 | value={field.state |
| 430 | onBlur={field.handleBlur |
| 431 | onChange={(e) |
| 432 | field.handleChange(e |
| 433 | } |
| 434 | placeholder={ |
| 435 | localization.auth.confirmPasswordPlaceholder |
| 436 | } |
| 437 | required |
| 438 | minLength={emailAndPassword?.minPasswordLength |
| 439 | maxLength={emailAndPassword?.maxPasswordLength |
| 440 | disabled={isPending} |
| 441 | aria-invalid={isInvalid} |
| 442 | /> |
| 443 | |
| 444 | <InputGroupAddon align=" |
| 445 | <InputGroupButton |
| 446 | size="icon-xs" |
| 447 | aria-label={ |
| 448 | isConfirmPasswordVisible |
| 449 | ? localization.auth. |
| 450 | : localization.auth. |
| 451 | } |
| 452 | title={ |
| 453 | isConfirmPasswordVisible |
| 454 | ? localization.auth. |
| 455 | : localization.auth. |
| 456 | } |
| 457 | onClick={() => |
| 458 | setIsConfirmPasswordVisible( |
| 459 | (visible) => |
| 460 | ) |
| 461 | } |
| 462 | > |
| 463 | {isConfirmPasswordVisible ? ( |
| 464 | <EyeOff /> |
| 465 | ) : ( |
| 466 | <Eye /> |
| 467 | )} |
| 468 | </InputGroupButton> |
| 469 | </InputGroupAddon> |
| 470 | </InputGroup> |
| 471 | |
| 472 | <field.AuthFormFieldError /> |
| 473 | </Field> |
| 474 | ); |
| 475 | }} |
| 476 | </form.AppField> |
| 477 | )} |
| 478 | |
| 479 | {signUpFields.map( |
| 480 | (configuredField) => |
| 481 | configuredField.signUp !== |
| 482 | <form.AppField |
| 483 | key={configuredField.name |
| 484 | name={`additionalFields.${configuredField |
| 485 | validators={getAuthAdditionalFieldValidators( |
| 486 | configuredField, |
| 487 | localization.auth.fieldRequired |
| 488 | )} |
| 489 | > |
| 490 | {(field) => |
| 491 | <field.AuthFormAdditionalField |
| 492 | field={configuredField} |
| 493 | isPending={isPending} |
| 494 | optionalLabel={localization.auth |
| 495 | /> |
| 496 | )} |
| 497 | </form.AppField> |
| 498 | ), |
| 499 | )} |
| 500 | |
| 501 | {Captcha && ( |
| 502 | <div className="flex justify-center |
| 503 | )} |
| 504 | |
| 505 | <div className="flex flex-col gap-3 |
| 506 | <form.AuthFormSubmitButton disabled={ |
| 507 | {localization.auth.signUp |
| 508 | </form.AuthFormSubmitButton> |
| 509 | |
| 510 | {plugins.flatMap(( |
| 511 | (plugin.authButtons ?? |
| 512 | <AuthButton |
| 513 | key={`${plugin |
| 514 | view="signUp" |
| 515 | /> |
| 516 | )), |
| 517 | )} |
| 518 | </div> |
| 519 | </FieldGroup> |
| 520 | </form.AuthFormRoot> |
| 521 | </form.AppForm> |
| 522 | )} |
| 523 | |
| 524 | {socialPosition === "bottom |
| 525 | <> |
| 526 | {showSeparator && ( |
| 527 | <FieldSeparator className=" |
| 528 | {localization.auth.or |
| 529 | </FieldSeparator> |
| 530 | )} |
| 531 | |
| 532 | {socialProviders && socialProviders |
| 533 | <ProviderButtons socialLayout={ |
| 534 | )} |
| 535 | </> |
| 536 | )} |
| 537 | </div> |
| 538 | |
| 539 | {emailAndPassword?.enabled && |
| 540 | <div className="flex flex-col gap-3 items-center w-full mt-4 |
| 541 | <FieldDescription className=" |
| 542 | {localization.auth.alreadyHaveAnAccount |
| 543 | <Link |
| 544 | href={getAuthLinkURL( |
| 545 | `${basePaths.auth} |
| 546 | redirectTo, |
| 547 | )} |
| 548 | className="underline underline-offset-4" |
| 549 | > |
| 550 | {localization.auth.signIn |
| 551 | </Link> |
| 552 | </FieldDescription> |
| 553 | </div> |
| 554 | )} |
| 555 | </CardContent> |
| 556 | </Card> |
| 557 | ); |
| 558 | } |