MonoCloud Authentication SDK
    Preparing search index...
    • Checks whether the currently authenticated user is a member of any of the specified groups.

      The groups parameter accepts group identifiers (IDs or names).

      The authenticated user's session may contain groups represented as:

      • Group IDs
      • Group names
      • Group objects (for example, { id: string; name: string })

      Matching is always performed against the group's ID and name, regardless of how the group is represented in the session.

      Parameters

      • groups: string[]

        Group IDs or names to check against the user's group memberships.

      • Optionaloptions: IsUserInGroupOptions

        Optional configuration controlling how group membership is evaluated.

      Returns Promise<boolean>

      Returns true if the user belongs to at least one specified group; otherwise false.

      import { isUserInGroup } from "@monocloud/auth-nextjs";

      export default async function AdminPanel() {
      const isAdmin = await isUserInGroup(["admin"]);

      if (!isAdmin) {
      return <div>Access Denied</div>;
      }

      return <div>Admin Control Panel</div>;
      }
      "use server";

      import { isUserInGroup } from "@monocloud/auth-nextjs";

      export async function deletePostAction() {
      const canDelete = await isUserInGroup(["admin", "editor"]);

      if (!canDelete) {
      return { success: false };
      }

      return { success: true };
      }
      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { NextResponse } from "next/server";

      export const GET = async () => {
      const allowed = await isUserInGroup(["admin", "editor"]);

      if (!allowed) {
      return new NextResponse("Forbidden", { status: 403 });
      }

      return NextResponse.json({ status: "success" });
      };
      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { NextResponse } from "next/server";

      export default async function proxy() {
      const isAdmin = await isUserInGroup(["admin"]);

      if (!isAdmin) {
      return new NextResponse("User is not admin", { status: 403 });
      }

      return NextResponse.next();
      }
    • Checks group membership using an explicit Web or Next.js request.

      Use this overload when you already have access to a Request or NextRequest (for example, in Middleware or Route Handlers).

      Parameters

      • req: Request | NextRequest

        Incoming request used to resolve authentication from cookies and headers.

      • groups: string[]

        Group IDs or names to check against the user's group memberships.

      • Optionaloptions: IsUserInGroupOptions

        Optional configuration controlling how group membership is evaluated.

      Returns Promise<boolean>

      Returns true if the user belongs to at least one specified group; otherwise false.

      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export default async function proxy(req: NextRequest) {
      const isAdmin = await isUserInGroup(req, ["admin"]);

      if (!isAdmin) {
      return new NextResponse("User is not admin", { status: 403 });
      }

      return NextResponse.next();
      }
      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export const GET = async (req: NextRequest) => {
      const isMember = await isUserInGroup(req, ["admin", "editor"]);

      return NextResponse.json({ isMember });
      };
    • Checks group membership using an explicit request and response.

      Use this overload when you have already created a response and want refreshed authentication cookies or headers applied to it.

      Parameters

      • req: Request | NextRequest

        Incoming request used to resolve authentication from cookies and headers.

      • res: Response | NextResponse<unknown>

        Existing response to update with refreshed authentication cookies or headers when required.

      • groups: string[]

        Group IDs or names to check against the user's group memberships.

      • Optionaloptions: IsUserInGroupOptions

        Optional configuration controlling how group membership is evaluated.

      Returns Promise<boolean>

      Returns true if the user belongs to at least one specified group; otherwise false.

      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export default async function proxy(req: NextRequest) {
      const res = NextResponse.next();

      const isAdmin = await isUserInGroup(req, res, ["admin"]);

      if (!isAdmin) {
      return new NextResponse("User is not admin", { status: 403 });
      }

      res.headers.set("x-user", "admin");

      return res;
      }
      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export const GET = async (req: NextRequest) => {
      const res = new NextResponse("Restricted Content");

      const allowed = await isUserInGroup(req, res, ["admin"]);

      if (!allowed) {
      return new NextResponse("Not Allowed", res);
      }

      return res;
      };
    • Checks group membership in the Pages Router or Node.js runtime.

      Use this overload in API routes or getServerSideProps, where Node.js request and response objects are available.

      Parameters

      • req: NextApiRequest | IncomingMessage

        Incoming Node.js request used to resolve authentication from cookies.

      • res: NextApiResponse | ServerResponse<IncomingMessage>

        Outgoing Node.js response used to apply refreshed authentication cookies when required.

      • groups: string[]

        Group IDs or names to check against the user's group memberships.

      • Optionaloptions: IsUserInGroupOptions

        Optional configuration controlling how group membership is evaluated.

      Returns Promise<boolean>

      Returns true if the user belongs to at least one specified group; otherwise false.

      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { GetServerSideProps } from "next";

      type Props = {
      isAdmin: boolean;
      };

      export default function Home({ isAdmin }: Props) {
      return <div>User is admin: {isAdmin.toString()}</div>;
      }

      export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
      const isAdmin = await isUserInGroup(ctx.req, ctx.res, ["admin"]);

      return {
      props: {
      isAdmin
      }
      };
      };
      import { isUserInGroup } from "@monocloud/auth-nextjs";
      import { NextApiRequest, NextApiResponse } from "next";

      export default async function handler(
      req: NextApiRequest,
      res: NextApiResponse
      ) {
      const isAdmin = await isUserInGroup(req, res, ["admin"]);

      if (!isAdmin) {
      return res.status(403).json({ error: "Forbidden" });
      }

      res.status(200).json({ message: "Welcome Admin" });
      }