'use client' import { useState, useEffect } from 'react' interface User { id: number username: string email: string } interface AuthModalProps { isOpen: boolean onClose: () => void onLogin: (user: User) => void } export default function AuthModal({ isOpen, onClose, onLogin }: AuthModalProps) { const [isLogin, setIsLogin] = useState(true) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [email, setEmail] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) useEffect(() => { if (isOpen) { // Reset form when modal opens setUsername('') setPassword('') setEmail('') setError('') setIsLogin(true) } }, [isOpen]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') setLoading(true) try { const endpoint = isLogin ? '/api/auth/login' : '/api/auth/register' const body = isLogin ? { username, password } : { username, password, email } const response = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(body), credentials: 'include', // Important for cookies }) const data = await response.json() if (!response.ok) { setError(data.error || 'An error occurred') setLoading(false) return } // Success - call onLogin callback and close modal onLogin(data.user) onClose() } catch (error) { console.error('Auth error:', error) setError('An unexpected error occurred') } finally { setLoading(false) } } if (!isOpen) return null return (