"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { UserSession } from "../../../lib/types"; interface UserItem { id: string; email: string; role: string; } export default function UserManagementPage() { const { data: session, status } = useSession(); const [users, setUsers] = useState([]); const [email, setEmail] = useState(""); const [role, setRole] = useState("user"); const [message, setMessage] = useState(""); const [loading, setLoading] = useState(true); useEffect(() => { if (status === "authenticated") { fetchUsers(); } }, [status]); const fetchUsers = async () => { setLoading(true); try { const res = await fetch("/api/dashboard/users"); const data = await res.json(); setUsers(data.users); } catch (error) { console.error("Failed to fetch users:", error); setMessage("Failed to load users."); } finally { setLoading(false); } }; async function inviteUser() { setMessage(""); try { const res = await fetch("/api/dashboard/users", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, role }), }); if (res.ok) { setMessage("User invited successfully!"); setEmail(""); // Clear the form // Refresh the user list fetchUsers(); } else { const error = await res.json(); setMessage( `Failed to invite user: ${error.message || "Unknown error"}` ); } } catch (error) { setMessage("Failed to invite user. Please try again."); console.error("Error inviting user:", error); } } // Loading state if (loading) { return
Loading users...
; } // Check for admin access if (session?.user?.role !== "admin") { return (

Access Denied

You don't have permission to view user management.

); } return (

User Management

{message && (
{message}
)}

Invite New User

{ e.preventDefault(); inviteUser(); }} autoComplete="off" // Disable autofill for the form >
setEmail(e.target.value)} required autoComplete="off" // Disable autofill for this input />

Current Users

{users.length === 0 ? ( ) : ( users.map((user) => ( )) )}
Email Role Actions
No users found
{user.email} {user.role} {/* For future: Add actions like edit, delete, etc. */} No actions available
); }