|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Container, |
| 4 | + Flex, |
| 5 | + FormControl, |
| 6 | + FormErrorMessage, |
| 7 | + FormLabel, |
| 8 | + Image, |
| 9 | + Input, |
| 10 | + Link, |
| 11 | + Text, |
| 12 | +} from "@chakra-ui/react" |
| 13 | +import { |
| 14 | + Link as RouterLink, |
| 15 | + createFileRoute, |
| 16 | + redirect, |
| 17 | +} from "@tanstack/react-router" |
| 18 | +import { type SubmitHandler, useForm } from "react-hook-form" |
| 19 | + |
| 20 | +import Logo from "/assets/images/fastapi-logo.svg" |
| 21 | +import type { UserRegister } from "../client" |
| 22 | +import useAuth, { isLoggedIn } from "../hooks/useAuth" |
| 23 | +import { confirmPasswordRules, emailPattern, passwordRules } from "../utils" |
| 24 | + |
| 25 | +export const Route = createFileRoute("/signup")({ |
| 26 | + component: SignUp, |
| 27 | + beforeLoad: async () => { |
| 28 | + if (isLoggedIn()) { |
| 29 | + throw redirect({ |
| 30 | + to: "/", |
| 31 | + }) |
| 32 | + } |
| 33 | + }, |
| 34 | +}) |
| 35 | + |
| 36 | +interface UserRegisterForm extends UserRegister { |
| 37 | + confirm_password: string |
| 38 | +} |
| 39 | + |
| 40 | +function SignUp() { |
| 41 | + const { signUpMutation } = useAuth() |
| 42 | + const { |
| 43 | + register, |
| 44 | + handleSubmit, |
| 45 | + getValues, |
| 46 | + formState: { errors, isSubmitting }, |
| 47 | + } = useForm<UserRegisterForm>({ |
| 48 | + mode: "onBlur", |
| 49 | + criteriaMode: "all", |
| 50 | + defaultValues: { |
| 51 | + email: "", |
| 52 | + full_name: "", |
| 53 | + password: "", |
| 54 | + confirm_password: "", |
| 55 | + }, |
| 56 | + }) |
| 57 | + |
| 58 | + const onSubmit: SubmitHandler<UserRegisterForm> = (data) => { |
| 59 | + signUpMutation.mutate(data) |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <Flex flexDir={{ base: "column", md: "row" }} justify="center" h="100vh"> |
| 65 | + <Container |
| 66 | + as="form" |
| 67 | + onSubmit={handleSubmit(onSubmit)} |
| 68 | + h="100vh" |
| 69 | + maxW="sm" |
| 70 | + alignItems="stretch" |
| 71 | + justifyContent="center" |
| 72 | + gap={4} |
| 73 | + centerContent |
| 74 | + > |
| 75 | + <Image |
| 76 | + src={Logo} |
| 77 | + alt="FastAPI logo" |
| 78 | + height="auto" |
| 79 | + maxW="2xs" |
| 80 | + alignSelf="center" |
| 81 | + mb={4} |
| 82 | + /> |
| 83 | + <FormControl id="full_name" isInvalid={!!errors.full_name}> |
| 84 | + <FormLabel htmlFor="full_name" srOnly> |
| 85 | + Full Name |
| 86 | + </FormLabel> |
| 87 | + <Input |
| 88 | + id="full_name" |
| 89 | + minLength={3} |
| 90 | + {...register("full_name")} |
| 91 | + placeholder="Full Name" |
| 92 | + type="text" |
| 93 | + /> |
| 94 | + {errors.full_name && ( |
| 95 | + <FormErrorMessage>{errors.full_name.message}</FormErrorMessage> |
| 96 | + )} |
| 97 | + </FormControl> |
| 98 | + <FormControl id="email" isInvalid={!!errors.email}> |
| 99 | + <FormLabel htmlFor="username" srOnly> |
| 100 | + Email |
| 101 | + </FormLabel> |
| 102 | + <Input |
| 103 | + id="email" |
| 104 | + {...register("email", { |
| 105 | + pattern: emailPattern, |
| 106 | + })} |
| 107 | + placeholder="Email" |
| 108 | + type="email" |
| 109 | + /> |
| 110 | + {errors.email && ( |
| 111 | + <FormErrorMessage>{errors.email.message}</FormErrorMessage> |
| 112 | + )} |
| 113 | + </FormControl> |
| 114 | + <FormControl id="password" isInvalid={!!errors.password}> |
| 115 | + <FormLabel htmlFor="password" srOnly> |
| 116 | + Password |
| 117 | + </FormLabel> |
| 118 | + <Input |
| 119 | + id="password" |
| 120 | + {...register("password", passwordRules())} |
| 121 | + placeholder="Password" |
| 122 | + type="password" |
| 123 | + /> |
| 124 | + {errors.password && ( |
| 125 | + <FormErrorMessage>{errors.password.message}</FormErrorMessage> |
| 126 | + )} |
| 127 | + </FormControl> |
| 128 | + <FormControl |
| 129 | + id="confirm_password" |
| 130 | + isInvalid={!!errors.confirm_password} |
| 131 | + > |
| 132 | + <FormLabel htmlFor="confirm_password" srOnly> |
| 133 | + Confirm Password |
| 134 | + </FormLabel> |
| 135 | + |
| 136 | + <Input |
| 137 | + id="confirm_password" |
| 138 | + {...register("confirm_password", confirmPasswordRules(getValues))} |
| 139 | + placeholder="Repeat Password" |
| 140 | + type="password" |
| 141 | + /> |
| 142 | + {errors.confirm_password && ( |
| 143 | + <FormErrorMessage> |
| 144 | + {errors.confirm_password.message} |
| 145 | + </FormErrorMessage> |
| 146 | + )} |
| 147 | + </FormControl> |
| 148 | + <Button variant="primary" type="submit" isLoading={isSubmitting}> |
| 149 | + Sign Up |
| 150 | + </Button> |
| 151 | + <Text> |
| 152 | + Already have an account?{" "} |
| 153 | + <Link as={RouterLink} to="/login" color="blue.500"> |
| 154 | + Log In |
| 155 | + </Link> |
| 156 | + </Text> |
| 157 | + </Container> |
| 158 | + </Flex> |
| 159 | + </> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +export default SignUp |
0 commit comments