import { zodResolver } from "@hookform/resolvers/zod" import { FC } from "react" import { SubmitHandler, useForm } from "react-hook-form" import { z } from "zod" import FormField from "../common/form/FormField" import InlineLink from "../common/InlineLink" import Input from "../common/Input" import SubmitButton from "../common/form/SubmitButton" import { PASSWORD_REGEX } from "../../config" import useSignUpFlow from "../../hooks/useSignUpFlow" const SignupFormSchema = z .object({ csrfToken: z.string(), username: z .string() .trim() .min(1, { message: "Username must be at least 1 character long." }) .max(16, { message: "Username can't be longer than 16 characters.." }), password: z .string() .trim() .regex( PASSWORD_REGEX, "Password must be 8-64 long and must contain a number, uppercase, lowercase and special character.", ), passwordRepeat: z.string().trim(), email: z.string().email({ message: "Not a valid email address" }).trim(), }) .refine((data) => data.password === data.passwordRepeat, { message: "Passwords do not match.", path: ["passwordRepeat"], }) type SignupFormValues = z.infer const formFields = [ { id: "username", label: "Username", type: "text" }, { id: "password", label: "Password", type: "password" }, { id: "passwordRepeat", label: "Confirm Password", type: "password" }, { id: "email", label: "Email", type: "email" }, ] const SignupForm: FC = () => { const signUpFlow = useSignUpFlow() const { register, handleSubmit, formState } = useForm({ resolver: zodResolver(SignupFormSchema), }) const onSubmit: SubmitHandler = async (data) => { await signUpFlow.submitData({ csrf_token: data.csrfToken, method: "password", password: data.password, traits: { email: data.email, }, }) } return (

Creating an account allows you to participate in chat, follow your favorite channels, and broadcast from your own channel.

{signUpFlow.flow && ( )} {formFields.map((field) => ( {formState.errors[(field.id as any) || ""]?.message}

} /> ))}

By clicking Sign Up, you are agreeing to twitch-clone's{" "} Terms of Service .

) } export default SignupForm