import React, { useState, useEffect } from "react"; import { useAuth } from "../contexts/AuthContext"; import PasswordStrengthMeter from "./PasswordStrengthMeter"; import PasswordInput from "./PasswordInput"; import ForgotPasswordModal from "./ForgotPasswordModal"; interface AuthModalProps { show: boolean; onHide: () => void; initialMode?: "login" | "signup"; } const AuthModal: React.FC = ({ show, onHide, initialMode = "login", }) => { const [mode, setMode] = useState<"login" | "signup">(initialMode); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [showForgotPassword, setShowForgotPassword] = useState(false); const { login, register } = useAuth(); // Update mode when modal is opened with different initialMode useEffect(() => { if (show && initialMode) { setMode(initialMode); } }, [show, initialMode]); const resetModal = () => { setError(""); setEmail(""); setPassword(""); setFirstName(""); setLastName(""); }; const handleGoogleLogin = () => { const clientId = process.env.REACT_APP_GOOGLE_CLIENT_ID; const redirectUri = `${window.location.origin}/auth/google/callback`; const scope = 'email profile'; const responseType = 'code'; const googleAuthUrl = `https://accounts.google.com/o/oauth2/v2/auth?` + `client_id=${encodeURIComponent(clientId || '')}` + `&redirect_uri=${encodeURIComponent(redirectUri)}` + `&response_type=${responseType}` + `&scope=${encodeURIComponent(scope)}` + `&access_type=offline` + `&prompt=consent`; window.location.href = googleAuthUrl; }; const handleEmailSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(""); try { if (mode === "login") { await login(email, password); onHide(); } else { await register({ email, password, firstName, lastName, username: email.split("@")[0], // Generate username from email }); onHide(); } } catch (err: any) { setError(err.response?.data?.message || "An error occurred"); } finally { setLoading(false); } }; if (!show && !showForgotPassword) return null; return ( <> {!showForgotPassword && (

Welcome to CommunityRentals.App

{error && (
{error}
)} {/* Email Form */}
{mode === "signup" && ( <>
setFirstName(e.target.value)} required />
setLastName(e.target.value)} required />
)}
setEmail(e.target.value)} required />
setPassword(e.target.value)} required /> {mode === "signup" && (
)} {mode === "login" && ( )}

or
{/* Social Login Options */}

By continuing, you agree to CommunityRentals.App's{" "} Terms of Service {" "} and{" "} Privacy Policy .

)} {/* Forgot Password Modal */} setShowForgotPassword(false)} onBackToLogin={() => setShowForgotPassword(false)} /> ); }; export default AuthModal;