| 1 | "use client"; |
| 2 | |
| 3 | import { authMutationKeys, validateStringLength |
| 4 | import { |
| 5 | isPasskeyAutoFillEnabled, |
| 6 | withPasskeyAutoFill, |
| 7 | } from "@better-auth-ui/core/plugins/passkey"; |
| 8 | import type { UsernameAuthClient |
| 9 | import { |
| 10 | AuthPrompts, |
| 11 | useAuth, |
| 12 | useAuthPlugin, |
| 13 | useFetchOptions, |
| 14 | useSignInEmail, |
| 15 | } from "@better-auth-ui/react"; |
| 16 | import { useSignInUsername } |
| 17 | import { useIsMutating } |
| 18 | import { Eye, EyeOff |
| 19 | import { useState } |
| 20 | import { |
| 21 | ProviderButtons, |
| 22 | type SocialLayout, |
| 23 | } from "@/components/auth/provider-buttons"; |
| 24 | import { Card, CardContent |
| 25 | import { Checkbox } |
| 26 | import { |
| 27 | Field, |
| 28 | FieldDescription, |
| 29 | FieldGroup, |
| 30 | FieldLabel, |
| 31 | FieldSeparator, |
| 32 | } from "@/components/ui/field"; |
| 33 | import { Input } |
| 34 | import { |
| 35 | InputGroup, |
| 36 | InputGroupAddon, |
| 37 | InputGroupButton, |
| 38 | InputGroupInput, |
| 39 | } from "@/components/ui/input-group"; |
| 40 | import { Spinner } |
| 41 | import { useSignInContinuation } |
| 42 | import { usernamePlugin } |
| 43 | import { cn } |
| 44 | import { isAuthFormFieldInvalid, useAuthForm |
| 45 | import { LastUsedBadge } |
| 46 | |
| 47 | export type SignInUsernameProps |
| 48 | className?: string |
| 49 | socialLayout?: SocialLayout |
| 50 | socialPosition?: " |
| 51 | }; |
| 52 | |
| 53 | function isEmail(value: |
| 54 | return /^[^\s |
| 55 | } |
| 56 | |
| 57 | /** Render the username-based sign-in form. Identical to the built-in `<SignIn>` design but routes non-email inputs through `signInUsername` instead of `signInEmail`. */ |
| 58 | export function SignInUsername({ |
| 59 | className, |
| 60 | socialLayout, |
| 61 | socialPosition = "bottom" |
| 62 | }: SignInUsernameProps) |
| 63 | const { |
| 64 | authClient, |
| 65 | basePaths, |
| 66 | emailAndPassword, |
| 67 | localization, |
| 68 | plugins, |
| 69 | socialProviders, |
| 70 | viewPaths, |
| 71 | navigate, |
| 72 | Link, |
| 73 | } = useAuth<UsernameAuthClient |
| 74 | |
| 75 | const { fetchOptions, |
| 76 | const continueSignIn = |
| 77 | |
| 78 | const { localization: |
| 79 | |
| 80 | const { mutate: |
| 81 | useSignInEmail(authClient, { |
| 82 | onError: (error, |
| 83 | form.setFieldValue("password |
| 84 | |
| 85 | if (error.error |
| 86 | sessionStorage.setItem("better-auth-ui.verify-email |
| 87 | navigate({ |
| 88 | to: `${basePaths. |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | resetFetchOptions(); |
| 93 | }, |
| 94 | onSuccess: (data) |
| 95 | sessionStorage.removeItem("better-auth-ui.verify-email |
| 96 | continueSignIn(data); |
| 97 | }, |
| 98 | }); |
| 99 | |
| 100 | const { mutate: |
| 101 | useSignInUsername(authClient, { |
| 102 | onError: (error) |
| 103 | form.setFieldValue("password |
| 104 | |
| 105 | if (error.error |
| 106 | sessionStorage.removeItem("better-auth-ui.verify-email |
| 107 | |
| 108 | navigate({ |
| 109 | to: `${basePaths. |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | resetFetchOptions(); |
| 114 | }, |
| 115 | onSuccess: (data) |
| 116 | sessionStorage.removeItem("better-auth-ui.verify-email |
| 117 | continueSignIn(data); |
| 118 | }, |
| 119 | }); |
| 120 | |
| 121 | const signInMutating = |
| 122 | mutationKey: authMutationKeys.signIn |
| 123 | }); |
| 124 | const signUpMutating = |
| 125 | mutationKey: authMutationKeys.signUp |
| 126 | }); |
| 127 | const isPending = |
| 128 | const isSignInPending = |
| 129 | |
| 130 | const Captcha = |
| 131 | (plugin) => |
| 132 | )?.captchaComponent; |
| 133 | |
| 134 | const passkeyAutoFill = |
| 135 | |
| 136 | const [isPasswordVisible, |
| 137 | const form = |
| 138 | defaultValues: { identifier: "" |
| 139 | onSubmit: ({ value |
| 140 | if (isEmail(value |
| 141 | signInEmail({ |
| 142 | email: value.identifier |
| 143 | password: value.password |
| 144 | ...(emailAndPassword?.rememberMe |
| 145 | ? { rememberMe: value |
| 146 | : {}), |
| 147 | fetchOptions, |
| 148 | }); |
| 149 | } else { |
| 150 | signInUsername({ |
| 151 | username: value.identifier |
| 152 | password: value.password |
| 153 | ...(emailAndPassword?.rememberMe |
| 154 | ? { rememberMe: value |
| 155 | : {}), |
| 156 | fetchOptions, |
| 157 | }); |
| 158 | } |
| 159 | }, |
| 160 | }); |
| 161 | |
| 162 | const showSeparator = |
| 163 | emailAndPassword?.enabled && |
| 164 | |
| 165 | return ( |
| 166 | <Card className={ |
| 167 | <AuthPrompts view=" |
| 168 | <CardHeader> |
| 169 | <CardTitle className=" |
| 170 | {localization.auth.signIn |
| 171 | </CardTitle> |
| 172 | </CardHeader> |
| 173 | |
| 174 | <CardContent> |
| 175 | <div className="flex flex-col gap-6 |
| 176 | {socialPosition === "top |
| 177 | <> |
| 178 | {socialProviders && socialProviders |
| 179 | <ProviderButtons socialLayout={ |
| 180 | )} |
| 181 | |
| 182 | {showSeparator && ( |
| 183 | <FieldSeparator className=" |
| 184 | {localization.auth.or |
| 185 | </FieldSeparator> |
| 186 | )} |
| 187 | </> |
| 188 | )} |
| 189 | |
| 190 | {emailAndPassword?.enabled && |
| 191 | <form.AppForm> |
| 192 | <form.AuthFormRoot> |
| 193 | <FieldGroup> |
| 194 | <form.AppField |
| 195 | name="identifier" |
| 196 | validators={{ |
| 197 | onChange: ({ value |
| 198 | validateStringLength(value, { |
| 199 | requiredMessage: localization.auth |
| 200 | trim: true, |
| 201 | }), |
| 202 | }} |
| 203 | > |
| 204 | {(field) => |
| 205 | const isInvalid = |
| 206 | field.state.meta |
| 207 | ); |
| 208 | return ( |
| 209 | <Field data-invalid={ |
| 210 | <FieldLabel htmlFor=" |
| 211 | {usernameLocalization.username} |
| 212 | </FieldLabel> |
| 213 | |
| 214 | <Input |
| 215 | id="email" |
| 216 | name={field.name |
| 217 | type="text" |
| 218 | autoComplete={withPasskeyAutoFill( |
| 219 | "username", |
| 220 | passkeyAutoFill, |
| 221 | )} |
| 222 | placeholder={ |
| 223 | usernameLocalization.usernameOrEmailPlaceholder |
| 224 | } |
| 225 | required |
| 226 | disabled={isPending} |
| 227 | value={field.state |
| 228 | onBlur={field.handleBlur |
| 229 | onChange={(event) |
| 230 | field.handleChange(event |
| 231 | } |
| 232 | aria-invalid={isInvalid} |
| 233 | /> |
| 234 | <field.AuthFormFieldError /> |
| 235 | </Field> |
| 236 | ); |
| 237 | }} |
| 238 | </form.AppField> |
| 239 | |
| 240 | <form.AppField |
| 241 | name="password" |
| 242 | validators={{ |
| 243 | onChange: ({ value |
| 244 | validateStringLength(value, { |
| 245 | maxLength: emailAndPassword?.maxPasswordLength |
| 246 | maxLengthMessage: localization.auth |
| 247 | "{{max}}", |
| 248 | String(emailAndPassword?.maxPasswordLength |
| 249 | ), |
| 250 | minLength: emailAndPassword?.minPasswordLength |
| 251 | minLengthMessage: localization.auth |
| 252 | "{{min}}", |
| 253 | String(emailAndPassword?.minPasswordLength |
| 254 | ), |
| 255 | requiredMessage: localization.auth |
| 256 | }), |
| 257 | }} |
| 258 | > |
| 259 | {(field) => |
| 260 | const isInvalid = |
| 261 | field.state.meta |
| 262 | ); |
| 263 | return ( |
| 264 | <Field data-invalid={ |
| 265 | <FieldLabel htmlFor=" |
| 266 | {localization.auth.password |
| 267 | </FieldLabel> |
| 268 | |
| 269 | <InputGroup> |
| 270 | <InputGroupInput |
| 271 | id="password" |
| 272 | name={field.name |
| 273 | type={isPasswordVisible ? |
| 274 | autoComplete={withPasskeyAutoFill( |
| 275 | "current-password", |
| 276 | passkeyAutoFill, |
| 277 | )} |
| 278 | value={field.state |
| 279 | onBlur={field.handleBlur |
| 280 | onChange={(event) |
| 281 | field.handleChange(event |
| 282 | } |
| 283 | placeholder={ |
| 284 | localization.auth.passwordPlaceholder |
| 285 | } |
| 286 | required |
| 287 | minLength={emailAndPassword?.minPasswordLength |
| 288 | maxLength={emailAndPassword?.maxPasswordLength |
| 289 | disabled={isPending} |
| 290 | aria-invalid={isInvalid} |
| 291 | /> |
| 292 | |
| 293 | <InputGroupAddon align=" |
| 294 | <InputGroupButton |
| 295 | size="icon-xs" |
| 296 | aria-label={ |
| 297 | isPasswordVisible |
| 298 | ? localization.auth. |
| 299 | : localization.auth. |
| 300 | } |
| 301 | title={ |
| 302 | isPasswordVisible |
| 303 | ? localization.auth. |
| 304 | : localization.auth. |
| 305 | } |
| 306 | onClick={() => |
| 307 | setIsPasswordVisible((visible) |
| 308 | }} |
| 309 | > |
| 310 | {isPasswordVisible ? <EyeOff |
| 311 | </InputGroupButton> |
| 312 | </InputGroupAddon> |
| 313 | </InputGroup> |
| 314 | |
| 315 | <field.AuthFormFieldError /> |
| 316 | </Field> |
| 317 | ); |
| 318 | }} |
| 319 | </form.AppField> |
| 320 | |
| 321 | {emailAndPassword.rememberMe && |
| 322 | <form.AppField name=" |
| 323 | {(field) => |
| 324 | <Field className=" |
| 325 | <div className="flex items-center gap-3 |
| 326 | <Checkbox |
| 327 | id="rememberMe" |
| 328 | name={field.name |
| 329 | checked={field.state |
| 330 | disabled={isPending} |
| 331 | onCheckedChange={(checked) |
| 332 | field.handleChange(checked |
| 333 | } |
| 334 | /> |
| 335 | |
| 336 | <FieldLabel |
| 337 | htmlFor="rememberMe" |
| 338 | className="cursor-pointer text-sm font-normal" |
| 339 | > |
| 340 | {localization.auth.rememberMe |
| 341 | </FieldLabel> |
| 342 | </div> |
| 343 | </Field> |
| 344 | )} |
| 345 | </form.AppField> |
| 346 | )} |
| 347 | |
| 348 | {Captcha && ( |
| 349 | <div className="flex justify-center |
| 350 | )} |
| 351 | |
| 352 | <div className="flex flex-col gap-3 |
| 353 | <form.AuthFormSubmitButton |
| 354 | className="relative overflow-visible" |
| 355 | disabled={isPending} |
| 356 | > |
| 357 | {isSignInPending && <Spinner |
| 358 | |
| 359 | {localization.auth.signIn |
| 360 | |
| 361 | <LastUsedBadge method={ |
| 362 | </form.AuthFormSubmitButton> |
| 363 | |
| 364 | {plugins.flatMap(( |
| 365 | (plugin.authButtons ?? |
| 366 | <AuthButton |
| 367 | key={`${plugin |
| 368 | view="signIn" |
| 369 | /> |
| 370 | )), |
| 371 | )} |
| 372 | </div> |
| 373 | </FieldGroup> |
| 374 | </form.AuthFormRoot> |
| 375 | </form.AppForm> |
| 376 | )} |
| 377 | |
| 378 | {socialPosition === "bottom |
| 379 | <> |
| 380 | {showSeparator && ( |
| 381 | <FieldSeparator className=" |
| 382 | {localization.auth.or |
| 383 | </FieldSeparator> |
| 384 | )} |
| 385 | |
| 386 | {socialProviders && socialProviders |
| 387 | <ProviderButtons socialLayout={ |
| 388 | )} |
| 389 | </> |
| 390 | )} |
| 391 | </div> |
| 392 | |
| 393 | <div className="flex flex-col gap-3 items-center w-full mt-4 |
| 394 | {emailAndPassword?.enabled && |
| 395 | <Link |
| 396 | href={`${basePaths |
| 397 | className="self-center text-sm underline-offset-4 hover:underline" |
| 398 | > |
| 399 | {localization.auth.forgotPasswordLink |
| 400 | </Link> |
| 401 | )} |
| 402 | |
| 403 | {emailAndPassword?.enabled && |
| 404 | <FieldDescription className=" |
| 405 | {localization.auth.needToCreateAnAccount |
| 406 | <Link |
| 407 | href={`${basePaths |
| 408 | className="underline underline-offset-4" |
| 409 | > |
| 410 | {localization.auth.signUp |
| 411 | </Link> |
| 412 | </FieldDescription> |
| 413 | )} |
| 414 | </div> |
| 415 | </CardContent> |
| 416 | </Card> |
| 417 | ); |
| 418 | } |