Refactor code
This commit is contained in:
@ -4,13 +4,15 @@ import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { headers } from "next/headers";
|
||||
import { z } from "zod/v4";
|
||||
import type { SessionWithRole } from "@/types/auth";
|
||||
|
||||
// Deletes a user account with safety checks (admin-only, cannot delete self or last admin).
|
||||
export async function deleteUser(id: string) {
|
||||
const userId = z.string().min(1).parse(id);
|
||||
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
const role = (session as any)?.user?.role as string | undefined;
|
||||
const currentUserId = (session as any)?.user?.id as string | undefined;
|
||||
const role = (session as SessionWithRole)?.user?.role;
|
||||
const currentUserId = (session as SessionWithRole)?.user?.id;
|
||||
|
||||
if (!session || role !== "admin") throw new Error("Forbidden");
|
||||
if (!currentUserId) throw new Error("Session missing user id");
|
||||
@ -40,5 +42,5 @@ async function await_attachTarget(userId: string) {
|
||||
select: { id: true, role: true },
|
||||
});
|
||||
if (!target) throw new Error("User not found.");
|
||||
return target as { id: string; role: "admin" | "user" };
|
||||
return target;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user