'use client'; import { useState, useEffect } from 'react'; import { signIn } from 'next-auth/react'; import Link from 'next/link'; import { authService, AuthService, RegisterRequest, agentsService, AgentType } from '@/services'; import { resetLogoutState } from '@/services/api'; type UserType = 'USER' | 'AGENT'; interface ValidationError { field: string; errors: string[]; } export default function SignUpPage() { const [userType, setUserType] = useState('USER'); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [error, setError] = useState(''); const [fieldErrors, setFieldErrors] = useState([]); const [loading, setLoading] = useState(false); const [registrationSuccess, setRegistrationSuccess] = useState(false); const [agentTypes, setAgentTypes] = useState([]); const [selectedAgentTypeId, setSelectedAgentTypeId] = useState(''); const [loadingAgentTypes, setLoadingAgentTypes] = useState(false); // Clear logout flags and fetch agent types on mount useEffect(() => { localStorage.removeItem('isLoggingOut'); resetLogoutState(); const fetchAgentTypes = async () => { setLoadingAgentTypes(true); try { const types = await agentsService.getAgentTypes(); setAgentTypes(types); } catch (err) { console.error('Failed to fetch agent types:', err); } finally { setLoadingAgentTypes(false); } }; fetchAgentTypes(); }, []); const getFieldError = (fieldName: string): string | null => { const fieldError = fieldErrors.find((e) => e.field === fieldName); return fieldError ? fieldError.errors[0] : null; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); setFieldErrors([]); // Validate agent type if registering as agent if (userType === 'AGENT' && !selectedAgentTypeId) { setError('Please select an agent type'); setLoading(false); return; } try { const registerData: RegisterRequest = { firstName, lastName, email, password, role: userType, ...(userType === 'AGENT' && selectedAgentTypeId && { agentTypeId: selectedAgentTypeId }), }; await authService.register(registerData); // Show success message - user must verify email before logging in setRegistrationSuccess(true); } catch (err) { // Get field errors const errors = AuthService.getFieldErrors(err); if (errors.length > 0) { setFieldErrors(errors); setError('Please fix the errors below'); } else { setError(AuthService.getErrorMessage(err)); } } finally { setLoading(false); } }; const handleSocialLogin = (provider: 'google' | 'facebook' | 'twitter') => { signIn(provider, { callbackUrl: '/' }); }; // Registration Success Message if (registrationSuccess) { return (

Check Your Email

We've sent a verification link to {email}. Please check your inbox and click the link to verify your account before logging in.

Go to Login

Didn't receive the email? Check your spam folder or contact support.

); } return ( <> {/* Sign Up Heading */}

Sign Up

Sign in or create an account to showcase your skills & expertise to millions of people starting their real estate journey today"

{/* User Type Toggle */}
{/* Sign Up Form */}
{error && (
{error}
)} {/* First Name & Last Name */}
setFirstName(e.target.value)} required className={`w-full px-6 py-6 bg-[#f0f5fc] rounded-[7px] text-[#00293d] text-[14px] font-light font-serif placeholder:text-[#00293d] focus:outline-none focus:ring-2 focus:ring-[#00293d]/20 transition-all ${ getFieldError('firstName') ? 'ring-2 ring-red-400' : '' }`} /> {getFieldError('firstName') && (

{getFieldError('firstName')}

)}
setLastName(e.target.value)} className={`w-full px-6 py-6 bg-[#f0f5fc] rounded-[7px] text-[#00293d] text-[14px] font-light font-serif placeholder:text-[#00293d] focus:outline-none focus:ring-2 focus:ring-[#00293d]/20 transition-all ${ getFieldError('lastName') ? 'ring-2 ring-red-400' : '' }`} /> {getFieldError('lastName') && (

{getFieldError('lastName')}

)}
{/* Email Input */}
setEmail(e.target.value)} required className={`w-full px-6 py-6 bg-[#f0f5fc] rounded-[7px] text-[#00293d] text-[14px] font-light font-serif placeholder:text-[#00293d] focus:outline-none focus:ring-2 focus:ring-[#00293d]/20 transition-all ${ getFieldError('email') ? 'ring-2 ring-red-400' : '' }`} /> {getFieldError('email') && (

{getFieldError('email')}

)}
{/* Password Input */}
setPassword(e.target.value)} required className={`w-full px-6 py-6 bg-[#f0f5fc] rounded-[7px] text-[#00293d] text-[14px] font-light font-serif placeholder:text-[#00293d] focus:outline-none focus:ring-2 focus:ring-[#00293d]/20 transition-all pr-14 ${ getFieldError('password') ? 'ring-2 ring-red-400' : '' }`} />
{getFieldError('password') && (

{getFieldError('password')}

)}

Min 8 chars, 1 uppercase, 1 lowercase, 1 number, 1 special character

{/* Agent Type Selection - Only for Agents */} {userType === 'AGENT' && (
{getFieldError('agentTypeId') && (

{getFieldError('agentTypeId')}

)}
)} {/* Sign Up Button */}
{/* Already Have Account */}
Already Have An Account ? Login
{/* Divider */}
Or
{/* Social Login Icons */}
{/* Apple */} {/* Google */} {/* X (Twitter) */} {/* Facebook */}
); }