"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; export function LoginForm() { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); async function onSubmit(event: React.FormEvent) { event.preventDefault(); setIsLoading(true); setError(""); try { const response = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ username, password, }), }); if (!response.ok) { throw new Error("Login failed"); } const data = await response.json(); if (data.success) { // Redirect to the dashboard router.push("/dashboard"); router.refresh(); } else { setError("Authentication failed. Please check your credentials and try again."); } } catch (error) { setError("Something went wrong. Please try again."); } finally { setIsLoading(false); } } return (