'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { Box, Paper, TextField, Button, Typography, Alert, CircularProgress, Container } from '@mui/material'; import { AdminPanelSettings } from '@mui/icons-material'; export function AdminLoginForm() { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const router = useRouter(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); try { const response = await fetch('/api/admin/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ email, password }), }); const data = await response.json(); if (response.ok) { // Force a small delay to ensure the cookie is set setTimeout(() => { router.push('/admin'); router.refresh(); }, 100); } else { setError(data.error || 'Login failed'); } } catch (error) { setError('Network error. Please try again.'); } finally { setLoading(false); } }; return ( Admin Portal Sign in to access the admin dashboard {error && ( {error} )} setEmail(e.target.value)} disabled={loading} /> setPassword(e.target.value)} disabled={loading} /> Admin access only. Contact system administrator if you need access. ); }