import React, { useState } from 'react' import { Eye, EyeOff, X } from 'lucide-react' import { useIsLoginModalOpen, useSetIsLoginModalOpen } from '../../contexts/UIContext' import { useAuth } from '../../contexts/AuthContext' import useRenderTracker from '../../hooks/useRenderTracker' interface LoginModalProps { openSignup: boolean | undefined } export default function LoginModal({ openSignup, }: LoginModalProps) { const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [isSignup, setIsSignup] = useState(openSignup) const [showPassword, setShowPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [name, setName] = useState('') const [verificationCode, setVerificationCode] = useState(''); const [isVerifying, setIsVerifying] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const [emailSent, setEmailSent] = useState(false); const [isPasswordReset, setIsPasswordReset] = useState(false); const [resetVerificationCode, setResetVerificationCode] = useState(''); const [newPassword, setNewPassword] = useState(''); const [showNewPassword, setShowNewPassword] = useState(false); const [resetSuccess, setResetSuccess] = useState(false); const isLoginModalOpen = useIsLoginModalOpen(); const setIsLoginModalOpen = useSetIsLoginModalOpen(); const { login, signup, requestVerification, authError, setAuthError, resetPassword } = useAuth() const handleRequestVerification = async () => { setIsSubmitting(true); try { await requestVerification(email, 'registration'); setIsVerifying(true); setEmailSent(true); } catch (error) { setAuthError(error instanceof Error ? error.message : 'An error occurred'); } finally { setIsSubmitting(false); } }; const handlePasswordResetRequest = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); try { await requestVerification(email, 'password_reset'); setIsVerifying(true); setEmailSent(true); } catch (error) { setAuthError(error instanceof Error ? error.message : 'An error occurred'); } finally { setIsSubmitting(false); } }; const handlePasswordReset = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); try { await resetPassword(email, resetVerificationCode, newPassword); setAuthError(null); setResetSuccess(true); } catch (error) { setAuthError(error instanceof Error ? error.message : 'Password reset failed'); } finally { setIsSubmitting(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); setAuthError(null); try { if (isSignup) { // Password match verification if (password !== confirmPassword) { setAuthError("Passwords do not match"); setIsSubmitting(false); return; } // Password strength validation if (password.length < 8) { setAuthError("Password must be at least 8 characters long"); setIsSubmitting(false); return; } if (!isVerifying) { await handleRequestVerification(); } else { await signup(name, email, password, verificationCode); } } else { await login(email, password); } } catch (error) { console.error('Error:', error); } finally { setIsSubmitting(false); } }; const resetState = () => { setEmail('') setPassword('') setConfirmPassword('') setIsSignup(openSignup) setShowPassword(false) setShowConfirmPassword(false) setName('') setAuthError(null) setIsPasswordReset(false) setResetVerificationCode('') setNewPassword('') setShowNewPassword(false) setIsVerifying(false) setEmailSent(false) } const handleClose = () => { resetState() setIsLoginModalOpen(false) } useRenderTracker('LoginModal', { isLoginModalOpen, authError, isSignup, email, name }); if (!isLoginModalOpen) return null if (isPasswordReset) { if (resetSuccess) { return (
Your password has been successfully reset!
You can now log in with your new password.
Already have an account?{' '}
) : (Don't have an account?{' '}
)}