import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants"; import { Button } from "@documenso/ui"; import { XCircleIcon } from "@heroicons/react/24/outline"; import { signIn } from "next-auth/react"; import Link from "next/link"; import { FormProvider, SubmitHandler, useForm } from "react-hook-form"; import { toast, Toast, Toaster } from "react-hot-toast"; import Logo from "./logo"; type FormValues = { email: string; password: string; apiError: string; }; export default function Signup(props: { source: string }) { const methods = useForm({}); const { register, trigger, formState: { errors, isSubmitting }, } = methods; const handleErrors = async (resp: Response) => { if (!resp.ok) { const err = await resp.json(); throw new Error(err.message); } }; const signUp: SubmitHandler = async (data) => { const res = await toast .promise( fetch("/api/auth/signup", { body: JSON.stringify({ source: props.source, ...data, }), headers: { "Content-Type": "application/json", }, method: "POST", }) .then(handleErrors) .then(async () => { await signIn<"credentials">("credentials", { ...data, callbackUrl: `${NEXT_PUBLIC_WEBAPP_URL}/dashboard`, }); }), { loading: "Creating your account...", success: "Done!", error: (err) => err.message, }, { style: { minWidth: "200px", }, } ) .catch((err) => { toast.dismiss(); methods.setError("apiError", { message: err.message }); }); }; function renderApiError() { if (!errors.apiError) return; return (

{errors.apiError &&
{errors.apiError?.message}
}

); } function renderFormValidation() { if (!errors.password && !errors.email) return; return (

{errors.password &&
{errors.password?.message}
}

{errors.email &&
{errors.email?.message}
}

); } return ( <>

Create a shiny, new

Documenso Account{" "}

Create your account and start using

state-of-the-art document signing for free.

{renderApiError()} {renderFormValidation()}
{ methods.clearErrors(); trigger(); }} className="mt-8 space-y-6" >

Already have an account?{" "} Sign In

); }