The App Router server component to protect.
Optionaloptions: ProtectAppPageOptionsOptional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied).
A wrapped page component that enforces authentication before rendering.
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.
Props returned from getServerSideProps.
Query parameters parsed from the URL.
Optionaloptions: ProtectPagePageOptions<P, Q>Optional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied).
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 }
})
});
Restricts access to App Router server-rendered pages.
Access control
onAccessDeniedis invoked (or default behavior applies).onGroupAccessDeniedis invoked (or the default "Access Denied" view is rendered).Both behaviors can be customized via options.