"use client"; import { useState, FormEvent, useRef, MouseEvent, useEffect } from "react"; interface AuthModalProps { isOpen: boolean; onClose: () => void; } export default function AuthModal({ isOpen, onClose }: AuthModalProps) { const [isLogin, setIsLogin] = useState(true); const modalRef = useRef(null); useEffect(() => { if (isOpen) setIsLogin(true); }, [isOpen]); if (!isOpen) return null; const handleOverlayClick = (e: MouseEvent) => { if (modalRef.current && !modalRef.current.contains(e.target as Node)) { onClose(); } }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); const formData = new FormData(e.currentTarget); const email = formData.get("email") as string; const password = formData.get("password") as string; const name = isLogin ? undefined : (formData.get("name") as string); const endpoint = isLogin ? "/api/login" : "/api/signup"; const body = isLogin ? { email, password } : { name: name!, email, password }; try { const res = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (res.ok) { console.log("Success!"); onClose(); } else { console.error("Error:", await res.text()); } } catch (error) { console.error("Request failed:", error instanceof Error ? error.message : String(error)); } }; return (

{isLogin ? "Login" : "Sign Up"}

{/* Form */}
{!isLogin && (
)}

{isLogin ? "Need an account?" : "Already have an account?"}{" "}

); }