protectPage(component:ProtectedAppServerComponent,options?:ProtectAppPageOptions):AppRouterPageHandler
Restricts access to App Router server-rendered pages.
Access control
onAccessDenied is invoked (or default behavior applies).onGroupAccessDenied is invoked (or the default "Access Denied" view is rendered).Both behaviors can be customized via options.
| Parameter | Type | Description |
|---|---|---|
component | ProtectedAppServerComponent | The App Router server component to protect. |
options? | ProtectAppPageOptions | Optional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied). |
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.</>;
});
Restricts access to Pages Router server-rendered pages using getServerSideProps.
Access control
onAccessDenied is invoked (or default behavior applies).groupAccessDenied is provided in props. Use onGroupAccessDenied to customize the props or behavior.Both behaviors can be customized via options.
| Type Parameter | Description |
|---|---|
P extends Record<string, any> | Props returned from getServerSideProps. |
Q extends ParsedUrlQuery | Query parameters parsed from the URL. |
| Parameter | Type | Description |
|---|---|---|
options? | ProtectPagePageOptions<P, Q> | Optional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied). |
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();