import { useState, useEffect } from "react" import { Link, useNavigate } from "react-router" import { FormInput } from "@/components/shared/form-input" import { FormButton } from "@/components/shared/form-button" import { useAuth } from "@/app/context/auth-context" import { register } from "@/lib/api" export default function RegisterPage() { const navigate = useNavigate() const { user, loading: authLoading } = useAuth() useEffect(() => { if (!authLoading && user) { navigate("/forms") } }, [user, authLoading, navigate]) const [form, setForm] = useState({ firstName: "", lastName: "", email: "", password: "", confirmPassword: "", }) const [errors, setErrors] = useState>({}) function handleChange(e: React.ChangeEvent) { setForm((prev) => ({ ...prev, [e.target.name]: e.target.value })) setErrors((prev) => ({ ...prev, [e.target.name]: "" })) } function validate() { const newErrors: Partial = {} if (!form.firstName.trim()) newErrors.firstName = "First name is required" if (!form.lastName.trim()) newErrors.lastName = "Last name is required" if (!form.email.trim()) newErrors.email = "Email is required" if (form.password.length < 8) newErrors.password = "Must be at least 8 characters" if (form.confirmPassword !== form.password) newErrors.confirmPassword = "Passwords do not match" return newErrors } async function handleSubmit(e: React.FormEvent) { e.preventDefault() const newErrors = validate() if (Object.keys(newErrors).length > 0) { setErrors(newErrors) return } try { await register(form.email, form.password) navigate("/login") } catch (err) { setErrors({ email: err instanceof Error ? err.message : "Something went wrong. Please try again." }) } } return (

Create your account

Start building beautiful forms in minutes

Create account

Already have an account?{" "} Sign in

) }