MonoCloud Authentication SDK
    Preparing search index...
    • Restricts access to App Router server-rendered pages.

      Access control

      • If the user is not authenticated, onAccessDenied is invoked (or default behavior applies).
      • If the user is authenticated but fails group checks, onGroupAccessDenied is invoked (or the default "Access Denied" view is rendered).

      Both behaviors can be customized via options.

      Parameters

      • component: ProtectedAppServerComponent

        The App Router server component to protect.

      • Optionaloptions: ProtectAppPageOptions

        Optional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied).

      Returns AppRouterPageHandler

      A wrapped page component that enforces authentication before rendering.

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

      export default protectPage(async function Home({ user }) {
      return <>Hi {user.email}. You accessed a protected page.</>;
      });
      import { protectPage } from "@monocloud/auth-nextjs";

      export default protectPage(
      async function Home({ user }) {
      return <>Hi {user.email}. You accessed a protected page.</>;
      },
      {
      returnUrl: "/dashboard",
      groups: ["admin"],
      }
      );
    • Restricts access to Pages Router server-rendered pages using getServerSideProps.

      Access control

      • If the user is not authenticated, onAccessDenied is invoked (or default behavior applies).
      • If the user is authenticated but fails group checks, the page can still render and groupAccessDenied is provided in props. Use onGroupAccessDenied to customize the props or behavior.

      Both behaviors can be customized via options.

      Type Parameters

      • P extends Record<string, any> = Record<string, any>

        Props returned from getServerSideProps.

      • Q extends ParsedUrlQuery = ParsedUrlQuery

        Query parameters parsed from the URL.

      Parameters

      • Optionaloptions: ProtectPagePageOptions<P, Q>

        Optional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied).

      Returns ProtectPagePageReturnType<P, Q>

      A getServerSideProps wrapper that enforces authentication before executing the page logic.

      import { protectPage, MonoCloudUser } from "@monocloud/auth-nextjs";

      type Props = {
      user: MonoCloudUser;
      };

      export default function Home({ user }: Props) {
      return <>Hi {user.email}. You accessed a protected page.</>;
      }

      export const getServerSideProps = protectPage();
      import { protectPage, MonoCloudUser } from "@monocloud/auth-nextjs";
      import { GetServerSidePropsContext } from "next";

      type Props = {
      user: MonoCloudUser;
      url: string;
      };

      export default function Home({ user, url }: Props) {
      console.log(url);
      return <div>Hi {user?.email}. You accessed a protected page.</div>;
      }

      export const getServerSideProps = protectPage({
      returnUrl: "/dashboard",
      groups: ["admin"],
      getServerSideProps: async (context: GetServerSidePropsContext) => ({
      props: { url: context.resolvedUrl }
      })
      });