2025-02-17 19:33:00 +00:00
|
|
|
import { Container, Flex, Image, Input, Text } from "@chakra-ui/react"
|
2024-07-22 19:40:56 -05:00
|
|
|
import {
|
|
|
|
Link as RouterLink,
|
|
|
|
createFileRoute,
|
|
|
|
redirect,
|
|
|
|
} from "@tanstack/react-router"
|
|
|
|
import { type SubmitHandler, useForm } from "react-hook-form"
|
2025-02-17 19:33:00 +00:00
|
|
|
import { FiLock, FiUser } from "react-icons/fi"
|
2025-02-17 19:55:20 +00:00
|
|
|
|
|
|
|
import type { UserRegister } from "@/client"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
import { Field } from "@/components/ui/field"
|
|
|
|
import { InputGroup } from "@/components/ui/input-group"
|
|
|
|
import { PasswordInput } from "@/components/ui/password-input"
|
|
|
|
import useAuth, { isLoggedIn } from "@/hooks/useAuth"
|
|
|
|
import { confirmPasswordRules, emailPattern, passwordRules } from "@/utils"
|
2024-07-22 19:40:56 -05:00
|
|
|
import Logo from "/assets/images/fastapi-logo.svg"
|
|
|
|
|
|
|
|
export const Route = createFileRoute("/signup")({
|
|
|
|
component: SignUp,
|
|
|
|
beforeLoad: async () => {
|
|
|
|
if (isLoggedIn()) {
|
|
|
|
throw redirect({
|
|
|
|
to: "/",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
interface UserRegisterForm extends UserRegister {
|
|
|
|
confirm_password: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function SignUp() {
|
|
|
|
const { signUpMutation } = useAuth()
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
getValues,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
} = useForm<UserRegisterForm>({
|
|
|
|
mode: "onBlur",
|
|
|
|
criteriaMode: "all",
|
|
|
|
defaultValues: {
|
|
|
|
email: "",
|
|
|
|
full_name: "",
|
|
|
|
password: "",
|
|
|
|
confirm_password: "",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const onSubmit: SubmitHandler<UserRegisterForm> = (data) => {
|
|
|
|
signUpMutation.mutate(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Flex flexDir={{ base: "column", md: "row" }} justify="center" h="100vh">
|
|
|
|
<Container
|
|
|
|
as="form"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
h="100vh"
|
|
|
|
maxW="sm"
|
|
|
|
alignItems="stretch"
|
|
|
|
justifyContent="center"
|
|
|
|
gap={4}
|
|
|
|
centerContent
|
|
|
|
>
|
|
|
|
<Image
|
|
|
|
src={Logo}
|
|
|
|
alt="FastAPI logo"
|
|
|
|
height="auto"
|
|
|
|
maxW="2xs"
|
|
|
|
alignSelf="center"
|
|
|
|
mb={4}
|
|
|
|
/>
|
2025-02-17 19:33:00 +00:00
|
|
|
<Field
|
|
|
|
invalid={!!errors.full_name}
|
|
|
|
errorText={errors.full_name?.message}
|
2024-07-22 19:40:56 -05:00
|
|
|
>
|
2025-02-17 19:33:00 +00:00
|
|
|
<InputGroup w="100%" startElement={<FiUser />}>
|
|
|
|
<Input
|
|
|
|
id="full_name"
|
|
|
|
minLength={3}
|
|
|
|
{...register("full_name", {
|
|
|
|
required: "Full Name is required",
|
|
|
|
})}
|
|
|
|
placeholder="Full Name"
|
|
|
|
type="text"
|
|
|
|
/>
|
|
|
|
</InputGroup>
|
|
|
|
</Field>
|
2024-07-22 19:40:56 -05:00
|
|
|
|
2025-02-17 19:33:00 +00:00
|
|
|
<Field invalid={!!errors.email} errorText={errors.email?.message}>
|
|
|
|
<InputGroup w="100%" startElement={<FiUser />}>
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
{...register("email", {
|
|
|
|
required: "Email is required",
|
|
|
|
pattern: emailPattern,
|
|
|
|
})}
|
|
|
|
placeholder="Email"
|
|
|
|
type="email"
|
|
|
|
/>
|
|
|
|
</InputGroup>
|
|
|
|
</Field>
|
|
|
|
<PasswordInput
|
|
|
|
type="password"
|
|
|
|
startElement={<FiLock />}
|
|
|
|
{...register("password", passwordRules())}
|
|
|
|
placeholder="Password"
|
|
|
|
errors={errors}
|
|
|
|
/>
|
|
|
|
<PasswordInput
|
|
|
|
type="confirm_password"
|
|
|
|
startElement={<FiLock />}
|
|
|
|
{...register("confirm_password", confirmPasswordRules(getValues))}
|
|
|
|
placeholder="Confirm Password"
|
|
|
|
errors={errors}
|
|
|
|
/>
|
|
|
|
<Button variant="solid" type="submit" loading={isSubmitting}>
|
2024-07-22 19:40:56 -05:00
|
|
|
Sign Up
|
|
|
|
</Button>
|
|
|
|
<Text>
|
|
|
|
Already have an account?{" "}
|
2025-02-17 19:33:00 +00:00
|
|
|
<RouterLink to="/login" className="main-link">
|
2024-07-22 19:40:56 -05:00
|
|
|
Log In
|
2025-02-17 19:33:00 +00:00
|
|
|
</RouterLink>
|
2024-07-22 19:40:56 -05:00
|
|
|
</Text>
|
|
|
|
</Container>
|
|
|
|
</Flex>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SignUp
|