'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 (
Your prayer companion
Enter your email and we'll send you a link to reset your password.
{forgotMessage && (