'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 (
e.stopPropagation()} >

{isLogin ? 'Login' : 'Register'}

{!isLogin && (
setEmail(e.target.value)} required style={{ width: '100%', padding: '12px', borderRadius: '8px', border: '1px solid var(--border)', background: 'var(--bg-soft)', color: 'var(--text)', fontSize: '14px', }} placeholder="your@email.com" />
)}
setUsername(e.target.value)} required minLength={isLogin ? undefined : 3} style={{ width: '100%', padding: '12px', borderRadius: '8px', border: '1px solid var(--border)', background: 'var(--bg-soft)', color: 'var(--text)', fontSize: '14px', }} placeholder="username" />
setPassword(e.target.value)} required minLength={isLogin ? undefined : 6} style={{ width: '100%', padding: '12px', borderRadius: '8px', border: '1px solid var(--border)', background: 'var(--bg-soft)', color: 'var(--text)', fontSize: '14px', }} placeholder="password" />
{error && (
{error}
)}
{isLogin ? ( <> Don't have an account?{' '} ) : ( <> Already have an account?{' '} )}
) }