39 lines
854 B
TypeScript
39 lines
854 B
TypeScript
import { auth } from "@/app/(auth)/auth";
|
|
import { ChatSDKError } from "@/lib/errors";
|
|
|
|
export function isSiteAdminType(type?: string | null) {
|
|
return type === "admin";
|
|
}
|
|
|
|
export function isAgentWorkflowAdminType(type?: string | null) {
|
|
return type === "admin" || type === "agent_admin";
|
|
}
|
|
|
|
export async function requireAdmin() {
|
|
const session = await auth();
|
|
|
|
if (!session?.user) {
|
|
throw new ChatSDKError("unauthorized:admin");
|
|
}
|
|
|
|
if (!isSiteAdminType(session.user.type)) {
|
|
throw new ChatSDKError("forbidden:admin");
|
|
}
|
|
|
|
return session;
|
|
}
|
|
|
|
export async function requireAgentWorkflowAdmin() {
|
|
const session = await auth();
|
|
|
|
if (!session?.user) {
|
|
throw new ChatSDKError("unauthorized:admin");
|
|
}
|
|
|
|
if (!isAgentWorkflowAdminType(session.user.type)) {
|
|
throw new ChatSDKError("forbidden:admin");
|
|
}
|
|
|
|
return session;
|
|
}
|