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 (

{isLogin ? 'Welcome Back' : 'Create Account'}

{error && (
{error}
)}
setUsername(e.target.value)} required style={{ width: '100%', padding: '12px', background: '#222', border: '1px solid #333', borderRadius: 6, color: '#fff', outline: 'none', fontSize: '1rem' }} onFocus={e => e.currentTarget.style.borderColor = '#555'} onBlur={e => e.currentTarget.style.borderColor = '#333'} />
setPassword(e.target.value)} required style={{ width: '100%', padding: '12px', background: '#222', border: '1px solid #333', borderRadius: 6, color: '#fff', outline: 'none', fontSize: '1rem' }} onFocus={e => e.currentTarget.style.borderColor = '#555'} onBlur={e => e.currentTarget.style.borderColor = '#333'} />
{isLogin ? "Don't have an account? " : "Already have an account? "}
) }