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 (

Password Reset Successful

Your password has been successfully reset!

You can now log in with your new password.

); } return (

{isVerifying ? 'Reset Password' : 'Forgot Password'}

{!isVerifying && (
setEmail(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required />
)} {isVerifying && ( <> {emailSent && (

We've sent a verification code to {email}

Please check your inbox (and spam folder) for the code.

)}
setResetVerificationCode(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required autoComplete="off" />
setNewPassword(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required />
)} {authError && (
{typeof authError === 'string' ? authError : 'An error occurred'}
)}
); } return (

{isSignup ? (isVerifying ? 'Verify Email' : 'Sign Up') : 'Login'}

{isSignup && !isVerifying && (
setName(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required />
)} {(!isVerifying) && (
setEmail(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required />
)} {(!isSignup || !isVerifying) && (
setPassword(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required />
{!isSignup && (
)}
)} {isSignup && !isVerifying && (
setConfirmPassword(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required />
)} {isSignup && isVerifying && ( <> {emailSent && (

We've sent a verification code to {email}

Please check your inbox (and spam folder) for the code.

)}
setVerificationCode(e.target.value)} className="w-full px-4 py-3 rounded-2xl border border-gray-300 dark:border-gray-600 focus:outline-none focus:ring-1 focus:ring-primary focus:border-transparent bg-white dark:bg-gray-500/15 text-gray-900 dark:text-gray-100" required autoComplete="off" />
)} {authError && (
{typeof authError === 'string' ? authError : 'An error occurred'}
)}
{isSignup ? (

Already have an account?{' '}

) : (

Don't have an account?{' '}

)}
) }