import { zodResolver } from "@hookform/resolvers/zod"; import { InfoIcon } from "@phosphor-icons/react"; import axios from "axios"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { useNavigate } from "react-router-dom"; import { z } from "zod"; import { publicApi } from "../api/apiClient"; import Logo from "../components/Logo"; import FormField from "../components/ui/FormField"; import Saajan from "../components/ui/Saajan"; import { endpoints } from "../config/endpoints"; import { ROUTES } from "../config/routes"; import { CryptoUtils } from "../utils/crypto"; const registerSchema = z .object({ full_name: z.string().min(2, "Name must be at least 2 characters"), email: z.email("Please enter a valid email"), password: z.string().min(8, "Password must be at least 8 characters"), confirm_password: z.string(), }) .refine((data) => data.password === data.confirm_password, { message: "Passwords don't match", path: ["confirm_password"], }); type RegisterInputs = z.infer; export default function Register() { const navigate = useNavigate(); const [isLoading, setIsLoading] = useState(false); const [apiError, setApiError] = useState(null); const [saajanMessage, setSaajanMessage] = useState( "I didn't think I'd be here either.\nAnd yet, here we are.", ); const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(registerSchema), }); const onSubmit = async (data: RegisterInputs) => { setSaajanMessage("Good. I'll remember that."); setIsLoading(true); setApiError(null); try { // we generate the key bundle here to get the authHash (password) to be haSHed and stored in the db. const { authHash } = await CryptoUtils.deriveKeyBundle( data.password, data.email, ); await publicApi.post(endpoints.REGISTER, { full_name: data.full_name, email: data.email, password: authHash, }); navigate(ROUTES.VERIFY_EMAIL, { replace: true }); } catch (err) { let message = "Registration failed. Please try again."; if (axios.isAxiosError(err)) { message = err.response?.data?.message || message; } setApiError(message); } finally { setIsLoading(false); } }; return (
Create a Account
{apiError && (
{apiError}
)} setSaajanMessage("Hello friend. What should I call you?") } /> setSaajanMessage( "Where should I send your letters?\nNo empty lunchboxes, please.", ) } /> setSaajanMessage( "Something only you know.\nI have one of those too.", ) } /> setSaajanMessage( "Just once? Trust me, \nsome things are worth repeating twice.", ) } />

Choose a password you won't forget.
Just like life,{" "} there is no reset{" "} here. If you lose it, your letters cannot be recovered.

); }