'use client'; import { useState } from 'react'; import type { AuthUser } from '@/types'; type Mode = 'login' | 'register'; interface Props { onSuccess: (user: AuthUser) => void; onClose: () => void; } export default function AuthModal({ onSuccess, onClose }: Props) { const [mode, setMode] = useState('login'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(''); const endpoint = mode === 'login' ? '/api/auth/login' : '/api/auth/register'; try { const res = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); const data = await res.json(); if (!res.ok) { setError(data.error ?? 'Something went wrong'); return; } onSuccess(data.user as AuthUser); } catch { setError('Network error. Please try again.'); } finally { setLoading(false); } } return (
e.target === e.currentTarget && onClose()} >
{/* Header */}

Save your progress

{/* Mode tabs */}
{(['login', 'register'] as const).map((m) => ( ))}
{/* Form */}
setEmail(e.target.value)} required autoComplete="email" placeholder="you@example.com" className="w-full rounded-lg border border-zinc-700 bg-zinc-800 px-3 py-2 text-xs text-zinc-200 placeholder-zinc-600 focus:border-blue-500 focus:outline-none" />
setPassword(e.target.value)} required autoComplete={mode === 'login' ? 'current-password' : 'new-password'} placeholder={mode === 'register' ? 'At least 8 characters' : '••••••••'} className="w-full rounded-lg border border-zinc-700 bg-zinc-800 px-3 py-2 text-xs text-zinc-200 placeholder-zinc-600 focus:border-blue-500 focus:outline-none" />
{error && (
{error}
)}

{mode === 'login' ? ( <>No account?{' '} ) : ( <>Already have an account?{' '} )}

); }