import { useEffect, useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import type { MessageDescriptor } from '@lingui/core'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import { AnimatePresence, motion } from 'framer-motion'; import { useForm } from 'react-hook-form'; import { FaIdCardClip } from 'react-icons/fa6'; import { FcGoogle } from 'react-icons/fc'; import { Link, useNavigate, useSearchParams } from 'react-router'; import { z } from 'zod'; import communityCardsImage from '@documenso/assets/images/community-cards.png'; import { authClient } from '@documenso/auth/client'; import { useAnalytics } from '@documenso/lib/client-only/hooks/use-analytics'; import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app'; import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error'; import { ZPasswordSchema } from '@documenso/trpc/server/auth-router/schema'; import { cn } from '@documenso/ui/lib/utils'; import { Button } from '@documenso/ui/primitives/button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@documenso/ui/primitives/form/form'; import { Input } from '@documenso/ui/primitives/input'; import { PasswordInput } from '@documenso/ui/primitives/password-input'; import { SignaturePad } from '@documenso/ui/primitives/signature-pad'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { UserProfileSkeleton } from '~/components/general/user-profile-skeleton'; import { UserProfileTimur } from '~/components/general/user-profile-timur'; type SignUpStep = 'BASIC_DETAILS' | 'CLAIM_USERNAME'; export const ZSignUpFormSchema = z .object({ name: z .string() .trim() .min(1, { message: msg`Please enter a valid name.`.id }), email: z.string().email().min(1), password: ZPasswordSchema, signature: z.string().min(1, { message: msg`We need your signature to sign documents`.id }), url: z .string() .trim() .toLowerCase() .min(1, { message: msg`We need a username to create your profile`.id }) .regex(/^[a-z0-9-]+$/, { message: msg`Username can only container alphanumeric characters and dashes.`.id, }), }) .refine( (data) => { const { name, email, password } = data; return !password.includes(name) && !password.includes(email.split('@')[0]); }, { message: msg`Password should not be common or based on personal information`.id, path: ['password'], }, ); export const signupErrorMessages: Record = { SIGNUP_DISABLED: msg`Signups are disabled.`, [AppErrorCode.ALREADY_EXISTS]: msg`User with this email already exists. Please use a different email address.`, [AppErrorCode.INVALID_REQUEST]: msg`We were unable to create your account. Please review the information you provided and try again.`, PROFILE_URL_TAKEN: msg`This username has already been taken`, PREMIUM_PROFILE_URL: msg`Only subscribers can have a username shorter than 6 characters`, }; export type TSignUpFormSchema = z.infer; export type SignUpFormProps = { className?: string; initialEmail?: string; isGoogleSSOEnabled?: boolean; isOIDCSSOEnabled?: boolean; }; export const SignUpForm = ({ className, initialEmail, isGoogleSSOEnabled, isOIDCSSOEnabled, }: SignUpFormProps) => { const { _ } = useLingui(); const { toast } = useToast(); const analytics = useAnalytics(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const [step, setStep] = useState('BASIC_DETAILS'); const utmSrc = searchParams.get('utm_source') ?? null; const baseUrl = new URL(NEXT_PUBLIC_WEBAPP_URL() ?? 'http://localhost:3000'); const form = useForm({ values: { name: '', email: initialEmail ?? '', password: '', signature: '', url: '', }, mode: 'onBlur', resolver: zodResolver(ZSignUpFormSchema), }); const isSubmitting = form.formState.isSubmitting; const name = form.watch('name'); const url = form.watch('url'); const onFormSubmit = async ({ name, email, password, signature, url }: TSignUpFormSchema) => { try { await authClient.emailPassword.signUp({ name, email, password, signature, url, }); await navigate(`/unverified-account`); toast({ title: _(msg`Registration Successful`), description: _( msg`You have successfully registered. Please verify your account by clicking on the link you received in the email.`, ), duration: 5000, }); analytics.capture('App: User Sign Up', { email, timestamp: new Date().toISOString(), custom_campaign_params: { src: utmSrc }, }); } catch (err) { const error = AppError.parseError(err); const errorMessage = signupErrorMessages[error.code] ?? signupErrorMessages.INVALID_REQUEST; if (error.code === 'PROFILE_URL_TAKEN' || error.code === 'PREMIUM_PROFILE_URL') { form.setError('url', { type: 'manual', message: _(errorMessage), }); } else { toast({ title: _(msg`An error occurred`), description: _(errorMessage), variant: 'destructive', }); } } }; const onNextClick = async () => { const valid = await form.trigger(['name', 'email', 'password', 'signature']); if (valid) { setStep('CLAIM_USERNAME'); } }; const onSignUpWithGoogleClick = async () => { try { await authClient.google.signIn(); } catch (err) { toast({ title: _(msg`An unknown error occurred`), description: _( msg`We encountered an unknown error while attempting to sign you Up. Please try again later.`, ), variant: 'destructive', }); } }; const onSignUpWithOIDCClick = async () => { try { await authClient.oidc.signIn(); } catch (err) { toast({ title: _(msg`An unknown error occurred`), description: _( msg`We encountered an unknown error while attempting to sign you Up. Please try again later.`, ), variant: 'destructive', }); } }; useEffect(() => { const hash = window.location.hash.slice(1); const params = new URLSearchParams(hash); const email = params.get('email'); if (email) { form.setValue('email', email); } }, [form]); return (
community-cards
User profiles are here!
{step === 'BASIC_DETAILS' ? ( ) : ( )}
{step === 'BASIC_DETAILS' && (

Create a new account

Create your account and start using state-of-the-art document signing. Open and beautiful signing is within your grasp.

)} {step === 'CLAIM_USERNAME' && (

Claim your username now

You will get notified & be able to set up your documenso public profile when we launch the feature.

)}
{step === 'BASIC_DETAILS' && (
( Full Name )} /> ( Email Address )} /> ( Password )} /> ( Sign Here onChange(v ?? '')} /> )} /> {(isGoogleSSOEnabled || isOIDCSSOEnabled) && ( <>
Or
)} {isGoogleSSOEnabled && ( <> )} {isOIDCSSOEnabled && ( <> )}

Already have an account?{' '} Sign in instead

)} {step === 'CLAIM_USERNAME' && (
( Public profile username
{baseUrl.host}/u/{field.value || ''}
)} />
)}
{step === 'BASIC_DETAILS' && (

Basic details {' '} 1/2

)} {step === 'CLAIM_USERNAME' && (

Claim username {' '} 2/2

)}
{/* Go back button, disabled if step is basic details */} {/* Continue button */} {step === 'BASIC_DETAILS' && ( )} {/* Sign up button */} {step === 'CLAIM_USERNAME' && ( )}

By proceeding, you agree to our{' '} Terms of Service {' '} and{' '} Privacy Policy .

); };