'use client'; import { useState, useEffect } from 'react'; import { useAuth } from '@/context/AuthContext'; export default function Login() { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [firstName, setFirstName] = useState(''); const [isSignup, setIsSignup] = useState(false); const [rememberMe, setRememberMe] = useState(false); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const [showForgotPassword, setShowForgotPassword] = useState(false); const [forgotEmail, setForgotEmail] = useState(''); const [forgotLoading, setForgotLoading] = useState(false); const [forgotMessage, setForgotMessage] = useState(''); const [showVerificationBanner, setShowVerificationBanner] = useState(false); const [verificationSent, setVerificationSent] = useState(false); const [needsVerification, setNeedsVerification] = useState(false); const [resendEmail, setResendEmail] = useState(''); const [resendPassword, setResendPassword] = useState(''); const [showPasswordPrompt, setShowPasswordPrompt] = useState(false); const { login, signup, sendPasswordReset, sendVerificationEmailToUser } = useAuth(); // Persist verification state across re-renders useEffect(() => { const savedEmail = sessionStorage.getItem('needsVerification'); if (savedEmail) { setNeedsVerification(true); setResendEmail(savedEmail); setError('Please verify your email address before logging in. Check your inbox for the verification link.'); } }, []); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setLoading(true); if (isSignup) { if (!firstName.trim()) { setError('Please enter your first name'); setLoading(false); return; } if (password !== confirmPassword) { setError('Passwords do not match'); setLoading(false); return; } try { const newUser = await signup(email, password, firstName); // Send verification email to the newly created user if (newUser) { try { await sendVerificationEmailToUser(newUser); // Sign them out immediately - they must verify before login const { signOut } = await import('firebase/auth'); const { auth } = await import('@/lib/firebase'); await signOut(auth); setVerificationSent(true); setShowVerificationBanner(true); } catch (err) { console.error('Error sending verification email:', err); } } } catch (err: unknown) { setError(err instanceof Error ? err.message : 'An error occurred'); } } else { try { await login(email, password); // Store remember me timestamp if checked if (rememberMe) { localStorage.setItem('rememberMeTimestamp', Date.now().toString()); } else { localStorage.removeItem('rememberMeTimestamp'); } } catch (err: unknown) { const errorMessage = err instanceof Error ? err.message : 'An error occurred'; const errorCode = err instanceof Error && 'code' in err ? (err as any).code : ''; console.log('Login error:', errorMessage, 'Code:', errorCode); // Debug log setError(errorMessage); } } setLoading(false); }; const handleResendVerification = async () => { if (!resendPassword) { setShowPasswordPrompt(true); return; } setLoading(true); setError(''); try { // Try to login temporarily to send verification const { signInWithEmailAndPassword } = await import('firebase/auth'); const { auth } = await import('@/lib/firebase'); const userCred = await signInWithEmailAndPassword(auth, resendEmail, resendPassword); await sendVerificationEmailToUser(userCred.user); // Sign them back out const { signOut } = await import('firebase/auth'); await signOut(auth); setError(''); setNeedsVerification(false); sessionStorage.removeItem('needsVerification'); setShowVerificationBanner(true); setVerificationSent(true); setResendPassword(''); setShowPasswordPrompt(false); } catch (err: unknown) { setError(err instanceof Error ? err.message : 'Failed to resend verification email. Check your password.'); setResendPassword(''); } setLoading(false); }; const handleForgotPassword = async () => { if (!forgotEmail) { setForgotMessage('Please enter your email address'); return; } setForgotLoading(true); setForgotMessage(''); try { await sendPasswordReset(forgotEmail); setForgotMessage('Password reset email sent! Check your inbox.'); } catch (err: unknown) { setForgotMessage(err instanceof Error ? err.message : 'Failed to send reset email'); } setForgotLoading(false); }; return (

Haver

Your prayer companion

{isSignup ? 'Create Account' : 'Welcome Back'}

{error && (
{error}
)} {needsVerification && (

⚠️ Email Not Verified

Your account exists, but you haven't verified your email yet.

Check your inbox for {resendEmail} and click the verification link. Don't forget to check your spam folder!

📧 Can't find the email?

Click the button below to send a new verification email.

{showPasswordPrompt && (
setResendPassword(e.target.value)} placeholder="Your password" className="w-full p-2 bg-yellow-950 border border-yellow-700 rounded-lg text-white placeholder-yellow-600 focus:ring-2 focus:ring-yellow-500 focus:border-transparent" onKeyDown={(e) => { if (e.key === 'Enter') { handleResendVerification(); } }} />
)}
)} {isSignup && (
setFirstName(e.target.value)} className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="Enter your first name" required={isSignup} />
)}
setEmail(e.target.value)} autoFocus className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="Enter your email" required />
setPassword(e.target.value)} className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="Enter your password" required />
{isSignup && (
setConfirmPassword(e.target.value)} className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors" placeholder="Confirm your password" required />
)} {!isSignup && (
setRememberMe(e.target.checked)} className="w-4 h-4 rounded border-gray-600 bg-gray-700 text-blue-600 focus:ring-2 focus:ring-blue-500 cursor-pointer" />
)} {showVerificationBanner && (

⚠️ Email Verification Required

Your account has been created, but you must verify your email before you can sign in.

A verification email has been sent to {email || resendEmail}. Please check your inbox (and spam folder) and click the verification link.

📧 Didn't receive the email?

It may take a few minutes to arrive. Check your spam/junk folder.

)}
{/* Forgot Password Modal */} {showForgotPassword && (

Reset Password

Enter your email and we'll send you a link to reset your password.

{forgotMessage && (
{forgotMessage}
)} setForgotEmail(e.target.value)} placeholder="Enter your email" className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-colors mb-4" />
)}
); }