'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { signIn } from 'next-auth/react'; import Link from 'next/link'; import { resetLogoutState } from '@/services/api'; export default function LoginPage() { const router = useRouter(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); // Safety net: clear logout flags when login page loads. // This resets both the localStorage flag and the module-level flag in api.ts // so the API interceptor stops blocking requests. if (typeof window !== 'undefined') { localStorage.removeItem('isLoggingOut'); resetLogoutState(); } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); try { // First, validate credentials with backend to get proper error messages const validateRes = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); const validateData = await validateRes.json(); if (!validateRes.ok) { // Show the actual error message from backend setError(validateData.message || 'Invalid email or password'); setLoading(false); return; } // Check if 2FA is required (response is wrapped in { success, data }) if (validateData.data?.requiresTwoFactor) { // Redirect to 2FA verification page with tempToken const params = new URLSearchParams({ token: validateData.data.tempToken, email: email, }); router.push(`/login/verify-2fa?${params.toString()}`); return; } // If validation passed, use NextAuth to create session const result = await signIn('credentials', { email, password, redirect: false, }); setLoading(false); if (result?.error) { setError('Login failed. Please try again.'); } else { router.push('/'); router.refresh(); } } catch (err) { setLoading(false); setError('An error occurred. Please try again.'); } }; const handleSocialLogin = (provider: 'google' | 'facebook' | 'twitter') => { signIn(provider, { callbackUrl: '/' }); }; return ( <> {/* Login Heading */}

Login

Login or create an account to enjoy FREE consultation on all property inquiries.

{/* User Type Toggle */}
{/* Login Form */}
{error && (
{error}
)} {/* 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" />
{/* 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" />
{/* Forgot Password Link */}
Forget Your Password ?
{/* Login Button */}
{/* Divider */}
Or
{/* Social Login Icons */}
{/* Apple */} {/* Google */} {/* X (Twitter) */} {/* Facebook */}
); }