Creates a new client instance.
Optionaloptions: MonoCloudOptions
Optional configuration for initializing the MonoCloud client. If not provided, settings are automatically resolved from environment variables.
This exposes the framework-agnostic MonoCloud client used internally by the Next.js SDK. Use it if you need access to lower-level functionality not directly exposed by MonoCloudNextClient.
Returns the underlying Node client instance.
This is intended for advanced scenarios requiring direct control over the authorization or token flow.
Returns the underlying OIDC client used for OpenID Connect operations.
Optionaloptions: MonoCloudMiddlewareOptions
Optional configuration that controls how authentication is enforced (for example, redirect behavior, route matching, or custom handling of unauthenticated requests).
Returns a Next.js middleware result (NextResponse, redirect, or undefined to continue processing).
authMiddleware for full docs and examples.
Incoming Next.js middleware request used to resolve authentication state.
Next.js middleware event providing lifecycle hooks such as waitUntil.
Returns a Next.js middleware result (NextResponse, redirect, or undefined to continue processing).
authMiddleware for full docs and examples.
Optionaloptions: GetSessionOptions
Optional configuration controlling session retrieval behavior.
Returns the resolved session, or undefined if none exists.
getSession for full docs and examples.
Incoming request used to read authentication cookies and headers to resolve the current user's session.
Optionaloptions: GetSessionOptions
Optional configuration controlling session retrieval behavior.
Returns the resolved session, or undefined if none exists.
getSession for full docs and examples.
Incoming request used to read authentication cookies and headers to resolve the current user's session.
Optional response to update if session resolution requires refreshed authentication cookies or headers.
Optionaloptions: GetSessionOptions
Optional configuration controlling session retrieval behavior.
Returns the resolved session, or undefined if none exists.
getSession for full docs and examples.
Incoming Node.js request used to read authentication cookies and resolve the current user's session.
Outgoing Node.js response used to apply refreshed authentication cookies when required.
Optionaloptions: GetSessionOptions
Optional configuration controlling session retrieval behavior.
Returns the resolved session, or undefined if none exists.
getSession for full docs and examples.
Optionaloptions: GetTokensOptions
Optional configuration controlling refresh behavior and resource/scope selection.
The current user's tokens, refreshed if necessary.
getTokens for full docs and examples.
MonoCloudValidationError If no valid session exists.
Incoming request used to resolve authentication from cookies and headers.
Optionaloptions: GetTokensOptions
Optional configuration controlling refresh behavior and resource/scope selection.
The current user's tokens, refreshed if necessary.
getTokens for full docs and examples.
MonoCloudValidationError If no valid session exists.
Incoming request used to resolve authentication from cookies and headers.
Existing response to update with refreshed authentication cookies or headers.
Optionaloptions: GetTokensOptions
Optional configuration controlling refresh behavior and resource/scope selection.
The current user's tokens, refreshed if necessary.
getTokens for full docs and examples.
MonoCloudValidationError If no valid session exists.
Incoming Node.js request used to resolve authentication from cookies.
Outgoing Node.js response used to apply refreshed authentication cookies when required.
Optionaloptions: GetTokensOptions
Optional configuration controlling refresh behavior and resource/scope selection.
The current user's tokens, refreshed if necessary.
getTokens for full docs and examples.
MonoCloudValidationError If no valid session exists.
Returns true if a valid session exists; otherwise false.
isAuthenticated for full docs and examples.
Incoming request used to resolve authentication from cookies and headers.
Optionalres: Response | NextResponse<unknown>
Optional response to update if refreshed authentication cookies or headers are required.
Returns true if a valid session exists; otherwise false.
isAuthenticated for full docs and examples.
Incoming Node.js request used to resolve authentication from cookies.
Outgoing Node.js response used to apply refreshed authentication cookies when required.
Returns true if a valid session exists; otherwise false.
isAuthenticated for full docs and examples.
Group IDs or names to check against the user's group memberships.
Optionaloptions: IsUserInGroupOptions
Optional configuration controlling how group membership is evaluated.
Returns true if the user belongs to at least one specified group; otherwise false.
isUserInGroup for full docs and examples.
Incoming request used to resolve authentication from cookies and headers.
Group IDs or names to check against the user's group memberships.
Optionaloptions: IsUserInGroupOptions
Optional configuration controlling how group membership is evaluated.
Returns true if the user belongs to at least one specified group; otherwise false.
isUserInGroup for full docs and examples.
Incoming request used to resolve authentication from cookies and headers.
Existing response to update with refreshed authentication cookies or headers when required.
Group IDs or names to check against the user's group memberships.
Optionaloptions: IsUserInGroupOptions
Optional configuration controlling how group membership is evaluated.
Returns true if the user belongs to at least one specified group; otherwise false.
isUserInGroup for full docs and examples.
Incoming Node.js request used to resolve authentication from cookies.
Outgoing Node.js response used to apply refreshed authentication cookies when required.
Group IDs or names to check against the user's group memberships.
Optionaloptions: IsUserInGroupOptions
Optional configuration controlling how group membership is evaluated.
Returns true if the user belongs to at least one specified group; otherwise false.
isUserInGroup for full docs and examples.
Optionaloptions: MonoCloudAuthOptions
Optional configuration for the auth handler.
Returns a Next.js-compatible handler for App Router route handlers or Pages Router API routes.
monoCloudAuth for full docs and examples.
Both the
form_postresponse mode and back-channel logout deliver requests to the auth routes asPOST. If you configure either, export the handler forPOSTas well asGET.
Optionaloptions: ProtectOptions
Optional configuration for redirect behavior (for example, return URL or sign-in parameters).
Resolves if the user is authenticated; otherwise triggers a redirect.
protect for full docs and examples.
The route handler to protect.
Optionaloptions: ProtectApiAppOptions
Optional configuration controlling authentication and authorization behavior.
Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
protectApi for full docs and examples.
The route handler to protect.
Optionaloptions: ProtectApiPageOptions
Optional configuration controlling authentication and authorization behavior.
Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
protectApi for full docs and examples.
The App Router server component to protect.
Optionaloptions: ProtectAppPageOptions
Optional configuration for authentication, authorization, and custom access handling (onAccessDenied, onGroupAccessDenied).
A wrapped page component that enforces authentication before rendering.
protectPage for full docs and examples.
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.
protectPage for full docs and examples.
Optionaloptions: RedirectToSignInOptions
Optional configuration for the redirect, such as returnUrl or additional sign-in parameters.
Never resolves. Triggers a redirect to the sign-in flow.
redirectToSignIn for full docs and examples.
Optionaloptions: RedirectToSignOutOptions
Optional configuration for the redirect, such as postLogoutRedirectUri or additional sign-out parameters.
Never resolves. Triggers a redirect to the sign-out flow.
redirectToSignOut for full docs and examples.
MonoCloudNextClientis the core SDK entry point for integrating MonoCloud authentication into a Next.js application.It provides:
1. Add environment variables
2. Register middleware
Advanced usage
Create a shared client instance
By default, the SDK exposes function exports (for example,
authMiddleware(),getSession(),getTokens()) that internally use a shared singletonMonoCloudNextClient.Create your own
MonoCloudNextClientinstance when you need multiple configurations, dependency injection, or explicit control over initialization.Using instance methods
Once you create a client instance, call methods directly on it instead of using the default function exports.
Using constructor options
When configuration is provided through both constructor options and environment variables, the values passed to the constructor take precedence. Environment variables are used only for options that are not explicitly supplied.
Modifying default routes
If you customize any of the default auth route paths:
NEXT_PUBLIC_environment variables so client-side helpers (for example<SignIn />,<SignOut />, anduseAuth()) can discover the correct URLs.Example:
When routes are overridden, the Redirect URI configured in the dashboard must reflect the new path. For example, during local development:
http://localhost:3000/api/custom_callbackNext.js-specific environment variables
In addition to the
MONOCLOUD_AUTH_*variables documented on MonoCloudOptions, the Next.js SDK reads the following variables.These are browser-facing mirrors of the corresponding
MONOCLOUD_AUTH_*options, set them when you override the matching server-side route orgroupsClaim.NEXT_PUBLIC_MONOCLOUD_AUTH_SIGNIN_URL<SignIn>,<SignUp>, and client-side page protection to redirect the browser. Set this to matchMONOCLOUD_AUTH_SIGNIN_URLwhen you override the sign-in route.NEXT_PUBLIC_MONOCLOUD_AUTH_SIGNOUT_URL<SignOut>. Set this to matchMONOCLOUD_AUTH_SIGNOUT_URLwhen you override the sign-out route.NEXT_PUBLIC_MONOCLOUD_AUTH_USER_INFO_URLuseAuth()to fetch the authenticated user. Set this to matchMONOCLOUD_AUTH_USER_INFO_URLwhen you override the userinfo route.NEXT_PUBLIC_MONOCLOUD_AUTH_GROUPS_CLAIMgroupsClaimoption (MONOCLOUD_AUTH_GROUPS_CLAIM), used by client components such as<Protected>and client-side page protection.