Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

667 B
Raw Blame
import React from "react";
import * as Sentry from "@sentry/nextjs";
import { redirect } from "next/navigation";
import { getServerUser } from "./auth/server/getServerUser";

export async function withRoleAccess(allowedRoles: string[]) {
  "use server";
  return await Sentry.withServerActionInstrumentation(
    "withRoleAccess",
    {},
    async () => {
      return async function <T extends React.ComponentType<any>>(Component: T) {
        const { user, role, error } = await getServerUser();

        if (error || !user || !role || !allowedRoles.includes(role)) {
          redirect("/unauthorized");
        }
        return Component;
      };
    },
  );
}