Creates a new client instance.
Optionaloptions: MonoCloudOptionsOptional 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: MonoCloudMiddlewareOptionsOptional 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.
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.
Optionalres: Response | NextResponse<unknown>Optional response to update if session resolution requires refreshed authentication cookies or headers.
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.
Returns the resolved session, or undefined if none exists.
getSession for full docs and examples.
Optionaloptions: GetTokensOptionsOptional 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: GetTokensOptionsOptional 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: GetTokensOptionsOptional 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: GetTokensOptionsOptional 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: IsUserInGroupOptionsOptional 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: IsUserInGroupOptionsOptional 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: IsUserInGroupOptionsOptional 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: IsUserInGroupOptionsOptional 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: MonoCloudAuthOptionsOptional 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.
Optionaloptions: ProtectOptionsOptional 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: ProtectApiAppOptionsOptional 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: ProtectApiPageOptionsOptional 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: ProtectAppPageOptionsOptional 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: RedirectToSignInOptionsOptional 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: RedirectToSignOutOptionsOptional 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_callback