Sign in

Function: protectPage

Call Signature

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

ParameterTypeDescription
componentProtectedAppServerComponentThe App Router server component to protect.
options?ProtectAppPageOptionsOptional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied).

Returns

AppRouterPageHandler

A wrapped page component that enforces authentication before rendering.

Examples

src/app/page.tsx
import { protectPage } from "@monocloud/auth-nextjs";

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

Call Signature

protectPage<P, Q>(options?: ProtectPagePageOptions<P, Q>): ProtectPagePageReturnType<P, Q>

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

Type ParameterDescription
P extends Record<string, any>Props returned from getServerSideProps.
Q extends ParsedUrlQueryQuery parameters parsed from the URL.

Parameters

ParameterTypeDescription
options?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.

Examples

src/pages/index.tsx
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();
© 2024 MonoCloud. All rights reserved.