"use client"; // Mark as a Client Component import { useState } from "react"; import { signIn } from "next-auth/react"; import { useRouter } from "next/navigation"; import Link from "next/link"; export default function Login() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const handleLogin = async (e) => { e.preventDefault(); setIsLoading(true); setError(""); try { const result = await signIn("credentials", { redirect: false, username, password, }); if (result?.error) { setError("Invalid username or password"); setIsLoading(false); } else { // Fetch the user role to redirect appropriately const response = await fetch("/api/user"); const userData = await response.json(); if (userData.role === "admin") { router.push("/admin"); } else { router.push("/team-selection"); } } } catch (err) { setError("An unexpected error occurred"); console.error(err); setIsLoading(false); } }; return (