'use client' import { useState } from 'react' import { useRouter, useSearchParams } from 'next/navigation' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Separator } from '@/components/ui/separator' import { Alert, AlertDescription } from '@/components/ui/alert' import { Checkbox } from '@/components/ui/checkbox' import { signUpSchema, type SignUpValues } from '@/lib/validations/auth' import { Eye, EyeOff, Chrome } from 'lucide-react' import Link from 'next/link' import { signIn } from 'next-auth/react' export default function SignUpPage() { const [showPassword, setShowPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState('') const [isMember, setIsMember] = useState(false) const router = useRouter() const searchParams = useSearchParams() const referralCode = searchParams.get('ref') const { register, handleSubmit, formState: { errors }, setValue, } = useForm({ resolver: zodResolver(signUpSchema), defaultValues: { referralCode: referralCode || '', }, }) const onSubmit = async (data: SignUpValues) => { setIsLoading(true) setError('') try { const response = await fetch('/api/auth/signup', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ ...data, role: isMember ? 'MEMBER' : 'CUSTOMER', }), }) if (!response.ok) { const errorData = await response.json() throw new Error(errorData.error || 'Failed to create account') } // Sign in the user after successful registration const result = await signIn('credentials', { email: data.email, password: data.password, redirect: false, }) if (result?.error) { setError('Account created but failed to sign in. Please try signing in manually.') } else { router.push('/dashboard') } } catch (error) { setError(error instanceof Error ? error.message : 'Something went wrong. Please try again.') } finally { setIsLoading(false) } } const handleGoogleSignIn = async () => { setIsLoading(true) await signIn('google', { callbackUrl: '/dashboard' }) } return (
Create Account Join thousands of successful entrepreneurs {error && ( {error} )}
Or continue with email
{errors.name && (

{errors.name.message}

)}
{errors.email && (

{errors.email.message}

)}
{errors.phone && (

{errors.phone.message}

)}
{errors.password && (

{errors.password.message}

)}
{errors.confirmPassword && (

{errors.confirmPassword.message}

)}
{errors.referralCode && (

{errors.referralCode.message}

)}
setIsMember(checked === true)} disabled={isLoading} />

Already have an account?{' '} Sign in

) }