'use client'; import { useState, useEffect } from 'react'; import { useAuth } from '@/lib/auth-context'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import type { ApiError } from '@/lib/api'; export default function AdminLoginPage() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); const { login, isAuthenticated, isLoading: authLoading } = useAuth(); const router = useRouter(); // Redirect if already logged in useEffect(() => { if (!authLoading && isAuthenticated) { router.push('/admin'); } }, [isAuthenticated, authLoading, router]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setIsLoading(true); try { await login(username, password); router.push('/admin'); } catch (err) { const apiError = err as ApiError; setError(apiError.message || 'Login failed'); } finally { setIsLoading(false); } }; // Show loading while checking auth status if (authLoading) { return (

Loading...

); } // Don't render login form if already authenticated (will redirect) if (isAuthenticated) { return null; } return (
{/* Hero Section */}

Admin Login

Sign in to your account

Back to Home
{/* Main Content */}
{error && (

{error}

)}
setUsername(e.target.value)} />
setPassword(e.target.value)} />
); }