import { useState } from 'react' import { useAuth } from '../hooks/useAuth' import { colors } from '../constants/theme' import { apiUrl } from '../config' interface AuthModalProps { isOpen: boolean onClose: () => void } export function AuthModal({ isOpen, onClose }: AuthModalProps) { const { login } = useAuth() const [isLogin, setIsLogin] = useState(true) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [isLoading, setIsLoading] = useState(false) if (!isOpen) return null const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') setIsLoading(true) try { const endpoint = isLogin ? '/api/auth/login' : '/api/auth/register' const res = await fetch(apiUrl(endpoint), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }) if (!res.ok) { const text = await res.text() throw new Error(text || 'Authentication failed') } const data = await res.json() as any if (isLogin) { login(data.username, data.token) onClose() } else { // After register, switch to login or auto-login? // The backend register returns { success: true, username } // Let's just switch to login view and show success message setIsLogin(true) setError('Registration successful! Please log in.') setPassword('') } } catch (e: any) { setError(e.message) } finally { setIsLoading(false) } } return (