feat: add delete user functionality for site admins
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
"use client"
|
||||
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
interface UserActionsProps {
|
||||
userId: string;
|
||||
isSiteAdmin: boolean;
|
||||
currentUserId: string;
|
||||
}
|
||||
|
||||
export default function UserActions({ userId, isSiteAdmin, currentUserId }: UserActionsProps) {
|
||||
const router = useRouter()
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (confirm("Are you sure you want to delete this user?")) {
|
||||
const response = await fetch(`/api/admin/users/${userId}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
if (response.ok) {
|
||||
router.refresh();
|
||||
} else {
|
||||
const data = await response.json();
|
||||
alert(data.error || "Failed to delete user");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex space-x-2">
|
||||
<a
|
||||
href={`/admin/users/${userId}/edit`}
|
||||
className="text-blue-600 hover:text-blue-900"
|
||||
>
|
||||
Edit
|
||||
</a>
|
||||
{isSiteAdmin && userId !== currentUserId && (
|
||||
<button
|
||||
onClick={handleDelete}
|
||||
className="text-red-600 hover:text-red-900"
|
||||
>
|
||||
Delete
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import Link from "next/link"
|
||||
import { redirect } from "next/navigation"
|
||||
import { getSession } from "@/lib/auth-simple"
|
||||
import { hasRole } from "@/lib/permissions"
|
||||
import UserActions from "./components/UserActions"
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -16,6 +17,9 @@ export default async function AdminUsersPage() {
|
||||
|
||||
const session = await getSession();
|
||||
const userId = session?.user?.id || session?.user?.userId;
|
||||
|
||||
// Check if user is site_admin for delete permissions
|
||||
const isSiteAdmin = await hasRole("site_admin");
|
||||
|
||||
// Fetch all users with their associated players
|
||||
const users = await prisma.user.findMany({
|
||||
@@ -121,14 +125,11 @@ export default async function AdminUsersPage() {
|
||||
{new Date(user.createdAt).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm">
|
||||
<div className="flex space-x-2">
|
||||
<Link
|
||||
href={`/admin/users/${user.id}/edit`}
|
||||
className="text-blue-600 hover:text-blue-900"
|
||||
>
|
||||
Edit
|
||||
</Link>
|
||||
</div>
|
||||
<UserActions
|
||||
userId={user.id}
|
||||
isSiteAdmin={isSiteAdmin.allowed}
|
||||
currentUserId={userId || ""}
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
|
||||
Reference in New Issue
Block a user