@monocloud/auth-nextjs 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/client/index.cjs +3 -3
- package/dist/client/index.d.mts +68 -112
- package/dist/client/index.mjs +2 -2
- package/dist/components/client/index.cjs +40 -84
- package/dist/components/client/index.cjs.map +1 -1
- package/dist/components/client/index.d.mts +55 -90
- package/dist/components/client/index.mjs +38 -82
- package/dist/components/client/index.mjs.map +1 -1
- package/dist/components/index.cjs +44 -41
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.mts +68 -45
- package/dist/components/index.mjs +44 -41
- package/dist/components/index.mjs.map +1 -1
- package/dist/index.cjs +384 -365
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +1545 -1833
- package/dist/index.mjs +380 -372
- package/dist/index.mjs.map +1 -1
- package/dist/{protect-K9srvUkq.mjs → protect-client-page-BFVskb3X.mjs} +58 -106
- package/dist/protect-client-page-BFVskb3X.mjs.map +1 -0
- package/dist/{protect-BCIji2i7.cjs → protect-client-page-BdsnH8gs.cjs} +59 -107
- package/dist/protect-client-page-BdsnH8gs.cjs.map +1 -0
- package/dist/types-ClljFIvK.d.mts +543 -0
- package/package.json +3 -2
- package/dist/protect-BCIji2i7.cjs.map +0 -1
- package/dist/protect-K9srvUkq.mjs.map +0 -1
- package/dist/types-Cx32VRoI.d.mts +0 -409
package/dist/index.d.mts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AccessToken, GetTokensOptions, GetTokensOptions as GetTokensOptions$1, MonoCloudAuthBaseError, MonoCloudCoreClient, MonoCloudHttpError, MonoCloudOPError,
|
|
1
|
+
import { A as ProtectedAppServerComponent, C as ProtectAppPageOptions, D as ProtectPagePageOnGroupAccessDeniedType, E as ProtectPagePageOnAccessDeniedType, F as RedirectToSignOutOptions, M as ProtectedRouteMatcher, N as ProtectedRoutes, O as ProtectPagePageOptions, P as RedirectToSignInOptions, S as ProtectApiPageOptions, T as ProtectPageGetServerSidePropsContext, _ as OnError, a as AppRouterContext, b as PageRouterApiOnGroupAccessDeniedHandler, c as ExtraAuthParams, d as MonoCloudAuthHandler, f as MonoCloudAuthOptions, g as NextMiddlewareResult, h as NextMiddlewareOnGroupAccessDenied, i as AppRouterApiOnGroupAccessDeniedHandler, j as ProtectedAppServerComponentProps, k as ProtectPagePageReturnType, l as GroupOptions, m as NextMiddlewareOnAccessDenied, n as AppRouterApiHandlerFn, o as AppRouterPageHandler, p as MonoCloudMiddlewareOptions, r as AppRouterApiOnAccessDeniedHandler, s as CustomProtectedRouteMatcher, t as AppOnError, u as IsUserInGroupOptions, v as PageOnError, w as ProtectOptions, x as ProtectApiAppOptions, y as PageRouterApiOnAccessDeniedHandler } from "./types-ClljFIvK.mjs";
|
|
2
|
+
import { AccessToken, Address, ApplicationState, Authenticators, AuthorizationParams, CodeChallengeMethod, DisplayOptions, GetTokensOptions, GetTokensOptions as GetTokensOptions$1, Group, IdTokenClaims, Indicator, Jwk, MonoCloudAuthBaseError, MonoCloudCookieOptions, MonoCloudCoreClient, MonoCloudHttpError, MonoCloudOPError, MonoCloudOptions, MonoCloudOptions as MonoCloudOptions$1, MonoCloudRequest, MonoCloudRoutes, MonoCloudSession, MonoCloudSession as MonoCloudSession$1, MonoCloudSessionOptions, MonoCloudSessionOptionsBase, MonoCloudSessionStore, MonoCloudStateOptions, MonoCloudStatePartialOptions, MonoCloudTokenError, MonoCloudTokens, MonoCloudTokens as MonoCloudTokens$1, MonoCloudUser, MonoCloudValidationError, OnBackChannelLogout, OnSessionCreating, OnSetApplicationState, Prompt, ResponseModes, ResponseTypes, SameSiteValues, SecurityAlgorithms, SessionLifetime, UserinfoResponse } from "@monocloud/auth-node-core";
|
|
3
3
|
import { NextFetchEvent, NextMiddleware, NextProxy, NextRequest, NextResponse } from "next/server.js";
|
|
4
|
+
import { NextFetchEvent as NextFetchEvent$1, NextMiddleware as NextMiddleware$1, NextProxy as NextProxy$1, NextRequest as NextRequest$1, NextResponse as NextResponse$1 } from "next/server";
|
|
4
5
|
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next/types";
|
|
5
6
|
import { ParsedUrlQuery } from "node:querystring";
|
|
7
|
+
import { MonoCloudOidcClient, MonoCloudSession as MonoCloudSession$2 } from "@monocloud/auth-core";
|
|
6
8
|
import { IncomingMessage, ServerResponse } from "node:http";
|
|
9
|
+
import { NextApiHandler as NextApiHandler$1, NextApiRequest as NextApiRequest$1, NextApiResponse as NextApiResponse$1 } from "next";
|
|
7
10
|
|
|
8
11
|
//#region src/monocloud-next-client.d.ts
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* `MonoCloudNextClient` is the core SDK entry point for integrating MonoCloud authentication into a Next.js application.
|
|
11
14
|
*
|
|
12
|
-
*
|
|
15
|
+
* It provides:
|
|
16
|
+
* - Authentication middleware
|
|
17
|
+
* - Route protection helpers
|
|
18
|
+
* - Session and token access
|
|
19
|
+
* - Redirect utilities
|
|
20
|
+
* - Server-side enforcement helpers
|
|
13
21
|
*
|
|
14
|
-
* 1. Add
|
|
22
|
+
* ## 1. Add environment variables
|
|
15
23
|
*
|
|
16
|
-
* ```bash
|
|
24
|
+
* ```bash:.env.local
|
|
17
25
|
* MONOCLOUD_AUTH_TENANT_DOMAIN=<tenant-domain>
|
|
18
26
|
* MONOCLOUD_AUTH_CLIENT_ID=<client-id>
|
|
19
27
|
* MONOCLOUD_AUTH_CLIENT_SECRET=<client-secret>
|
|
20
|
-
* MONOCLOUD_AUTH_SCOPES=openid profile email
|
|
28
|
+
* MONOCLOUD_AUTH_SCOPES=openid profile email
|
|
21
29
|
* MONOCLOUD_AUTH_APP_URL=http://localhost:3000
|
|
22
30
|
* MONOCLOUD_AUTH_COOKIE_SECRET=<cookie-secret>
|
|
23
31
|
* ```
|
|
24
32
|
*
|
|
25
|
-
* 2.
|
|
33
|
+
* ## 2. Register middleware
|
|
26
34
|
*
|
|
27
|
-
* ```typescript
|
|
28
|
-
* import {
|
|
35
|
+
* ```typescript:src/proxy.ts
|
|
36
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
29
37
|
*
|
|
30
|
-
* export
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* 3. Add MonoCloud middleware/proxy
|
|
34
|
-
*
|
|
35
|
-
* ```typescript
|
|
36
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
37
|
-
*
|
|
38
|
-
* export default monoCloud.authMiddleware();
|
|
38
|
+
* export default authMiddleware();
|
|
39
39
|
*
|
|
40
40
|
* export const config = {
|
|
41
41
|
* matcher: [
|
|
@@ -44,1926 +44,1638 @@ import { IncomingMessage, ServerResponse } from "node:http";
|
|
|
44
44
|
* };
|
|
45
45
|
* ```
|
|
46
46
|
*
|
|
47
|
-
*
|
|
47
|
+
* ## Advanced usage
|
|
48
|
+
*
|
|
49
|
+
* ### Create a shared client instance
|
|
50
|
+
*
|
|
51
|
+
* By default, the SDK exposes function exports (for example, `authMiddleware()`, `getSession()`, `getTokens()`) that internally use a shared singleton `MonoCloudNextClient`.
|
|
52
|
+
*
|
|
53
|
+
* Create your own `MonoCloudNextClient` instance when you need multiple configurations, dependency injection, or explicit control over initialization.
|
|
54
|
+
*
|
|
55
|
+
* ```ts:src/monocloud.ts
|
|
56
|
+
* import { MonoCloudNextClient } from "@monocloud/auth-nextjs";
|
|
57
|
+
*
|
|
58
|
+
* export const monoCloud = new MonoCloudNextClient();
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* ### Using instance methods
|
|
62
|
+
*
|
|
63
|
+
* Once you create a client instance, call methods directly on it instead of using the default function exports.
|
|
64
|
+
*
|
|
65
|
+
* ```ts:src/app/page.tsx
|
|
66
|
+
* import { monoCloud } from "@/monocloud";
|
|
67
|
+
*
|
|
68
|
+
* export default async function Page() {
|
|
69
|
+
* const session = await monoCloud.getSession();
|
|
48
70
|
*
|
|
49
|
-
*
|
|
71
|
+
* if (!session) {
|
|
72
|
+
* return <>Not signed in</>;
|
|
73
|
+
* }
|
|
50
74
|
*
|
|
51
|
-
*
|
|
75
|
+
* return <>Hello {session.user.name}</>;
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* #### Using constructor options
|
|
52
80
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
81
|
+
* 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.
|
|
82
|
+
*
|
|
83
|
+
* ```ts:src/monocloud.ts
|
|
84
|
+
* import { MonoCloudNextClient } from "@monocloud/auth-nextjs";
|
|
55
85
|
*
|
|
56
86
|
* export const monoCloud = new MonoCloudNextClient({
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
87
|
+
* tenantDomain: "<tenant-domain>",
|
|
88
|
+
* clientId: "<client-id>",
|
|
89
|
+
* clientSecret: "<client-secret>",
|
|
90
|
+
* appUrl: "http://localhost:3000",
|
|
91
|
+
* cookieSecret: "<cookie-secret>",
|
|
92
|
+
* defaultAuthParams: {
|
|
93
|
+
* scopes: "openid profile email",
|
|
94
|
+
* },
|
|
63
95
|
* });
|
|
64
96
|
* ```
|
|
65
|
-
* 2. Add MonoCloud middleware/proxy
|
|
66
97
|
*
|
|
67
|
-
*
|
|
68
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
98
|
+
* ### Modifying default routes
|
|
69
99
|
*
|
|
70
|
-
*
|
|
100
|
+
* If you customize any of the default auth route paths:
|
|
71
101
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
102
|
+
* - Also set the corresponding `NEXT_PUBLIC_` environment variables so client-side helpers
|
|
103
|
+
* (for example `<SignIn />`, `<SignOut />`, and `useAuth()`) can discover the correct URLs.
|
|
104
|
+
* - Update the **Application URLs** in your MonoCloud Dashboard to match the new paths.
|
|
105
|
+
*
|
|
106
|
+
* Example:
|
|
107
|
+
*
|
|
108
|
+
* ```bash:.env.local
|
|
109
|
+
* MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
110
|
+
* NEXT_PUBLIC_MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
77
111
|
* ```
|
|
78
112
|
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* <h4>Core Configuration (Required)</h4>
|
|
82
|
-
*
|
|
83
|
-
* <ul>
|
|
84
|
-
* <li><strong>MONOCLOUD_AUTH_CLIENT_ID : </strong>Unique identifier for your application/client.</li>
|
|
85
|
-
* <li><strong>MONOCLOUD_AUTH_CLIENT_SECRET : </strong>Application/client secret.</li>
|
|
86
|
-
* <li><strong>MONOCLOUD_AUTH_TENANT_DOMAIN : </strong>The domain of your MonoCloud tenant (e.g., https://your-tenant.us.monocloud.com).</li>
|
|
87
|
-
* <li><strong>MONOCLOUD_AUTH_APP_URL : </strong>The base URL where your application is hosted.</li>
|
|
88
|
-
* <li><strong>MONOCLOUD_AUTH_COOKIE_SECRET : </strong>A long, random string used to encrypt and sign session cookies.</li>
|
|
89
|
-
* </ul>
|
|
90
|
-
*
|
|
91
|
-
* <h4>Authentication & Security</h4>
|
|
92
|
-
*
|
|
93
|
-
* <ul>
|
|
94
|
-
* <li><strong>MONOCLOUD_AUTH_SCOPES : </strong>A space-separated list of OIDC scopes to request (e.g., openid profile email).</li>
|
|
95
|
-
* <li><strong>MONOCLOUD_AUTH_RESOURCE : </strong>The default resource/audience identifier for access tokens.</li>
|
|
96
|
-
* <li><strong>MONOCLOUD_AUTH_USE_PAR : </strong>Enables Pushed Authorization Requests.</li>
|
|
97
|
-
* <li><strong>MONOCLOUD_AUTH_CLOCK_SKEW : </strong>The allowed clock drift in seconds when validating token timestamps.</li>
|
|
98
|
-
* <li><strong>MONOCLOUD_AUTH_FEDERATED_SIGNOUT : </strong>If true, signs the user out of MonoCloud (SSO sign-out) when they sign out of the app.</li>
|
|
99
|
-
* <li><strong>MONOCLOUD_AUTH_RESPONSE_TIMEOUT : </strong>The maximum time in milliseconds to wait for a response.</li>
|
|
100
|
-
* <li><strong>MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES : </strong>Allows dynamic overrides of auth parameters via URL query strings.</li>
|
|
101
|
-
* <li><strong>MONOCLOUD_AUTH_POST_LOGOUT_REDIRECT_URI : </strong>The URL users are sent to after a successful logout.</li>
|
|
102
|
-
* <li><strong>MONOCLOUD_AUTH_USER_INFO : </strong>Determines if user profile data from the UserInfo endpoint should be fetched after authorization code exchange.</li>
|
|
103
|
-
* <li><strong>MONOCLOUD_AUTH_REFETCH_USER_INFO : </strong>If true, re-fetches user information on every request to userinfo endpoint or when calling getTokens()</li>
|
|
104
|
-
* <li><strong>MONOCLOUD_AUTH_ID_TOKEN_SIGNING_ALG : </strong>The expected algorithm for signing ID tokens (e.g., RS256).</li>
|
|
105
|
-
* <li><strong>MONOCLOUD_AUTH_FILTERED_ID_TOKEN_CLAIMS : </strong>A space-separated list of claims to exclude from the session object.</li>
|
|
106
|
-
* </ul>
|
|
107
|
-
*
|
|
108
|
-
* <h4>Routes</h4>
|
|
109
|
-
*
|
|
110
|
-
* <aside>
|
|
111
|
-
* <strong>⚠️ Important: Modifying Default Routes</strong>
|
|
112
|
-
* <p>If you choose to customize any of the default route paths, you must adhere to the following requirements:</p>
|
|
113
|
-
* <ul>
|
|
114
|
-
* <li>
|
|
115
|
-
* <strong>Client-Side Synchronization:</strong> You must also define a corresponding <code>NEXT_PUBLIC_</code> version of the environment variable (e.g., <code>NEXT_PUBLIC_MONOCLOUD_AUTH_CALLBACK_URL</code>). This ensures that client-side components like <code><SignIn /></code>, <code><SignOut /></code>, and the <code>useAuth()</code> hook can correctly identify your custom endpoints.
|
|
116
|
-
* </li>
|
|
117
|
-
* <li>
|
|
118
|
-
* <strong>Dashboard Configuration:</strong> Changing these URLs will alter the endpoints required by MonoCloud. You must update the <strong>Application URLs</strong> section in your MonoCloud Dashboard to match these new paths.
|
|
119
|
-
* </li>
|
|
120
|
-
* </ul>
|
|
121
|
-
* <p><em>Example:</em></p>
|
|
122
|
-
* <code>
|
|
123
|
-
* MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback<br />
|
|
124
|
-
* NEXT_PUBLIC_MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
125
|
-
* </code>
|
|
126
|
-
* <p>In this case, the Redirect URI in your dashboard should be set to: <code>http://localhost:3000/api/custom_callback</code> (assuming local development).</p>
|
|
127
|
-
* </aside>
|
|
128
|
-
*
|
|
129
|
-
* <ul>
|
|
130
|
-
* <li><strong>MONOCLOUD_AUTH_CALLBACK_URL : </strong>The application path where MonoCloud sends the user after authentication.</li>
|
|
131
|
-
* <li><strong>MONOCLOUD_AUTH_SIGNIN_URL : </strong>The internal route path to trigger the sign-in.</li>
|
|
132
|
-
* <li><strong>MONOCLOUD_AUTH_SIGNOUT_URL : </strong>The internal route path to trigger the sign-out.</li>
|
|
133
|
-
* <li><strong>MONOCLOUD_AUTH_USER_INFO_URL : </strong>The route that exposes the current user's profile from userinfo endpoint.</li>
|
|
134
|
-
* </ul>
|
|
135
|
-
*
|
|
136
|
-
* <h4>Session Cookie Settings</h4>
|
|
137
|
-
*
|
|
138
|
-
* <ul>
|
|
139
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_NAME : </strong>The name of the cookie used to store the user session.</li>
|
|
140
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_PATH : </strong>The scope path for the session cookie.</li>
|
|
141
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_DOMAIN : </strong>The domain scope for the session cookie.</li>
|
|
142
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_HTTP_ONLY : </strong>Prevents client-side scripts from accessing the session cookie.</li>
|
|
143
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_SECURE : </strong>Ensures the session cookie is only sent over HTTPS.</li>
|
|
144
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_SAME_SITE : </strong>The SameSite policy for the session cookie (Lax, Strict, or None).</li>
|
|
145
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_PERSISTENT : </strong>If true, the session survives browser restarts.</li>
|
|
146
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_SLIDING : </strong>If true, the session will be a sliding session instead of absolute.</li>
|
|
147
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_DURATION : </strong>The session lifetime in seconds.</li>
|
|
148
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_MAX_DURATION : </strong>The absolute maximum lifetime of a session in seconds.</li>
|
|
149
|
-
* </ul>
|
|
150
|
-
*
|
|
151
|
-
* <h4>State Cookie Settings</h4>
|
|
152
|
-
*
|
|
153
|
-
* <ul>
|
|
154
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_NAME : </strong>The name of the cookie used to store OpenID state/nonce.</li>
|
|
155
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_PATH : </strong>The scope path for the state cookie.</li>
|
|
156
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_DOMAIN : </strong>The domain scope for the state cookie.</li>
|
|
157
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_SECURE : </strong>Ensures the state cookie is only sent over HTTPS</li>
|
|
158
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_SAME_SITE : </strong>The SameSite policy for the state cookie.</li>
|
|
159
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_PERSISTENT : </strong>Whether the state cookie is persistent.</li>
|
|
160
|
-
* </ul>
|
|
161
|
-
*
|
|
162
|
-
* <h4>Caching</h4>
|
|
163
|
-
*
|
|
164
|
-
* <ul>
|
|
165
|
-
* <li><strong>MONOCLOUD_AUTH_JWKS_CACHE_DURATION : </strong>Duration in seconds to cache the JSON Web Key Set.</li>
|
|
166
|
-
* <li><strong>MONOCLOUD_AUTH_METADATA_CACHE_DURATION : </strong>Duration in seconds to cache the OpenID discovery metadata.</li>
|
|
167
|
-
* </ul>
|
|
168
|
-
* </details>
|
|
113
|
+
* When routes are overridden, the Redirect URI configured in the dashboard
|
|
114
|
+
* must reflect the new path. For example, during local development:
|
|
169
115
|
*
|
|
116
|
+
* `http://localhost:3000/api/custom_callback`
|
|
170
117
|
*
|
|
118
|
+
* @category Classes
|
|
171
119
|
*/
|
|
172
120
|
declare class MonoCloudNextClient {
|
|
121
|
+
private readonly _coreClient;
|
|
173
122
|
/**
|
|
174
|
-
*
|
|
123
|
+
* This exposes the framework-agnostic MonoCloud client used internally by the Next.js SDK.
|
|
124
|
+
* Use it if you need access to lower-level functionality not directly exposed by MonoCloudNextClient.
|
|
175
125
|
*
|
|
176
|
-
*
|
|
177
|
-
* You can access this to use low-level methods not directly exposed by the Next.js wrapper.
|
|
178
|
-
*
|
|
179
|
-
* @example Manually destroy session
|
|
180
|
-
* ```typescript
|
|
181
|
-
* // req and res must implement IMonoCloudCookieRequest/Response
|
|
182
|
-
* await monoCloud.coreClient.destroySession(request, response);
|
|
183
|
-
* ```
|
|
126
|
+
* @returns Returns the underlying **Node client** instance.
|
|
184
127
|
*/
|
|
185
|
-
|
|
128
|
+
get coreClient(): MonoCloudCoreClient;
|
|
186
129
|
/**
|
|
187
|
-
*
|
|
130
|
+
* This is intended for advanced scenarios requiring direct control over the authorization or token flow.
|
|
188
131
|
*
|
|
189
|
-
* @
|
|
190
|
-
* // Manually revoke an access token
|
|
191
|
-
* await client.oidcClient.revokeToken(accessToken, 'access_token');
|
|
132
|
+
* @returns Returns the underlying **OIDC client** used for OpenID Connect operations.
|
|
192
133
|
*/
|
|
193
134
|
get oidcClient(): MonoCloudOidcClient;
|
|
194
135
|
/**
|
|
195
|
-
*
|
|
136
|
+
* Creates a new client instance.
|
|
137
|
+
*
|
|
138
|
+
* @param options Optional configuration for initializing the MonoCloud client. If not provided, settings are automatically resolved from environment variables.
|
|
196
139
|
*/
|
|
197
140
|
constructor(options?: MonoCloudOptions$1);
|
|
198
141
|
/**
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* @param options Authentication configuration routes.
|
|
204
|
-
*
|
|
205
|
-
* **Note:** If you are already using `authMiddleware()`, you typically do **not**
|
|
206
|
-
* need this API route handler. This function is intended for applications where
|
|
207
|
-
* middleware cannot be used—such as statically generated (SSG) deployments that still
|
|
208
|
-
* require server-side authentication flows.
|
|
209
|
-
*
|
|
210
|
-
* @example App Router
|
|
211
|
-
*
|
|
212
|
-
* ```typescript
|
|
213
|
-
* // app/api/auth/[...monocloud]/route.ts
|
|
214
|
-
*
|
|
215
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
216
|
-
*
|
|
217
|
-
* export const GET = monoCloud.monoCloudAuth();
|
|
218
|
-
*```
|
|
219
|
-
*
|
|
220
|
-
* @example App Router with Response
|
|
221
|
-
*
|
|
222
|
-
* ```typescript
|
|
223
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
224
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
225
|
-
*
|
|
226
|
-
* export const GET = (req: NextRequest) => {
|
|
227
|
-
* const authHandler = monoCloud.monoCloudAuth();
|
|
228
|
-
*
|
|
229
|
-
* const res = new NextResponse();
|
|
230
|
-
*
|
|
231
|
-
* res.cookies.set("last_auth_requested", `${Date.now()}`);
|
|
232
|
-
*
|
|
233
|
-
* return authHandler(req, res);
|
|
234
|
-
* };
|
|
235
|
-
* ```
|
|
236
|
-
*
|
|
237
|
-
* @example Pages Router
|
|
238
|
-
*
|
|
239
|
-
* ```typescript
|
|
240
|
-
* // pages/api/auth/[...monocloud].ts
|
|
241
|
-
*
|
|
242
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
243
|
-
*
|
|
244
|
-
* export default monoCloud.monoCloudAuth();
|
|
245
|
-
*```
|
|
246
|
-
*
|
|
247
|
-
* @example Page Router with Response
|
|
248
|
-
*
|
|
249
|
-
* ```typescript
|
|
250
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
251
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
252
|
-
*
|
|
253
|
-
* export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
254
|
-
* const authHandler = monoCloud.monoCloudAuth();
|
|
255
|
-
*
|
|
256
|
-
* res.setHeader("last_auth_requested", `${Date.now()}`);
|
|
257
|
-
*
|
|
258
|
-
* return authHandler(req, res);
|
|
259
|
-
* }
|
|
260
|
-
* ```
|
|
261
|
-
*
|
|
142
|
+
* @see {@link monoCloudAuth} for full docs and examples.
|
|
143
|
+
* @param options Optional configuration for the auth handler.
|
|
144
|
+
* @returns Returns a Next.js-compatible handler for App Router route handlers or Pages Router API routes.
|
|
262
145
|
*/
|
|
263
146
|
monoCloudAuth(options?: MonoCloudAuthOptions): MonoCloudAuthHandler;
|
|
264
147
|
/**
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* **Note⚠️ - When using groups to protect a page, 'Access Denied' is rendered by default when the user does not belong to the groups.
|
|
271
|
-
* To display a custom component, pass the `onGroupAccessDenied` parameter.**
|
|
272
|
-
*
|
|
273
|
-
* @param component The App Router server component that protectPage wraps and secures
|
|
274
|
-
* @param options App Router `protectPage()` configuration options
|
|
275
|
-
*
|
|
276
|
-
* @returns A protected page handler.
|
|
277
|
-
*
|
|
278
|
-
* @example
|
|
279
|
-
*
|
|
280
|
-
* ```typescript
|
|
281
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
282
|
-
*
|
|
283
|
-
* export default monoCloud.protectPage(async function Home({ user }) {
|
|
284
|
-
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
285
|
-
* });
|
|
286
|
-
* ```
|
|
287
|
-
*
|
|
288
|
-
* @example App Router with options
|
|
289
|
-
*
|
|
290
|
-
* ```typescript
|
|
291
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
292
|
-
*
|
|
293
|
-
* export default monoCloud.protectPage(
|
|
294
|
-
* async function Home({ user }) {
|
|
295
|
-
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
296
|
-
* },
|
|
297
|
-
* {
|
|
298
|
-
* returnUrl: "/dashboard",
|
|
299
|
-
* groups: ["admin"],
|
|
300
|
-
* }
|
|
301
|
-
* );
|
|
302
|
-
* ```
|
|
148
|
+
* @see {@link protectPage} for full docs and examples.
|
|
149
|
+
* @param component The App Router server component to protect.
|
|
150
|
+
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
151
|
+
* @returns A wrapped page component that enforces authentication before rendering.
|
|
303
152
|
*/
|
|
304
153
|
protectPage(component: ProtectedAppServerComponent, options?: ProtectAppPageOptions): AppRouterPageHandler;
|
|
305
154
|
/**
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
* You should check the props for `groupAccessDenied` boolean value to determine whether the user is
|
|
312
|
-
* allowed to access the page. Alternatively, you can pass `onGroupAccessDenied` parameter to return custom props.**
|
|
313
|
-
*
|
|
314
|
-
* @param options Pages Router `protectPage()` configuration options
|
|
315
|
-
*
|
|
316
|
-
* @typeParam P - The type of parameters accepted by the page handler.
|
|
317
|
-
* @typeParam Q - The type of query parameters parsed from the URL.
|
|
318
|
-
*
|
|
319
|
-
* @returns A protected page handler.
|
|
320
|
-
*
|
|
321
|
-
* @example
|
|
322
|
-
*
|
|
323
|
-
* ```typescript
|
|
324
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
325
|
-
* import { InferGetServerSidePropsType } from "next";
|
|
326
|
-
*
|
|
327
|
-
* export default function Home({
|
|
328
|
-
* user,
|
|
329
|
-
* }: InferGetServerSidePropsType<typeof getServerSideProps>) {
|
|
330
|
-
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
331
|
-
* }
|
|
332
|
-
*
|
|
333
|
-
* export const getServerSideProps = monoCloud.protectPage();
|
|
334
|
-
* ```
|
|
335
|
-
*
|
|
336
|
-
* @example Pages Router with options
|
|
337
|
-
*
|
|
338
|
-
* ```typescript
|
|
339
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
340
|
-
* import { GetServerSidePropsContext, InferGetServerSidePropsType } from "next";
|
|
341
|
-
*
|
|
342
|
-
* export default function Home({
|
|
343
|
-
* user,
|
|
344
|
-
* url,
|
|
345
|
-
* }: InferGetServerSidePropsType<typeof getServerSideProps>) {
|
|
346
|
-
* console.log(url);
|
|
347
|
-
* return <div>Hi {user?.email}. You accessed a protected page.</div>;
|
|
348
|
-
* }
|
|
349
|
-
*
|
|
350
|
-
* export const getServerSideProps = monoCloud.protectPage({
|
|
351
|
-
* returnUrl: "/dashboard",
|
|
352
|
-
* groups: ["admin"],
|
|
353
|
-
* getServerSideProps: async (context: GetServerSidePropsContext) => ({
|
|
354
|
-
* props: { url: context.resolvedUrl },
|
|
355
|
-
* }),
|
|
356
|
-
* });
|
|
357
|
-
* ```
|
|
155
|
+
* @see {@link protectPage} for full docs and examples.
|
|
156
|
+
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
157
|
+
* @typeParam P - Props returned from `getServerSideProps`.
|
|
158
|
+
* @typeParam Q - Query parameters parsed from the URL.
|
|
159
|
+
* @returns A getServerSideProps wrapper that enforces authentication before executing the page logic.
|
|
358
160
|
*/
|
|
359
161
|
protectPage<P extends Record<string, any> = Record<string, any>, Q extends ParsedUrlQuery = ParsedUrlQuery>(options?: ProtectPagePageOptions<P, Q>): ProtectPagePageReturnType<P, Q>;
|
|
360
162
|
private protectAppPage;
|
|
361
163
|
private protectPagePage;
|
|
362
164
|
/**
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
* @param handler The api route handler function to protect
|
|
368
|
-
* @param options App Router `protectApi()` configuration options
|
|
369
|
-
*
|
|
370
|
-
* @returns Protected route handler
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
*
|
|
374
|
-
* ```typescript
|
|
375
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
376
|
-
* import { NextResponse } from "next/server";
|
|
377
|
-
*
|
|
378
|
-
* export const GET = monoCloud.protectApi(async () => {
|
|
379
|
-
* return NextResponse.json({
|
|
380
|
-
* message: "You accessed a protected endpoint",
|
|
381
|
-
* });
|
|
382
|
-
* });
|
|
383
|
-
* ```
|
|
165
|
+
* @see {@link protectApi} for full docs and examples.
|
|
166
|
+
* @param handler The route handler to protect.
|
|
167
|
+
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
168
|
+
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
384
169
|
*/
|
|
385
170
|
protectApi(handler: AppRouterApiHandlerFn, options?: ProtectApiAppOptions): AppRouterApiHandlerFn;
|
|
386
171
|
/**
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
* @param handler The api route handler function to protect
|
|
392
|
-
* @param options Pages Router `protectApi()` configuration options
|
|
393
|
-
*
|
|
394
|
-
* @returns Protected route handler
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
*
|
|
398
|
-
* ```typescript
|
|
399
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
400
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
401
|
-
*
|
|
402
|
-
* export default monoCloud.protectApi(
|
|
403
|
-
* async (req: NextApiRequest, res: NextApiResponse) => {
|
|
404
|
-
* return res.json({
|
|
405
|
-
* message: "You accessed a protected endpoint",
|
|
406
|
-
* });
|
|
407
|
-
* }
|
|
408
|
-
* );
|
|
409
|
-
* ```
|
|
172
|
+
* @see {@link protectApi} for full docs and examples.
|
|
173
|
+
* @param handler - The route handler to protect.
|
|
174
|
+
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
175
|
+
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
410
176
|
*/
|
|
411
177
|
protectApi(handler: NextApiHandler, options?: ProtectApiPageOptions): NextApiHandler;
|
|
412
178
|
private protectAppApi;
|
|
413
179
|
private protectPageApi;
|
|
414
180
|
/**
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
* @
|
|
418
|
-
*
|
|
419
|
-
* @returns A Next.js middleware/proxy function.
|
|
420
|
-
*
|
|
421
|
-
* @example Protect All Routes
|
|
422
|
-
*
|
|
423
|
-
* - Default behavior: protect all routes matched by `config.matcher`
|
|
424
|
-
*
|
|
425
|
-
* ```typescript
|
|
426
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
427
|
-
*
|
|
428
|
-
* export default monoCloud.authMiddleware();
|
|
429
|
-
*
|
|
430
|
-
* export const config = {
|
|
431
|
-
* matcher: [
|
|
432
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
433
|
-
* ],
|
|
434
|
-
* };
|
|
435
|
-
* ```
|
|
436
|
-
*
|
|
437
|
-
* @example Protect Selected Routes
|
|
438
|
-
*
|
|
439
|
-
* - Protect only the routes listed in `protectedRoutes`
|
|
440
|
-
*
|
|
441
|
-
* ```typescript
|
|
442
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
443
|
-
*
|
|
444
|
-
* export default monoCloud.authMiddleware({
|
|
445
|
-
* protectedRoutes: ["/api/admin", "^/api/protected(/.*)?$"],
|
|
446
|
-
* });
|
|
447
|
-
*
|
|
448
|
-
* export const config = {
|
|
449
|
-
* matcher: [
|
|
450
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
451
|
-
* ],
|
|
452
|
-
* };
|
|
453
|
-
*```
|
|
454
|
-
*
|
|
455
|
-
* @example Make All Routes Public
|
|
456
|
-
*
|
|
457
|
-
* - Do not protect any routes; MonoCloud still handles auth endpoints
|
|
458
|
-
*
|
|
459
|
-
* ```typescript
|
|
460
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
461
|
-
*
|
|
462
|
-
* export default monoCloud.authMiddleware({
|
|
463
|
-
* protectedRoutes: [],
|
|
464
|
-
* });
|
|
465
|
-
*
|
|
466
|
-
* export const config = {
|
|
467
|
-
* matcher: [
|
|
468
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
469
|
-
* ],
|
|
470
|
-
* };
|
|
471
|
-
* ```
|
|
472
|
-
*
|
|
473
|
-
* @example Protect Routes Dynamically
|
|
474
|
-
*
|
|
475
|
-
* - Decide at runtime which routes to protect
|
|
476
|
-
*
|
|
477
|
-
* ```typescript
|
|
478
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
479
|
-
*
|
|
480
|
-
* export default monoCloud.authMiddleware({
|
|
481
|
-
* protectedRoutes: (req) => {
|
|
482
|
-
* return req.nextUrl.pathname.startsWith("/api/protected");
|
|
483
|
-
* },
|
|
484
|
-
* });
|
|
485
|
-
*
|
|
486
|
-
* export const config = {
|
|
487
|
-
* matcher: [
|
|
488
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
489
|
-
* ],
|
|
490
|
-
* };
|
|
491
|
-
* ```
|
|
492
|
-
*
|
|
493
|
-
* @example Protect routes based on groups
|
|
494
|
-
*
|
|
495
|
-
* ```typescript
|
|
496
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
497
|
-
*
|
|
498
|
-
* export default monoCloud.authMiddleware({
|
|
499
|
-
* // group names or IDs
|
|
500
|
-
* protectedRoutes: [
|
|
501
|
-
* {
|
|
502
|
-
* groups: ["admin", "editor", "537e7c3d-a442-4b5b-b308-30837aa045a4"],
|
|
503
|
-
* routes: ["/internal", "/api/internal(.*)"],
|
|
504
|
-
* },
|
|
505
|
-
* ],
|
|
506
|
-
* });
|
|
507
|
-
*
|
|
508
|
-
* export const config = {
|
|
509
|
-
* matcher: [
|
|
510
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
511
|
-
* ],
|
|
512
|
-
* };
|
|
513
|
-
* ```
|
|
514
|
-
*
|
|
181
|
+
* @see {@link authMiddleware} for full docs and examples.
|
|
182
|
+
* @param options Optional configuration that controls how authentication is enforced (for example, redirect behavior, route matching, or custom handling of unauthenticated requests).
|
|
183
|
+
* @returns Returns a Next.js middleware result (`NextResponse`, redirect, or `undefined` to continue processing).
|
|
515
184
|
*/
|
|
516
185
|
authMiddleware(options?: MonoCloudMiddlewareOptions): NextMiddleware | NextProxy;
|
|
517
186
|
/**
|
|
518
|
-
*
|
|
519
|
-
*
|
|
520
|
-
* @param
|
|
521
|
-
* @
|
|
522
|
-
*
|
|
523
|
-
* @returns A promise resolving to a Next.js middleware result or a Next.js middleware result.
|
|
524
|
-
*
|
|
525
|
-
* @example Nest Custom Middleware
|
|
526
|
-
*
|
|
527
|
-
* - Use your own middleware wrapper and call MonoCloud only for specific routes
|
|
528
|
-
*
|
|
529
|
-
* ```typescript
|
|
530
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
531
|
-
* import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
|
|
532
|
-
*
|
|
533
|
-
* export default function customMiddleware(req: NextRequest, evt: NextFetchEvent) {
|
|
534
|
-
* if (req.nextUrl.pathname.startsWith("/api/protected")) {
|
|
535
|
-
* return monoCloud.authMiddleware(req, evt);
|
|
536
|
-
* }
|
|
537
|
-
*
|
|
538
|
-
* return NextResponse.next();
|
|
539
|
-
* }
|
|
540
|
-
*
|
|
541
|
-
* export const config = {
|
|
542
|
-
* matcher: [
|
|
543
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
544
|
-
* ],
|
|
545
|
-
* };
|
|
546
|
-
* ```
|
|
547
|
-
*
|
|
187
|
+
* @see {@link authMiddleware} for full docs and examples.
|
|
188
|
+
* @param request Incoming Next.js middleware request used to resolve authentication state.
|
|
189
|
+
* @param event Next.js middleware event providing lifecycle hooks such as `waitUntil`.
|
|
190
|
+
* @returns Returns a Next.js middleware result (`NextResponse`, redirect, or `undefined` to continue processing).
|
|
548
191
|
*/
|
|
549
192
|
authMiddleware(request: NextRequest, event: NextFetchEvent): Promise<NextMiddlewareResult> | NextMiddlewareResult;
|
|
550
193
|
private authMiddlewareHandler;
|
|
551
194
|
private handleAuthRoutes;
|
|
552
195
|
/**
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
* Retrieves the session object for the currently authenticated user on the server.
|
|
556
|
-
*
|
|
557
|
-
* **Use Case:**
|
|
558
|
-
* - App Router Server Components (RSC).
|
|
559
|
-
* - Server Actions
|
|
560
|
-
* - Route Handlers (App Router only).
|
|
561
|
-
* - Middleware (App Router and Pages Router).
|
|
562
|
-
*
|
|
563
|
-
* *Note: If the session cannot be resolved or an underlying error occurs, the promise rejects with an error.*
|
|
564
|
-
*
|
|
565
|
-
* @returns `MonoCloudSession` if found, or `undefined`.
|
|
566
|
-
*
|
|
567
|
-
* @example Middleware/Proxy
|
|
568
|
-
*
|
|
569
|
-
* ```typescript
|
|
570
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
571
|
-
* import { NextResponse } from "next/server";
|
|
572
|
-
*
|
|
573
|
-
* export default async function middleware() {
|
|
574
|
-
* const session = await monoCloud.getSession();
|
|
575
|
-
*
|
|
576
|
-
* if (!session) {
|
|
577
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
578
|
-
* }
|
|
579
|
-
*
|
|
580
|
-
* return NextResponse.next();
|
|
581
|
-
* }
|
|
582
|
-
*
|
|
583
|
-
* export const config = {
|
|
584
|
-
* matcher: [
|
|
585
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
586
|
-
* ],
|
|
587
|
-
* };
|
|
588
|
-
* ```
|
|
589
|
-
*
|
|
590
|
-
* @example App Router API Handler
|
|
591
|
-
*
|
|
592
|
-
* ```typescript
|
|
593
|
-
* import { NextResponse } from "next/server";
|
|
594
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
595
|
-
*
|
|
596
|
-
* export const GET = async () => {
|
|
597
|
-
* const session = await monoCloud.getSession();
|
|
598
|
-
*
|
|
599
|
-
* return NextResponse.json({ name: session?.user.name });
|
|
600
|
-
* };
|
|
601
|
-
* ```
|
|
602
|
-
*
|
|
603
|
-
* @example React Server Components
|
|
604
|
-
*
|
|
605
|
-
* ```tsx
|
|
606
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
607
|
-
*
|
|
608
|
-
* export default async function Home() {
|
|
609
|
-
* const session = await monoCloud.getSession();
|
|
610
|
-
*
|
|
611
|
-
* return <div>{session?.user.name}</div>;
|
|
612
|
-
* }
|
|
613
|
-
* ```
|
|
614
|
-
*
|
|
615
|
-
* @example Server Action
|
|
616
|
-
*
|
|
617
|
-
* ```typescript
|
|
618
|
-
* "use server";
|
|
619
|
-
*
|
|
620
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
621
|
-
*
|
|
622
|
-
* export async function getUserAction() {
|
|
623
|
-
* const session = await monoCloud.getSession();
|
|
624
|
-
*
|
|
625
|
-
* return { name: session?.user.name };
|
|
626
|
-
* }
|
|
627
|
-
* ```
|
|
628
|
-
*
|
|
196
|
+
* @see {@link getSession} for full docs and examples.
|
|
197
|
+
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
629
198
|
*/
|
|
630
199
|
getSession(): Promise<MonoCloudSession$1 | undefined>;
|
|
631
200
|
/**
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
* **Use Case:**
|
|
637
|
-
* - Middleware (for both App and Pages Router).
|
|
638
|
-
* - App Router Route Handlers (API routes).
|
|
639
|
-
* - Edge functions.
|
|
640
|
-
*
|
|
641
|
-
* *Note: If the session cannot be resolved or an underlying error occurs, the promise rejects with an error.*
|
|
642
|
-
*
|
|
643
|
-
* @param req NextRequest
|
|
644
|
-
* @param res An optional `NextResponse` instance. Pass this if you have already initialized a response; otherwise, omit this parameter.
|
|
645
|
-
*
|
|
646
|
-
* @returns `MonoCloudSession` if found, or `undefined`.
|
|
647
|
-
*
|
|
648
|
-
* @example Middleware/Proxy
|
|
649
|
-
*
|
|
650
|
-
* ```typescript
|
|
651
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
652
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
653
|
-
*
|
|
654
|
-
* export default async function middleware(req: NextRequest) {
|
|
655
|
-
* const session = await monoCloud.getSession(req);
|
|
656
|
-
*
|
|
657
|
-
* if (!session) {
|
|
658
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
659
|
-
* }
|
|
660
|
-
*
|
|
661
|
-
* return NextResponse.next();
|
|
662
|
-
* }
|
|
663
|
-
*
|
|
664
|
-
* export const config = {
|
|
665
|
-
* matcher: [
|
|
666
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
667
|
-
* ],
|
|
668
|
-
* };
|
|
669
|
-
* ```
|
|
670
|
-
*
|
|
671
|
-
* @example Middleware/Proxy (Custom Response)
|
|
672
|
-
*
|
|
673
|
-
* ```typescript
|
|
674
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
675
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
676
|
-
*
|
|
677
|
-
* export default async function middleware(req: NextRequest) {
|
|
678
|
-
* const res = NextResponse.next();
|
|
679
|
-
*
|
|
680
|
-
* const session = await monoCloud.getSession(req, res);
|
|
681
|
-
*
|
|
682
|
-
* if (!session) {
|
|
683
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
684
|
-
* }
|
|
685
|
-
*
|
|
686
|
-
* res.headers.set("x-auth-status", "active");
|
|
687
|
-
*
|
|
688
|
-
* return res;
|
|
689
|
-
* }
|
|
690
|
-
*
|
|
691
|
-
* export const config = {
|
|
692
|
-
* matcher: [
|
|
693
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
694
|
-
* ],
|
|
695
|
-
* };
|
|
696
|
-
* ```
|
|
697
|
-
*
|
|
698
|
-
* @example API Handler
|
|
699
|
-
*
|
|
700
|
-
* ```typescript
|
|
701
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
702
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
703
|
-
*
|
|
704
|
-
* export const GET = async (req: NextRequest) => {
|
|
705
|
-
* const session = await monoCloud.getSession(req);
|
|
706
|
-
*
|
|
707
|
-
* return NextResponse.json({ name: session?.user.name });
|
|
708
|
-
* };
|
|
709
|
-
* ```
|
|
710
|
-
*
|
|
711
|
-
* @example API Handler with NextResponse
|
|
712
|
-
*
|
|
713
|
-
* ```typescript
|
|
714
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
715
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
716
|
-
*
|
|
717
|
-
* export const GET = async (req: NextRequest) => {
|
|
718
|
-
* const res = new NextResponse("YOUR CUSTOM RESPONSE");
|
|
719
|
-
*
|
|
720
|
-
* const session = await monoCloud.getSession(req, res);
|
|
721
|
-
*
|
|
722
|
-
* if (session?.user) {
|
|
723
|
-
* res.cookies.set("something", "important");
|
|
724
|
-
* }
|
|
725
|
-
*
|
|
726
|
-
* return res;
|
|
727
|
-
* };
|
|
728
|
-
* ```
|
|
201
|
+
* @see {@link getSession} for full docs and examples.
|
|
202
|
+
* @param req Incoming request used to read authentication cookies and headers to resolve the current user's session.
|
|
203
|
+
* @param res Optional response to update if session resolution requires refreshed authentication cookies or headers.
|
|
204
|
+
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
729
205
|
*/
|
|
730
206
|
getSession(req: NextRequest | Request, res?: NextResponse | Response): Promise<MonoCloudSession$1 | undefined>;
|
|
731
207
|
/**
|
|
732
|
-
*
|
|
733
|
-
*
|
|
734
|
-
*
|
|
735
|
-
*
|
|
736
|
-
* *Note: If the session cannot be resolved or an underlying error occurs, the promise rejects with an error.*
|
|
737
|
-
*
|
|
738
|
-
* @param req NextApiRequest
|
|
739
|
-
* @param res NextApiResponse
|
|
740
|
-
*
|
|
741
|
-
* @returns `MonoCloudSession` if found, or `undefined`.
|
|
742
|
-
*
|
|
743
|
-
* @example API Handler
|
|
744
|
-
*
|
|
745
|
-
* ```typescript
|
|
746
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
747
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
748
|
-
*
|
|
749
|
-
* type Data = {
|
|
750
|
-
* name?: string;
|
|
751
|
-
* };
|
|
752
|
-
*
|
|
753
|
-
* export default async function handler(
|
|
754
|
-
* req: NextApiRequest,
|
|
755
|
-
* res: NextApiResponse<Data>
|
|
756
|
-
* ) {
|
|
757
|
-
* const session = await monoCloud.getSession(req, res);
|
|
758
|
-
*
|
|
759
|
-
* res.status(200).json({ name: session?.user.name });
|
|
760
|
-
* }
|
|
761
|
-
* ```
|
|
762
|
-
*
|
|
763
|
-
* @example SSR Component
|
|
764
|
-
*
|
|
765
|
-
* ```typescript
|
|
766
|
-
* import { monoCloud } from "@/monocloud";
|
|
767
|
-
* import type {
|
|
768
|
-
* GetServerSideProps,
|
|
769
|
-
* GetServerSidePropsContext,
|
|
770
|
-
* InferGetServerSidePropsType,
|
|
771
|
-
* } from "next";
|
|
772
|
-
*
|
|
773
|
-
* type HomeProps = InferGetServerSidePropsType<typeof getServerSideProps>;
|
|
774
|
-
*
|
|
775
|
-
* export default function Home({ session }: HomeProps) {
|
|
776
|
-
* return <pre>Session: {JSON.stringify(session, null, 2)}</pre>;
|
|
777
|
-
* }
|
|
778
|
-
*
|
|
779
|
-
* export const getServerSideProps = (async (
|
|
780
|
-
* context: GetServerSidePropsContext,
|
|
781
|
-
* ) => {
|
|
782
|
-
* const session = await monoCloud.getSession(
|
|
783
|
-
* context.req,
|
|
784
|
-
* context.res,
|
|
785
|
-
* );
|
|
786
|
-
*
|
|
787
|
-
* return {
|
|
788
|
-
* props: {
|
|
789
|
-
* session: session ?? null,
|
|
790
|
-
* },
|
|
791
|
-
* };
|
|
792
|
-
* }) satisfies GetServerSideProps;
|
|
793
|
-
* ```
|
|
208
|
+
* @see {@link getSession} for full docs and examples.
|
|
209
|
+
* @param req Incoming Node.js request used to read authentication cookies and resolve the current user's session.
|
|
210
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
211
|
+
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
794
212
|
*/
|
|
795
213
|
getSession(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>): Promise<MonoCloudSession$1 | undefined>;
|
|
796
214
|
/**
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
* **Use Case:**
|
|
802
|
-
* - App Router Server Components (RSC).
|
|
803
|
-
* - Server Actions
|
|
804
|
-
* - Route Handlers (App Router only).
|
|
805
|
-
* - Middleware (App Router and Pages Router).
|
|
806
|
-
*
|
|
807
|
-
* @param options Configuration options for token retrieval.
|
|
808
|
-
*
|
|
809
|
-
* @returns
|
|
810
|
-
*
|
|
811
|
-
* @throws {@link MonoCloudValidationError} If session is not found
|
|
812
|
-
*
|
|
813
|
-
* @example Middleware/Proxy
|
|
814
|
-
*
|
|
815
|
-
* ```typescript
|
|
816
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
817
|
-
* import { NextResponse } from "next/server";
|
|
818
|
-
*
|
|
819
|
-
* export default async function middleware() {
|
|
820
|
-
* const tokens = await monoCloud.getTokens();
|
|
821
|
-
*
|
|
822
|
-
* if (tokens.isExpired) {
|
|
823
|
-
* return new NextResponse("Tokens expired", { status: 401 });
|
|
824
|
-
* }
|
|
825
|
-
*
|
|
826
|
-
* return NextResponse.next();
|
|
827
|
-
* }
|
|
828
|
-
*
|
|
829
|
-
* export const config = {
|
|
830
|
-
* matcher: [
|
|
831
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
832
|
-
* ],
|
|
833
|
-
* };
|
|
834
|
-
* ```
|
|
835
|
-
*
|
|
836
|
-
* @example App Router API Handler
|
|
837
|
-
*
|
|
838
|
-
* ```typescript
|
|
839
|
-
* import { NextResponse } from "next/server";
|
|
840
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
841
|
-
*
|
|
842
|
-
* export const GET = async () => {
|
|
843
|
-
* const tokens = await monoCloud.getTokens();
|
|
844
|
-
*
|
|
845
|
-
* return NextResponse.json({ expired: tokens.isExpired });
|
|
846
|
-
* };
|
|
847
|
-
* ```
|
|
848
|
-
*
|
|
849
|
-
* @example React Server Components
|
|
850
|
-
*
|
|
851
|
-
* ```tsx
|
|
852
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
853
|
-
*
|
|
854
|
-
* export default async function Home() {
|
|
855
|
-
* const tokens = await monoCloud.getTokens();
|
|
856
|
-
*
|
|
857
|
-
* return <div>Expired: {tokens.isExpired.toString()}</div>;
|
|
858
|
-
* }
|
|
859
|
-
* ```
|
|
860
|
-
*
|
|
861
|
-
* @example Server Action
|
|
862
|
-
*
|
|
863
|
-
* ```typescript
|
|
864
|
-
* "use server";
|
|
865
|
-
*
|
|
866
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
867
|
-
*
|
|
868
|
-
* export async function getExpiredAction() {
|
|
869
|
-
* const tokens = await monoCloud.getTokens();
|
|
870
|
-
*
|
|
871
|
-
* return { expired: tokens.isExpired };
|
|
872
|
-
* }
|
|
873
|
-
* ```
|
|
874
|
-
*
|
|
875
|
-
* @example Refresh Default Token
|
|
876
|
-
*
|
|
877
|
-
* The default token is an access token with scopes set through `MONOCLOUD_AUTH_SCOPES` or
|
|
878
|
-
* `options.defaultAuthParams.scopes`, and resources set through `MONOCLOUD_AUTH_RESOURCE` or
|
|
879
|
-
* `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without resource and scopes parameters.
|
|
880
|
-
*
|
|
881
|
-
* ```typescript
|
|
882
|
-
* import { NextResponse } from "next/server";
|
|
883
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
884
|
-
*
|
|
885
|
-
* export const GET = async () => {
|
|
886
|
-
* // Although the token refreshes automatically upon expiration, we are manually refreshing it here.
|
|
887
|
-
* const tokens = await monoCloud.getTokens({ forceRefresh: true });
|
|
888
|
-
*
|
|
889
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
890
|
-
* };
|
|
891
|
-
* ```
|
|
892
|
-
*
|
|
893
|
-
* @example Request new access token for resource(s)
|
|
894
|
-
*
|
|
895
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
896
|
-
*
|
|
897
|
-
* The following example shows how to request a new token scoped to two non-exclusive resources.
|
|
898
|
-
*
|
|
899
|
-
* ```typescript
|
|
900
|
-
* import { NextResponse } from "next/server";
|
|
901
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
902
|
-
*
|
|
903
|
-
* export const GET = async () => {
|
|
904
|
-
* const tokens = await monoCloud.getTokens({
|
|
905
|
-
* resource: "https://first.example.com https://second.example.com",
|
|
906
|
-
* scopes: "read:first read:second shared",
|
|
907
|
-
* });
|
|
908
|
-
*
|
|
909
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
910
|
-
* };
|
|
911
|
-
* ```
|
|
912
|
-
*
|
|
913
|
-
* @example Request an exclusive token
|
|
914
|
-
*
|
|
915
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
916
|
-
*
|
|
917
|
-
* ```typescript
|
|
918
|
-
* import { NextResponse } from "next/server";
|
|
919
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
920
|
-
*
|
|
921
|
-
* export const GET = async () => {
|
|
922
|
-
* const tokens = await monoCloud.getTokens({
|
|
923
|
-
* resource: "https://exclusive.example.com",
|
|
924
|
-
* scopes: "read:exclusive shared",
|
|
925
|
-
* });
|
|
926
|
-
*
|
|
927
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
928
|
-
* };
|
|
929
|
-
* ```
|
|
215
|
+
* @see {@link getTokens} for full docs and examples.
|
|
216
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
217
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
218
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
930
219
|
*/
|
|
931
220
|
getTokens(options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
932
221
|
/**
|
|
933
|
-
*
|
|
934
|
-
*
|
|
935
|
-
*
|
|
936
|
-
*
|
|
937
|
-
*
|
|
938
|
-
* - Middleware (for both App and Pages Router).
|
|
939
|
-
* - App Router Route Handlers (API routes).
|
|
940
|
-
* - Edge functions.
|
|
941
|
-
*
|
|
942
|
-
* @param req NextRequest
|
|
943
|
-
* @param options Configuration options for token retrieval.
|
|
944
|
-
*
|
|
945
|
-
* @returns
|
|
946
|
-
*
|
|
947
|
-
* @throws {@link MonoCloudValidationError} If session is not found
|
|
948
|
-
*
|
|
949
|
-
* @example Middleware/Proxy
|
|
950
|
-
*
|
|
951
|
-
* ```typescript
|
|
952
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
953
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
954
|
-
*
|
|
955
|
-
* export default async function middleware(req: NextRequest) {
|
|
956
|
-
* const tokens = await monoCloud.getTokens(req);
|
|
957
|
-
*
|
|
958
|
-
* if (tokens.isExpired) {
|
|
959
|
-
* return new NextResponse("Tokens expired", { status: 401 });
|
|
960
|
-
* }
|
|
961
|
-
*
|
|
962
|
-
* return NextResponse.next();
|
|
963
|
-
* }
|
|
964
|
-
*
|
|
965
|
-
* export const config = {
|
|
966
|
-
* matcher: [
|
|
967
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
968
|
-
* ],
|
|
969
|
-
* };
|
|
970
|
-
* ```
|
|
971
|
-
*
|
|
972
|
-
* @example App Router API Handler
|
|
973
|
-
*
|
|
974
|
-
* ```typescript
|
|
975
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
976
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
977
|
-
*
|
|
978
|
-
* export const GET = async (req: NextRequest) => {
|
|
979
|
-
* const tokens = await monoCloud.getTokens(req);
|
|
980
|
-
*
|
|
981
|
-
* return NextResponse.json({ expired: tokens?.isExpired });
|
|
982
|
-
* };
|
|
983
|
-
* ```
|
|
984
|
-
*
|
|
985
|
-
* @example Refresh Default Token
|
|
986
|
-
*
|
|
987
|
-
* The default token is an access token with scopes set through `MONOCLOUD_AUTH_SCOPES` or
|
|
988
|
-
* `options.defaultAuthParams.scopes`, and resources set through `MONOCLOUD_AUTH_RESOURCE` or
|
|
989
|
-
* `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without parameters.
|
|
990
|
-
*
|
|
991
|
-
* ```typescript
|
|
992
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
993
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
994
|
-
*
|
|
995
|
-
* export const GET = async (req: NextRequest) => {
|
|
996
|
-
* // Although the token refreshes automatically upon expiration, we are manually refreshing it here.
|
|
997
|
-
* const tokens = await monoCloud.getTokens(req, { forceRefresh: true });
|
|
998
|
-
*
|
|
999
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
1000
|
-
* };
|
|
1001
|
-
* ```
|
|
1002
|
-
*
|
|
1003
|
-
* @example Request new access token for resource(s)
|
|
1004
|
-
*
|
|
1005
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
1006
|
-
*
|
|
1007
|
-
* The following example shows how to request a new token scoped to two non-exclusive resources.
|
|
1008
|
-
*
|
|
1009
|
-
* ```typescript
|
|
1010
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1011
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1012
|
-
*
|
|
1013
|
-
* export const GET = async (req: NextRequest) => {
|
|
1014
|
-
* const tokens = await monoCloud.getTokens(req, {
|
|
1015
|
-
* resource: "https://first.example.com https://second.example.com",
|
|
1016
|
-
* scopes: "read:first read:second shared",
|
|
1017
|
-
* });
|
|
1018
|
-
*
|
|
1019
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
1020
|
-
* };
|
|
1021
|
-
* ```
|
|
1022
|
-
*
|
|
1023
|
-
* @example Request an exclusive token
|
|
1024
|
-
*
|
|
1025
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
1026
|
-
*
|
|
1027
|
-
* ```typescript
|
|
1028
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1029
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1030
|
-
*
|
|
1031
|
-
* export const GET = async (req: NextRequest) => {
|
|
1032
|
-
* const tokens = await monoCloud.getTokens(req, {
|
|
1033
|
-
* resource: "https://exclusive.example.com",
|
|
1034
|
-
* scopes: "read:exclusive shared",
|
|
1035
|
-
* });
|
|
1036
|
-
*
|
|
1037
|
-
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
1038
|
-
* };
|
|
1039
|
-
* ```
|
|
222
|
+
* @see {@link getTokens} for full docs and examples.
|
|
223
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
224
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
225
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
226
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
1040
227
|
*/
|
|
1041
228
|
getTokens(req: NextRequest | Request, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
1042
229
|
/**
|
|
1043
|
-
*
|
|
1044
|
-
*
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1049
|
-
* - App Router Route Handlers (when a NextResponse is already initialized).
|
|
1050
|
-
*
|
|
1051
|
-
* @param req NextRequest
|
|
1052
|
-
* @param res An optional `NextResponse` instance. Pass this if you have already initialized a response and want token updates (e.g., refreshing) to be applied to it.
|
|
1053
|
-
* @param options Configuration options for token retrieval.
|
|
1054
|
-
*
|
|
1055
|
-
* @returns
|
|
1056
|
-
*
|
|
1057
|
-
* @throws {@link MonoCloudValidationError} If session is not found
|
|
1058
|
-
*
|
|
1059
|
-
* @example Middleware/Proxy
|
|
1060
|
-
*
|
|
1061
|
-
*```typescript
|
|
1062
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1063
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1064
|
-
*
|
|
1065
|
-
* export default async function middleware(req: NextRequest) {
|
|
1066
|
-
* const res = NextResponse.next();
|
|
1067
|
-
*
|
|
1068
|
-
* const tokens = await monoCloud.getTokens(req, res);
|
|
1069
|
-
*
|
|
1070
|
-
* res.headers.set("x-tokens-expired", tokens.isExpired.toString());
|
|
1071
|
-
*
|
|
1072
|
-
* return res;
|
|
1073
|
-
* }
|
|
1074
|
-
*
|
|
1075
|
-
* export const config = {
|
|
1076
|
-
* matcher: [
|
|
1077
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
1078
|
-
* ],
|
|
1079
|
-
* };
|
|
1080
|
-
* ```
|
|
1081
|
-
*
|
|
1082
|
-
* @example API Handler with NextResponse
|
|
1083
|
-
*
|
|
1084
|
-
* ```typescript
|
|
1085
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1086
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1087
|
-
*
|
|
1088
|
-
* export const GET = async (req: NextRequest) => {
|
|
1089
|
-
* const res = new NextResponse("Custom Body");
|
|
1090
|
-
*
|
|
1091
|
-
* const tokens = await monoCloud.getTokens(req, res);
|
|
1092
|
-
*
|
|
1093
|
-
* if (!tokens.isExpired) {
|
|
1094
|
-
* res.headers.set("x-auth-status", "active");
|
|
1095
|
-
* }
|
|
1096
|
-
*
|
|
1097
|
-
* return res;
|
|
1098
|
-
* };
|
|
1099
|
-
* ```
|
|
1100
|
-
*
|
|
1101
|
-
* @example Refresh Default Token
|
|
1102
|
-
*
|
|
1103
|
-
* The default token is an access token with scopes set through `MONOCLOUD_AUTH_SCOPES` or
|
|
1104
|
-
* `options.defaultAuthParams.scopes`, and resources set through `MONOCLOUD_AUTH_RESOURCE` or
|
|
1105
|
-
* `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without parameters.
|
|
1106
|
-
*
|
|
1107
|
-
* ```typescript
|
|
1108
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1109
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1110
|
-
*
|
|
1111
|
-
* export const GET = async (req: NextRequest) => {
|
|
1112
|
-
* const res = new NextResponse("Custom Body");
|
|
1113
|
-
*
|
|
1114
|
-
* // Although the token refreshes automatically upon expiration, we are manually refreshing it here.
|
|
1115
|
-
* const tokens = await monoCloud.getTokens(req, res, { forceRefresh: true });
|
|
1116
|
-
*
|
|
1117
|
-
* if (!tokens.isExpired) {
|
|
1118
|
-
* res.headers.set("x-auth-status", "active");
|
|
1119
|
-
* }
|
|
1120
|
-
*
|
|
1121
|
-
* return res;
|
|
1122
|
-
* };
|
|
1123
|
-
* ```
|
|
1124
|
-
*
|
|
1125
|
-
* @example Request new access token for resource(s)
|
|
1126
|
-
*
|
|
1127
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
1128
|
-
*
|
|
1129
|
-
* The following example shows how to request a new token scoped to two non-exclusive resources.
|
|
1130
|
-
*
|
|
1131
|
-
* ```typescript
|
|
1132
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1133
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1134
|
-
*
|
|
1135
|
-
* export const GET = async (req: NextRequest) => {
|
|
1136
|
-
* const res = new NextResponse("Custom Body");
|
|
1137
|
-
*
|
|
1138
|
-
* const tokens = await monoCloud.getTokens(req, res, {
|
|
1139
|
-
* resource: "https://first.example.com https://second.example.com",
|
|
1140
|
-
* scopes: "read:first read:second shared",
|
|
1141
|
-
* });
|
|
1142
|
-
*
|
|
1143
|
-
* if (!tokens.isExpired) {
|
|
1144
|
-
* res.headers.set("x-auth-status", "active");
|
|
1145
|
-
* }
|
|
1146
|
-
*
|
|
1147
|
-
* return res;
|
|
1148
|
-
* };
|
|
1149
|
-
* ```
|
|
1150
|
-
*
|
|
1151
|
-
* @example Request an exclusive token
|
|
1152
|
-
*
|
|
1153
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
1154
|
-
*
|
|
1155
|
-
* ```typescript
|
|
1156
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1157
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1158
|
-
*
|
|
1159
|
-
* export const GET = async (req: NextRequest) => {
|
|
1160
|
-
* const res = new NextResponse("Custom Body");
|
|
1161
|
-
*
|
|
1162
|
-
* const tokens = await monoCloud.getTokens(req, res, {
|
|
1163
|
-
* resource: "https://exclusive.example.com",
|
|
1164
|
-
* scopes: "read:exclusive shared",
|
|
1165
|
-
* });
|
|
1166
|
-
*
|
|
1167
|
-
* if (!tokens.isExpired) {
|
|
1168
|
-
* res.headers.set("x-auth-status", "active");
|
|
1169
|
-
* }
|
|
1170
|
-
*
|
|
1171
|
-
* return res;
|
|
1172
|
-
* };
|
|
1173
|
-
* ```
|
|
230
|
+
* @see {@link getTokens} for full docs and examples.
|
|
231
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
232
|
+
* @param res Existing response to update with refreshed authentication cookies or headers.
|
|
233
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
234
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
235
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
1174
236
|
*/
|
|
1175
237
|
getTokens(req: NextRequest | Request, res: NextResponse | Response, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
1176
238
|
/**
|
|
1177
|
-
*
|
|
1178
|
-
*
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
* @
|
|
1182
|
-
* @
|
|
1183
|
-
* @param options Configuration options for token retrieval.
|
|
1184
|
-
*
|
|
1185
|
-
* @returns
|
|
1186
|
-
*
|
|
1187
|
-
* @throws {@link MonoCloudValidationError} If session is not found
|
|
1188
|
-
*
|
|
1189
|
-
* @example API Route
|
|
1190
|
-
*
|
|
1191
|
-
* ```typescript
|
|
1192
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1193
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
1194
|
-
*
|
|
1195
|
-
* export default async function handler(
|
|
1196
|
-
* req: NextApiRequest,
|
|
1197
|
-
* res: NextApiResponse
|
|
1198
|
-
* ) {
|
|
1199
|
-
* const tokens = await monoCloud.getTokens(req, res);
|
|
1200
|
-
*
|
|
1201
|
-
* res.status(200).json({ accessToken: tokens?.accessToken });
|
|
1202
|
-
* }
|
|
1203
|
-
* ```
|
|
1204
|
-
*
|
|
1205
|
-
* @example SSR Component
|
|
1206
|
-
*
|
|
1207
|
-
* ```typescript
|
|
1208
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1209
|
-
* import type { GetServerSideProps, InferGetServerSidePropsType } from "next";
|
|
1210
|
-
*
|
|
1211
|
-
* type HomeProps = InferGetServerSidePropsType<typeof getServerSideProps>;
|
|
1212
|
-
*
|
|
1213
|
-
* export default function Home({ tokens }: HomeProps) {
|
|
1214
|
-
* return <pre>Tokens: {JSON.stringify(tokens, null, 2)}</pre>;
|
|
1215
|
-
* }
|
|
1216
|
-
*
|
|
1217
|
-
* export const getServerSideProps: GetServerSideProps = async (context) => {
|
|
1218
|
-
* const tokens = await monoCloud.getTokens(context.req, context.res);
|
|
1219
|
-
*
|
|
1220
|
-
* return {
|
|
1221
|
-
* props: {
|
|
1222
|
-
* tokens: tokens ?? null,
|
|
1223
|
-
* },
|
|
1224
|
-
* };
|
|
1225
|
-
* };
|
|
1226
|
-
* ```
|
|
1227
|
-
*
|
|
1228
|
-
* @example Refresh Default Token
|
|
1229
|
-
*
|
|
1230
|
-
* The default token is an access token with scopes set through `MONOCLOUD_AUTH_SCOPES` or
|
|
1231
|
-
* `options.defaultAuthParams.scopes`, and resources set through `MONOCLOUD_AUTH_RESOURCE` or
|
|
1232
|
-
* `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without parameters.
|
|
1233
|
-
*
|
|
1234
|
-
* ```typescript
|
|
1235
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1236
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
1237
|
-
*
|
|
1238
|
-
* export default async function handler(
|
|
1239
|
-
* req: NextApiRequest,
|
|
1240
|
-
* res: NextApiResponse
|
|
1241
|
-
* ) {
|
|
1242
|
-
* // Although the token refreshes automatically upon expiration, we are manually refreshing it here.
|
|
1243
|
-
* const tokens = await monoCloud.getTokens(req, res, { forceRefresh: true });
|
|
1244
|
-
*
|
|
1245
|
-
* res.status(200).json({ accessToken: tokens?.accessToken });
|
|
1246
|
-
* }
|
|
1247
|
-
* ```
|
|
1248
|
-
*
|
|
1249
|
-
* @example Request new access token for resource(s)
|
|
1250
|
-
*
|
|
1251
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
1252
|
-
*
|
|
1253
|
-
* The following example shows how to request a new token scoped to two non-exclusive resources.
|
|
1254
|
-
*
|
|
1255
|
-
* ```typescript
|
|
1256
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1257
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
1258
|
-
*
|
|
1259
|
-
* export default async function handler(
|
|
1260
|
-
* req: NextApiRequest,
|
|
1261
|
-
* res: NextApiResponse
|
|
1262
|
-
* ) {
|
|
1263
|
-
* const tokens = await monoCloud.getTokens(req, res, {
|
|
1264
|
-
* resource: "https://first.example.com https://second.example.com",
|
|
1265
|
-
* scopes: "read:first read:second shared",
|
|
1266
|
-
* });
|
|
1267
|
-
*
|
|
1268
|
-
* res.status(200).json({ accessToken: tokens?.accessToken });
|
|
1269
|
-
* }
|
|
1270
|
-
* ```
|
|
1271
|
-
*
|
|
1272
|
-
* @example Request an exclusive token
|
|
1273
|
-
*
|
|
1274
|
-
* **Note: Ensure that the resources and scopes are included in the initial authorization flow**
|
|
1275
|
-
*
|
|
1276
|
-
* ```typescript
|
|
1277
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1278
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
1279
|
-
*
|
|
1280
|
-
* export default async function handler(
|
|
1281
|
-
* req: NextApiRequest,
|
|
1282
|
-
* res: NextApiResponse
|
|
1283
|
-
* ) {
|
|
1284
|
-
* const tokens = await monoCloud.getTokens(req, res, {
|
|
1285
|
-
* resource: "https://exclusive.example.com",
|
|
1286
|
-
* scopes: "read:exclusive shared",
|
|
1287
|
-
* });
|
|
1288
|
-
*
|
|
1289
|
-
* res.status(200).json({ accessToken: tokens?.accessToken });
|
|
1290
|
-
* }
|
|
1291
|
-
* ```
|
|
239
|
+
* @see {@link getTokens} for full docs and examples.
|
|
240
|
+
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
241
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
242
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
243
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
244
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
1292
245
|
*/
|
|
1293
246
|
getTokens(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
1294
247
|
/**
|
|
1295
|
-
*
|
|
1296
|
-
*
|
|
1297
|
-
* Checks if the current user is authenticated.
|
|
1298
|
-
*
|
|
1299
|
-
* **Use Case:**
|
|
1300
|
-
* - App Router Server Components (RSC).
|
|
1301
|
-
* - Server Actions
|
|
1302
|
-
* - Route Handlers (App Router only).
|
|
1303
|
-
* - Middleware (App Router and Pages Router).
|
|
1304
|
-
*
|
|
1305
|
-
* @returns `true` if the user is authenticated, otherwise `false`.
|
|
1306
|
-
*
|
|
1307
|
-
* @example Middleware/Proxy
|
|
1308
|
-
*
|
|
1309
|
-
* ```typescript
|
|
1310
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1311
|
-
* import { NextResponse } from "next/server";
|
|
1312
|
-
*
|
|
1313
|
-
* export default async function middleware() {
|
|
1314
|
-
* const authenticated = await monoCloud.isAuthenticated();
|
|
1315
|
-
*
|
|
1316
|
-
* if (!authenticated) {
|
|
1317
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
1318
|
-
* }
|
|
1319
|
-
*
|
|
1320
|
-
* return NextResponse.next();
|
|
1321
|
-
* }
|
|
1322
|
-
*
|
|
1323
|
-
* export const config = {
|
|
1324
|
-
* matcher: [
|
|
1325
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
1326
|
-
* ],
|
|
1327
|
-
* };
|
|
1328
|
-
* ```
|
|
1329
|
-
*
|
|
1330
|
-
* @example App Router API Handler
|
|
1331
|
-
*
|
|
1332
|
-
* ```typescript
|
|
1333
|
-
* import { NextResponse } from "next/server";
|
|
1334
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1335
|
-
*
|
|
1336
|
-
* export const GET = async () => {
|
|
1337
|
-
* const authenticated = await monoCloud.isAuthenticated();
|
|
1338
|
-
*
|
|
1339
|
-
* return NextResponse.json({ authenticated });
|
|
1340
|
-
* };
|
|
1341
|
-
* ```
|
|
1342
|
-
*
|
|
1343
|
-
* @example React Server Components
|
|
1344
|
-
*
|
|
1345
|
-
* ```tsx
|
|
1346
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1347
|
-
*
|
|
1348
|
-
* export default async function Home() {
|
|
1349
|
-
* const authenticated = await monoCloud.isAuthenticated();
|
|
1350
|
-
*
|
|
1351
|
-
* return <div>Authenticated: {authenticated.toString()}</div>;
|
|
1352
|
-
* }
|
|
1353
|
-
* ```
|
|
1354
|
-
*
|
|
1355
|
-
* @example Server Action
|
|
1356
|
-
*
|
|
1357
|
-
* ```typescript
|
|
1358
|
-
* "use server";
|
|
1359
|
-
*
|
|
1360
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1361
|
-
*
|
|
1362
|
-
* export async function checkAuthAction() {
|
|
1363
|
-
* const authenticated = await monoCloud.isAuthenticated();
|
|
1364
|
-
*
|
|
1365
|
-
* return { authenticated };
|
|
1366
|
-
* }
|
|
1367
|
-
* ```
|
|
248
|
+
* @see {@link isAuthenticated} for full docs and examples.
|
|
249
|
+
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
1368
250
|
*/
|
|
1369
251
|
isAuthenticated(): Promise<boolean>;
|
|
1370
252
|
/**
|
|
1371
|
-
*
|
|
1372
|
-
*
|
|
1373
|
-
*
|
|
1374
|
-
*
|
|
1375
|
-
* **Use Case:**
|
|
1376
|
-
* - Middleware (for both App and Pages Router).
|
|
1377
|
-
* - App Router Route Handlers (API routes).
|
|
1378
|
-
* - Edge functions.
|
|
1379
|
-
*
|
|
1380
|
-
* @param req NextRequest
|
|
1381
|
-
* @param res An optional `NextResponse` instance. Pass this if you have already initialized a response; otherwise, omit this parameter.
|
|
1382
|
-
*
|
|
1383
|
-
* @returns `true` if the user is authenticated, otherwise `false`.
|
|
1384
|
-
*
|
|
1385
|
-
* @example Middleware/Proxy
|
|
1386
|
-
*
|
|
1387
|
-
* ```typescript
|
|
1388
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1389
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1390
|
-
*
|
|
1391
|
-
* export default async function middleware(req: NextRequest) {
|
|
1392
|
-
* const authenticated = await monoCloud.isAuthenticated(req);
|
|
1393
|
-
*
|
|
1394
|
-
* if (!authenticated) {
|
|
1395
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
1396
|
-
* }
|
|
1397
|
-
*
|
|
1398
|
-
* return NextResponse.next();
|
|
1399
|
-
* }
|
|
1400
|
-
*
|
|
1401
|
-
* export const config = {
|
|
1402
|
-
* matcher: [
|
|
1403
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
1404
|
-
* ],
|
|
1405
|
-
* };
|
|
1406
|
-
* ```
|
|
1407
|
-
*
|
|
1408
|
-
* @example Middleware/Proxy (Custom Response)
|
|
1409
|
-
*
|
|
1410
|
-
* ```typescript
|
|
1411
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1412
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1413
|
-
*
|
|
1414
|
-
* export default async function middleware(req: NextRequest) {
|
|
1415
|
-
* const res = NextResponse.next();
|
|
1416
|
-
*
|
|
1417
|
-
* const authenticated = await monoCloud.isAuthenticated(req, res);
|
|
1418
|
-
*
|
|
1419
|
-
* if (!authenticated) {
|
|
1420
|
-
* return new NextResponse("User not signed in", { status: 401 });
|
|
1421
|
-
* }
|
|
1422
|
-
*
|
|
1423
|
-
* res.headers.set("x-authenticated", "true");
|
|
1424
|
-
*
|
|
1425
|
-
* return res;
|
|
1426
|
-
* }
|
|
1427
|
-
*
|
|
1428
|
-
* export const config = {
|
|
1429
|
-
* matcher: [
|
|
1430
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
1431
|
-
* ],
|
|
1432
|
-
* };
|
|
1433
|
-
* ```
|
|
1434
|
-
*
|
|
1435
|
-
* @example API Handler
|
|
1436
|
-
*
|
|
1437
|
-
* ```typescript
|
|
1438
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1439
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1440
|
-
*
|
|
1441
|
-
* export const GET = async (req: NextRequest) => {
|
|
1442
|
-
* const authenticated = await monoCloud.isAuthenticated(req);
|
|
1443
|
-
*
|
|
1444
|
-
* return NextResponse.json({ authenticated });
|
|
1445
|
-
* };
|
|
1446
|
-
* ```
|
|
1447
|
-
*
|
|
1448
|
-
* @example API Handler with NextResponse
|
|
1449
|
-
*
|
|
1450
|
-
* ```typescript
|
|
1451
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1452
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1453
|
-
*
|
|
1454
|
-
* export const GET = async (req: NextRequest) => {
|
|
1455
|
-
* const res = new NextResponse("YOUR CUSTOM RESPONSE");
|
|
1456
|
-
*
|
|
1457
|
-
* const authenticated = await monoCloud.isAuthenticated(req, res);
|
|
1458
|
-
*
|
|
1459
|
-
* if (authenticated) {
|
|
1460
|
-
* res.cookies.set("something", "important");
|
|
1461
|
-
* }
|
|
1462
|
-
*
|
|
1463
|
-
* return res;
|
|
1464
|
-
* };
|
|
1465
|
-
* ```
|
|
253
|
+
* @see {@link isAuthenticated} for full docs and examples.
|
|
254
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
255
|
+
* @param res Optional response to update if refreshed authentication cookies or headers are required.
|
|
256
|
+
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
1466
257
|
*/
|
|
1467
258
|
isAuthenticated(req: NextRequest | Request, res?: NextResponse | Response): Promise<boolean>;
|
|
1468
259
|
/**
|
|
1469
|
-
*
|
|
1470
|
-
*
|
|
1471
|
-
*
|
|
1472
|
-
*
|
|
1473
|
-
* @param req NextApiRequest
|
|
1474
|
-
* @param res NextApiResponse
|
|
1475
|
-
*
|
|
1476
|
-
* @returns `true` if the user is authenticated, otherwise `false`.
|
|
1477
|
-
*
|
|
1478
|
-
* @example API Handler
|
|
1479
|
-
*
|
|
1480
|
-
* ```typescript
|
|
1481
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1482
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
1483
|
-
*
|
|
1484
|
-
* type Data = {
|
|
1485
|
-
* authenticated: boolean;
|
|
1486
|
-
* };
|
|
1487
|
-
*
|
|
1488
|
-
* export default async function handler(
|
|
1489
|
-
* req: NextApiRequest,
|
|
1490
|
-
* res: NextApiResponse<Data>
|
|
1491
|
-
* ) {
|
|
1492
|
-
* const authenticated = await monoCloud.isAuthenticated(req, res);
|
|
1493
|
-
*
|
|
1494
|
-
* res.status(200).json({ authenticated });
|
|
1495
|
-
* }
|
|
1496
|
-
* ```
|
|
1497
|
-
*
|
|
1498
|
-
* @example SSR Component
|
|
1499
|
-
*
|
|
1500
|
-
* ```typescript
|
|
1501
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1502
|
-
* import type { GetServerSideProps, InferGetServerSidePropsType } from "next";
|
|
1503
|
-
*
|
|
1504
|
-
* type HomeProps = InferGetServerSidePropsType<typeof getServerSideProps>;
|
|
1505
|
-
*
|
|
1506
|
-
* export default function Home({ authenticated }: HomeProps) {
|
|
1507
|
-
* return <pre>User is {authenticated ? "logged in" : "guest"}</pre>;
|
|
1508
|
-
* }
|
|
1509
|
-
*
|
|
1510
|
-
* export const getServerSideProps: GetServerSideProps = async (context) => {
|
|
1511
|
-
* const authenticated = await monoCloud.isAuthenticated(
|
|
1512
|
-
* context.req,
|
|
1513
|
-
* context.res
|
|
1514
|
-
* );
|
|
1515
|
-
*
|
|
1516
|
-
* return {
|
|
1517
|
-
* props: {
|
|
1518
|
-
* authenticated,
|
|
1519
|
-
* },
|
|
1520
|
-
* };
|
|
1521
|
-
* };
|
|
1522
|
-
* ```
|
|
260
|
+
* @see {@link isAuthenticated} for full docs and examples.
|
|
261
|
+
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
262
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
263
|
+
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
1523
264
|
*/
|
|
1524
265
|
isAuthenticated(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>): Promise<boolean>;
|
|
1525
266
|
/**
|
|
1526
|
-
*
|
|
1527
|
-
*
|
|
1528
|
-
*
|
|
1529
|
-
*
|
|
1530
|
-
* @param options Options to customize the sign-in.
|
|
1531
|
-
*
|
|
1532
|
-
* @returns
|
|
1533
|
-
*
|
|
1534
|
-
* @example React Server Component
|
|
1535
|
-
*
|
|
1536
|
-
* ```tsx
|
|
1537
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1538
|
-
*
|
|
1539
|
-
* export default async function Home() {
|
|
1540
|
-
* await monoCloud.protect();
|
|
1541
|
-
*
|
|
1542
|
-
* return <>You are signed in.</>;
|
|
1543
|
-
* }
|
|
1544
|
-
* ```
|
|
1545
|
-
*
|
|
1546
|
-
* @example API Handler
|
|
1547
|
-
*
|
|
1548
|
-
* ```typescript
|
|
1549
|
-
* import { NextResponse } from "next/server";
|
|
1550
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1551
|
-
*
|
|
1552
|
-
* export const GET = async () => {
|
|
1553
|
-
* await monoCloud.protect();
|
|
1554
|
-
*
|
|
1555
|
-
* return NextResponse.json({ secret: "ssshhhh!!!" });
|
|
1556
|
-
* };
|
|
1557
|
-
* ```
|
|
1558
|
-
*
|
|
1559
|
-
* @example Server Action
|
|
1560
|
-
*
|
|
1561
|
-
* ```typescript
|
|
1562
|
-
* "use server";
|
|
1563
|
-
*
|
|
1564
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1565
|
-
*
|
|
1566
|
-
* export async function getMessage() {
|
|
1567
|
-
* await monoCloud.protect();
|
|
1568
|
-
*
|
|
1569
|
-
* return { secret: "sssshhhhh!!!" };
|
|
1570
|
-
* }
|
|
1571
|
-
* ```
|
|
267
|
+
* @see {@link protect} for full docs and examples.
|
|
268
|
+
* @param options Optional configuration for redirect behavior (for example, return URL or sign-in parameters).
|
|
269
|
+
* @returns Resolves if the user is authenticated; otherwise triggers a redirect.
|
|
1572
270
|
*/
|
|
1573
271
|
protect(options?: ProtectOptions): Promise<void>;
|
|
1574
272
|
/**
|
|
1575
|
-
*
|
|
1576
|
-
*
|
|
1577
|
-
*
|
|
1578
|
-
*
|
|
1579
|
-
* **Use Case:**
|
|
1580
|
-
* - App Router Server Components (RSC).
|
|
1581
|
-
* - Server Actions
|
|
1582
|
-
* - Route Handlers (App Router only).
|
|
1583
|
-
* - Middleware (App Router and Pages Router).
|
|
1584
|
-
*
|
|
1585
|
-
* @param groups A list of group names or IDs to check against the user's group memberships.
|
|
1586
|
-
* @param options Configuration options.
|
|
1587
|
-
*
|
|
1588
|
-
* @returns
|
|
1589
|
-
*
|
|
1590
|
-
* @example Middleware/Proxy
|
|
1591
|
-
*
|
|
1592
|
-
* ```typescript
|
|
1593
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1594
|
-
* import { NextResponse } from "next/server";
|
|
1595
|
-
*
|
|
1596
|
-
* export default async function middleware() {
|
|
1597
|
-
* const isAdmin = await monoCloud.isUserInGroup(["admin"]);
|
|
1598
|
-
*
|
|
1599
|
-
* if (!isAdmin) {
|
|
1600
|
-
* return new NextResponse("User is not admin", { status: 403 });
|
|
1601
|
-
* }
|
|
1602
|
-
*
|
|
1603
|
-
* return NextResponse.next();
|
|
1604
|
-
* }
|
|
1605
|
-
*
|
|
1606
|
-
* export const config = {
|
|
1607
|
-
* matcher: [
|
|
1608
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
1609
|
-
* ],
|
|
1610
|
-
* };
|
|
1611
|
-
* ```
|
|
1612
|
-
*
|
|
1613
|
-
* @example App Router API Handler
|
|
1614
|
-
*
|
|
1615
|
-
* ```typescript
|
|
1616
|
-
* import { NextResponse } from "next/server";
|
|
1617
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1618
|
-
*
|
|
1619
|
-
* export const GET = async () => {
|
|
1620
|
-
* const allowed = await monoCloud.isUserInGroup(["admin", "editor"]);
|
|
1621
|
-
*
|
|
1622
|
-
* if (!allowed) {
|
|
1623
|
-
* return new NextResponse("Forbidden", { status: 403 });
|
|
1624
|
-
* }
|
|
1625
|
-
*
|
|
1626
|
-
* return NextResponse.json({ status: "success" });
|
|
1627
|
-
* };
|
|
1628
|
-
* ```
|
|
1629
|
-
*
|
|
1630
|
-
* @example React Server Components
|
|
1631
|
-
*
|
|
1632
|
-
* ```tsx
|
|
1633
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1634
|
-
*
|
|
1635
|
-
* export default async function AdminPanel() {
|
|
1636
|
-
* const isAdmin = await monoCloud.isUserInGroup(["admin"]);
|
|
1637
|
-
*
|
|
1638
|
-
* if (!isAdmin) {
|
|
1639
|
-
* return <div>Access Denied</div>;
|
|
1640
|
-
* }
|
|
1641
|
-
*
|
|
1642
|
-
* return <div>Admin Control Panel</div>;
|
|
1643
|
-
* }
|
|
1644
|
-
* ```
|
|
1645
|
-
*
|
|
1646
|
-
* @example Server Action
|
|
1647
|
-
*
|
|
1648
|
-
* ```typescript
|
|
1649
|
-
* "use server";
|
|
1650
|
-
*
|
|
1651
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1652
|
-
*
|
|
1653
|
-
* export async function deletePostAction() {
|
|
1654
|
-
* const canDelete = await monoCloud.isUserInGroup(["admin", "editor"]);
|
|
1655
|
-
*
|
|
1656
|
-
* if (!canDelete) {
|
|
1657
|
-
* return { success: false };
|
|
1658
|
-
* }
|
|
1659
|
-
*
|
|
1660
|
-
* return { success: true };
|
|
1661
|
-
* }
|
|
1662
|
-
* ```
|
|
273
|
+
* @see {@link isUserInGroup} for full docs and examples.
|
|
274
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
275
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
276
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1663
277
|
*/
|
|
1664
278
|
isUserInGroup(groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1665
279
|
/**
|
|
1666
|
-
*
|
|
1667
|
-
*
|
|
1668
|
-
*
|
|
1669
|
-
*
|
|
1670
|
-
*
|
|
1671
|
-
* - Middleware (for both App and Pages Router).
|
|
1672
|
-
* - App Router Route Handlers (API routes).
|
|
1673
|
-
* - Edge functions.
|
|
1674
|
-
*
|
|
1675
|
-
* @param req NextRequest
|
|
1676
|
-
* @param groups A list of group names or IDs to check against the user's group memberships.
|
|
1677
|
-
* @param options Configuration options.
|
|
1678
|
-
*
|
|
1679
|
-
* @returns
|
|
1680
|
-
*
|
|
1681
|
-
* @example Middleware/Proxy
|
|
1682
|
-
*
|
|
1683
|
-
* ```typescript
|
|
1684
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1685
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1686
|
-
*
|
|
1687
|
-
* export default async function middleware(req: NextRequest) {
|
|
1688
|
-
* const isAdmin = await monoCloud.isUserInGroup(req, ["admin"]);
|
|
1689
|
-
*
|
|
1690
|
-
* if (!isAdmin) {
|
|
1691
|
-
* return new NextResponse("User is not admin", { status: 403 });
|
|
1692
|
-
* }
|
|
1693
|
-
*
|
|
1694
|
-
* return NextResponse.next();
|
|
1695
|
-
* }
|
|
1696
|
-
*
|
|
1697
|
-
* export const config = {
|
|
1698
|
-
* matcher: [
|
|
1699
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
1700
|
-
* ],
|
|
1701
|
-
* };
|
|
1702
|
-
* ```
|
|
1703
|
-
*
|
|
1704
|
-
* @example App Router API Handler
|
|
1705
|
-
*
|
|
1706
|
-
* ```typescript
|
|
1707
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1708
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1709
|
-
*
|
|
1710
|
-
* export const GET = async (req: NextRequest) => {
|
|
1711
|
-
* const isMember = await monoCloud.isUserInGroup(req, ["admin", "editor"]);
|
|
1712
|
-
*
|
|
1713
|
-
* return NextResponse.json({ isMember });
|
|
1714
|
-
* };
|
|
1715
|
-
* ```
|
|
280
|
+
* @see {@link isUserInGroup} for full docs and examples.
|
|
281
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
282
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
283
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
284
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1716
285
|
*/
|
|
1717
286
|
isUserInGroup(req: NextRequest | Request, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1718
287
|
/**
|
|
1719
|
-
*
|
|
1720
|
-
*
|
|
1721
|
-
*
|
|
1722
|
-
*
|
|
1723
|
-
*
|
|
1724
|
-
*
|
|
1725
|
-
* - App Router Route Handlers (when a NextResponse is already initialized).
|
|
1726
|
-
*
|
|
1727
|
-
* @param req NextRequest
|
|
1728
|
-
* @param res An optional `NextResponse` instance. Pass this if you have already initialized a response and want token updates to be applied to it.
|
|
1729
|
-
* @param groups A list of group names or IDs to check against the user's group memberships.
|
|
1730
|
-
* @param options Configuration options.
|
|
1731
|
-
*
|
|
1732
|
-
* @returns
|
|
1733
|
-
*
|
|
1734
|
-
* @example Middleware/Proxy
|
|
1735
|
-
*
|
|
1736
|
-
* ```typescript
|
|
1737
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1738
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1739
|
-
*
|
|
1740
|
-
* export default async function middleware(req: NextRequest) {
|
|
1741
|
-
* const res = NextResponse.next();
|
|
1742
|
-
*
|
|
1743
|
-
* const isAdmin = await monoCloud.isUserInGroup(req, res, ["admin"]);
|
|
1744
|
-
*
|
|
1745
|
-
* if (!isAdmin) {
|
|
1746
|
-
* return new NextResponse("User is not admin", { status: 403 });
|
|
1747
|
-
* }
|
|
1748
|
-
*
|
|
1749
|
-
* res.headers.set("x-user", "admin");
|
|
1750
|
-
*
|
|
1751
|
-
* return res;
|
|
1752
|
-
* }
|
|
1753
|
-
*
|
|
1754
|
-
* export const config = {
|
|
1755
|
-
* matcher: [
|
|
1756
|
-
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
1757
|
-
* ],
|
|
1758
|
-
* };
|
|
1759
|
-
* ```
|
|
1760
|
-
*
|
|
1761
|
-
* @example API Handler with NextResponse
|
|
1762
|
-
*
|
|
1763
|
-
* ```typescript
|
|
1764
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
1765
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1766
|
-
*
|
|
1767
|
-
* export const GET = async (req: NextRequest) => {
|
|
1768
|
-
* const res = new NextResponse("Restricted Content");
|
|
1769
|
-
*
|
|
1770
|
-
* const allowed = await monoCloud.isUserInGroup(req, res, ["admin"]);
|
|
1771
|
-
*
|
|
1772
|
-
* if (!allowed) {
|
|
1773
|
-
* return new NextResponse("Not Allowed", res);
|
|
1774
|
-
* }
|
|
1775
|
-
*
|
|
1776
|
-
* return res;
|
|
1777
|
-
* };
|
|
1778
|
-
* ```
|
|
288
|
+
* @see {@link isUserInGroup} for full docs and examples.
|
|
289
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
290
|
+
* @param res Existing response to update with refreshed authentication cookies or headers when required.
|
|
291
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
292
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
293
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1779
294
|
*/
|
|
1780
295
|
isUserInGroup(req: NextRequest | Request, res: NextResponse | Response, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1781
296
|
/**
|
|
1782
|
-
*
|
|
1783
|
-
*
|
|
1784
|
-
*
|
|
1785
|
-
*
|
|
1786
|
-
* @param
|
|
1787
|
-
* @
|
|
1788
|
-
* @param groups A list of group names or IDs to check against the user's group memberships.
|
|
1789
|
-
* @param options Configuration options.
|
|
1790
|
-
*
|
|
1791
|
-
* @returns
|
|
1792
|
-
*
|
|
1793
|
-
* @example API Route
|
|
1794
|
-
*
|
|
1795
|
-
* ```typescript
|
|
1796
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1797
|
-
* import type { NextApiRequest, NextApiResponse } from "next";
|
|
1798
|
-
*
|
|
1799
|
-
* export default async function handler(
|
|
1800
|
-
* req: NextApiRequest,
|
|
1801
|
-
* res: NextApiResponse
|
|
1802
|
-
* ) {
|
|
1803
|
-
* const isAdmin = await monoCloud.isUserInGroup(req, res, ["admin"]);
|
|
1804
|
-
*
|
|
1805
|
-
* if (!isAdmin) {
|
|
1806
|
-
* return res.status(403).json({ error: "Forbidden" });
|
|
1807
|
-
* }
|
|
1808
|
-
*
|
|
1809
|
-
* res.status(200).json({ message: "Welcome Admin" });
|
|
1810
|
-
* }
|
|
1811
|
-
* ```
|
|
1812
|
-
*
|
|
1813
|
-
* @example SSR Component
|
|
1814
|
-
*
|
|
1815
|
-
* ```typescript
|
|
1816
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1817
|
-
* import type { GetServerSideProps, InferGetServerSidePropsType } from "next";
|
|
1818
|
-
*
|
|
1819
|
-
* type HomeProps = InferGetServerSidePropsType<typeof getServerSideProps>;
|
|
1820
|
-
*
|
|
1821
|
-
* export default function Home({ isAdmin }: HomeProps) {
|
|
1822
|
-
* return <div>User is admin: {isAdmin.toString()}</div>;
|
|
1823
|
-
* }
|
|
1824
|
-
*
|
|
1825
|
-
* export const getServerSideProps: GetServerSideProps = async (context) => {
|
|
1826
|
-
* const isAdmin = await monoCloud.isUserInGroup(context.req, context.res, [
|
|
1827
|
-
* "admin",
|
|
1828
|
-
* ]);
|
|
1829
|
-
*
|
|
1830
|
-
* return {
|
|
1831
|
-
* props: {
|
|
1832
|
-
* isAdmin,
|
|
1833
|
-
* },
|
|
1834
|
-
* };
|
|
1835
|
-
* };
|
|
1836
|
-
* ```
|
|
297
|
+
* @see {@link isUserInGroup} for full docs and examples.
|
|
298
|
+
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
299
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
300
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
301
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
302
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1837
303
|
*/
|
|
1838
304
|
isUserInGroup(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1839
305
|
/**
|
|
1840
|
-
*
|
|
1841
|
-
*
|
|
1842
|
-
*
|
|
1843
|
-
*
|
|
1844
|
-
* @param options Options to customize the sign-in.
|
|
1845
|
-
*
|
|
1846
|
-
* @returns
|
|
1847
|
-
*
|
|
1848
|
-
* @example React Server Component
|
|
1849
|
-
*
|
|
1850
|
-
* ```tsx
|
|
1851
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1852
|
-
*
|
|
1853
|
-
* export default async function Home() {
|
|
1854
|
-
* const allowed = await monoCloud.isUserInGroup(["admin"]);
|
|
1855
|
-
*
|
|
1856
|
-
* if (!allowed) {
|
|
1857
|
-
* await monoCloud.redirectToSignIn({ returnUrl: "/home" });
|
|
1858
|
-
* }
|
|
1859
|
-
*
|
|
1860
|
-
* return <>You are signed in.</>;
|
|
1861
|
-
* }
|
|
1862
|
-
* ```
|
|
1863
|
-
*
|
|
1864
|
-
* @example Server Action
|
|
1865
|
-
*
|
|
1866
|
-
* ```typescript
|
|
1867
|
-
* "use server";
|
|
1868
|
-
*
|
|
1869
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1870
|
-
*
|
|
1871
|
-
* export async function protectedAction() {
|
|
1872
|
-
* const session = await monoCloud.getSession();
|
|
1873
|
-
*
|
|
1874
|
-
* if (!session) {
|
|
1875
|
-
* await monoCloud.redirectToSignIn();
|
|
1876
|
-
* }
|
|
1877
|
-
*
|
|
1878
|
-
* return { data: "Sensitive Data" };
|
|
1879
|
-
* }
|
|
1880
|
-
* ```
|
|
1881
|
-
*
|
|
1882
|
-
* @example API Handler
|
|
1883
|
-
*
|
|
1884
|
-
* ```typescript
|
|
1885
|
-
* import { NextResponse } from "next/server";
|
|
1886
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1887
|
-
*
|
|
1888
|
-
* export const GET = async () => {
|
|
1889
|
-
* const session = await monoCloud.getSession();
|
|
1890
|
-
*
|
|
1891
|
-
* if (!session) {
|
|
1892
|
-
* await monoCloud.redirectToSignIn({
|
|
1893
|
-
* returnUrl: "/dashboard",
|
|
1894
|
-
* });
|
|
1895
|
-
* }
|
|
1896
|
-
*
|
|
1897
|
-
* return NextResponse.json({ data: "Protected content" });
|
|
1898
|
-
* };
|
|
1899
|
-
* ```
|
|
306
|
+
* @see {@link redirectToSignIn} for full docs and examples.
|
|
307
|
+
* @param options Optional configuration for the redirect, such as `returnUrl` or additional sign-in parameters.
|
|
308
|
+
* @returns Never resolves. Triggers a redirect to the sign-in flow.
|
|
1900
309
|
*/
|
|
1901
310
|
redirectToSignIn(options?: RedirectToSignInOptions): Promise<void>;
|
|
1902
311
|
/**
|
|
1903
|
-
*
|
|
1904
|
-
*
|
|
1905
|
-
*
|
|
1906
|
-
*
|
|
1907
|
-
* @param options Options to customize the sign out.
|
|
1908
|
-
*
|
|
1909
|
-
* @returns
|
|
1910
|
-
*
|
|
1911
|
-
* @example React Server Component
|
|
1912
|
-
*
|
|
1913
|
-
* ```tsx
|
|
1914
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1915
|
-
*
|
|
1916
|
-
* export default async function Page() {
|
|
1917
|
-
* const session = await monoCloud.getSession();
|
|
1918
|
-
*
|
|
1919
|
-
* // Example: Force sign-out if a specific condition is met (e.g., account suspended)
|
|
1920
|
-
* if (session?.user.isSuspended) {
|
|
1921
|
-
* await monoCloud.redirectToSignOut();
|
|
1922
|
-
* }
|
|
1923
|
-
*
|
|
1924
|
-
* return <>Welcome User</>;
|
|
1925
|
-
* }
|
|
1926
|
-
* ```
|
|
1927
|
-
*
|
|
1928
|
-
* @example Server Action
|
|
1929
|
-
*
|
|
1930
|
-
* ```typescript
|
|
1931
|
-
* "use server";
|
|
1932
|
-
*
|
|
1933
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1934
|
-
*
|
|
1935
|
-
* export async function signOutAction() {
|
|
1936
|
-
* const session = await monoCloud.getSession();
|
|
1937
|
-
*
|
|
1938
|
-
* if (session) {
|
|
1939
|
-
* await monoCloud.redirectToSignOut();
|
|
1940
|
-
* }
|
|
1941
|
-
* }
|
|
1942
|
-
* ```
|
|
1943
|
-
*
|
|
1944
|
-
* @example API Handler
|
|
1945
|
-
*
|
|
1946
|
-
* ```typescript
|
|
1947
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1948
|
-
* import { NextResponse } from "next/server";
|
|
1949
|
-
*
|
|
1950
|
-
* export const GET = async () => {
|
|
1951
|
-
* const session = await monoCloud.getSession();
|
|
1952
|
-
*
|
|
1953
|
-
* if (session) {
|
|
1954
|
-
* await monoCloud.redirectToSignOut({
|
|
1955
|
-
* postLogoutRedirectUri: "/goodbye",
|
|
1956
|
-
* });
|
|
1957
|
-
* }
|
|
1958
|
-
*
|
|
1959
|
-
* return NextResponse.json({ status: "already_signed_out" });
|
|
1960
|
-
* };
|
|
1961
|
-
* ```
|
|
312
|
+
* @see {@link redirectToSignOut} for full docs and examples.
|
|
313
|
+
* @param options Optional configuration for the redirect, such as `postLogoutRedirectUri` or additional sign-out parameters.
|
|
314
|
+
* @returns Never resolves. Triggers a redirect to the sign-out flow.
|
|
1962
315
|
*/
|
|
1963
316
|
redirectToSignOut(options?: RedirectToSignOutOptions): Promise<void>;
|
|
1964
317
|
private getOptions;
|
|
1965
318
|
private registerPublicEnvVariables;
|
|
1966
319
|
}
|
|
1967
320
|
//#endregion
|
|
1968
|
-
|
|
321
|
+
//#region src/initialize.d.ts
|
|
322
|
+
/**
|
|
323
|
+
* Creates a Next.js catch-all auth route handler (Pages Router and App Router) for the built-in routes (`/signin`, `/callback`, `/userinfo`, `/signout`).
|
|
324
|
+
*
|
|
325
|
+
* Mount this handler on a catch-all route (e.g. `/api/auth/[...monocloud]`).
|
|
326
|
+
*
|
|
327
|
+
* > If you already use `authMiddleware()`, you typically don’t need this handler. Use `monoCloudAuth()` when middleware cannot be used or when auth routes need customization.
|
|
328
|
+
*
|
|
329
|
+
* @example App Router
|
|
330
|
+
* ```tsx:src/app/api/auth/[...monocloud]/route.ts tab="App Router" tab-group="monoCloudAuth"
|
|
331
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
332
|
+
*
|
|
333
|
+
* export const GET = monoCloudAuth();
|
|
334
|
+
*```
|
|
335
|
+
*
|
|
336
|
+
* @example App Router (Response)
|
|
337
|
+
* ```tsx:src/app/api/auth/[...monocloud]/route.ts tab="App Router (Response)" tab-group="monoCloudAuth"
|
|
338
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
339
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
340
|
+
*
|
|
341
|
+
* export const GET = (req: NextRequest) => {
|
|
342
|
+
* const authHandler = monoCloudAuth();
|
|
343
|
+
*
|
|
344
|
+
* const res = new NextResponse();
|
|
345
|
+
*
|
|
346
|
+
* res.cookies.set("last_auth_requested", `${Date.now()}`);
|
|
347
|
+
*
|
|
348
|
+
* return authHandler(req, res);
|
|
349
|
+
* };
|
|
350
|
+
* ```
|
|
351
|
+
*
|
|
352
|
+
* @example Pages Router
|
|
353
|
+
* ```tsx:src/pages/api/auth/[...monocloud].ts tab="Pages Router" tab-group="monoCloudAuth"
|
|
354
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
355
|
+
*
|
|
356
|
+
* export default monoCloudAuth();
|
|
357
|
+
*```
|
|
358
|
+
*
|
|
359
|
+
* @example Pages Router (Response)
|
|
360
|
+
* ```tsx:src/pages/api/auth/[...monocloud].ts tab="Pages Router (Response)" tab-group="monoCloudAuth"
|
|
361
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
362
|
+
* import { NextApiRequest, NextApiResponse } from "next";
|
|
363
|
+
*
|
|
364
|
+
* export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
365
|
+
* const authHandler = monoCloudAuth();
|
|
366
|
+
*
|
|
367
|
+
* res.setHeader("last_auth_requested", `${Date.now()}`);
|
|
368
|
+
*
|
|
369
|
+
* return authHandler(req, res);
|
|
370
|
+
* }
|
|
371
|
+
* ```
|
|
372
|
+
*
|
|
373
|
+
* @param options Optional configuration for the auth handler.
|
|
374
|
+
* @returns Returns a Next.js-compatible handler for App Router route handlers or Pages Router API routes.
|
|
375
|
+
*
|
|
376
|
+
* @category Functions
|
|
377
|
+
*/
|
|
378
|
+
declare function monoCloudAuth(options?: MonoCloudAuthOptions): MonoCloudAuthHandler;
|
|
379
|
+
/**
|
|
380
|
+
* Creates a Next.js authentication middleware that protects routes.
|
|
381
|
+
*
|
|
382
|
+
* By default, all routes matched by `config.matcher` are protected unless configured otherwise.
|
|
383
|
+
*
|
|
384
|
+
* @example Protect All Routes
|
|
385
|
+
* ```tsx:src/proxy.ts tab="Protect All Routes" tab-group="auth-middleware"
|
|
386
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
387
|
+
*
|
|
388
|
+
* export default authMiddleware();
|
|
389
|
+
*
|
|
390
|
+
* export const config = {
|
|
391
|
+
* matcher: [
|
|
392
|
+
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
393
|
+
* ],
|
|
394
|
+
* };
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
* @example Protect Selected Routes
|
|
398
|
+
* ```tsx:src/proxy.ts tab="Protect Selected Routes" tab-group="auth-middleware"
|
|
399
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
400
|
+
*
|
|
401
|
+
* export default authMiddleware({
|
|
402
|
+
* protectedRoutes: ["/api/admin", "^/api/protected(/.*)?$"],
|
|
403
|
+
* });
|
|
404
|
+
*
|
|
405
|
+
* export const config = {
|
|
406
|
+
* matcher: [
|
|
407
|
+
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
408
|
+
* ],
|
|
409
|
+
* };
|
|
410
|
+
*```
|
|
411
|
+
*
|
|
412
|
+
* @example No Protected Routes
|
|
413
|
+
* ```tsx:src/proxy.ts tab="No Protected Routes" tab-group="auth-middleware"
|
|
414
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
415
|
+
*
|
|
416
|
+
* export default authMiddleware({
|
|
417
|
+
* protectedRoutes: [],
|
|
418
|
+
* });
|
|
419
|
+
*
|
|
420
|
+
* export const config = {
|
|
421
|
+
* matcher: [
|
|
422
|
+
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
423
|
+
* ],
|
|
424
|
+
* };
|
|
425
|
+
* ```
|
|
426
|
+
*
|
|
427
|
+
* @example Dynamic
|
|
428
|
+
* ```tsx:src/proxy.ts tab="Dynamic" tab-group="auth-middleware"
|
|
429
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
430
|
+
*
|
|
431
|
+
* export default authMiddleware({
|
|
432
|
+
* protectedRoutes: (req) => {
|
|
433
|
+
* return req.nextUrl.pathname.startsWith("/api/protected");
|
|
434
|
+
* },
|
|
435
|
+
* });
|
|
436
|
+
*
|
|
437
|
+
* export const config = {
|
|
438
|
+
* matcher: [
|
|
439
|
+
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
440
|
+
* ],
|
|
441
|
+
* };
|
|
442
|
+
* ```
|
|
443
|
+
*
|
|
444
|
+
* @example Group Protection
|
|
445
|
+
* ```tsx:src/proxy.ts tab="Group Protection" tab-group="auth-middleware"
|
|
446
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
447
|
+
*
|
|
448
|
+
* export default authMiddleware({
|
|
449
|
+
* protectedRoutes: [
|
|
450
|
+
* {
|
|
451
|
+
* groups: ["admin", "editor", "537e7c3d-a442-4b5b-b308-30837aa045a4"],
|
|
452
|
+
* routes: ["/internal", "/api/internal(.*)"],
|
|
453
|
+
* },
|
|
454
|
+
* ],
|
|
455
|
+
* });
|
|
456
|
+
*
|
|
457
|
+
* export const config = {
|
|
458
|
+
* matcher: [
|
|
459
|
+
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
460
|
+
* ],
|
|
461
|
+
* };
|
|
462
|
+
* ```
|
|
463
|
+
*
|
|
464
|
+
* @param options Optional configuration that controls how authentication is enforced (for example, redirect behavior, route matching, or custom handling of unauthenticated requests).
|
|
465
|
+
* @returns Returns a Next.js middleware result, such as a `NextResponse`, `redirect`, or `undefined` to continue processing.
|
|
466
|
+
*
|
|
467
|
+
* @category Functions
|
|
468
|
+
*/
|
|
469
|
+
declare function authMiddleware(options?: MonoCloudMiddlewareOptions): NextMiddleware$1 | NextProxy$1;
|
|
470
|
+
/**
|
|
471
|
+
* Executes the authentication middleware manually.
|
|
472
|
+
*
|
|
473
|
+
* Intended for advanced scenarios where the middleware is composed within custom logic.
|
|
474
|
+
*
|
|
475
|
+
* @example Composing with custom middleware
|
|
476
|
+
*
|
|
477
|
+
* ```tsx:src/proxy.ts title="Composing with custom middleware"
|
|
478
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
479
|
+
* import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
|
|
480
|
+
*
|
|
481
|
+
* export default function customMiddleware(req: NextRequest, evt: NextFetchEvent) {
|
|
482
|
+
* if (req.nextUrl.pathname.startsWith("/api/protected")) {
|
|
483
|
+
* return authMiddleware(req, evt);
|
|
484
|
+
* }
|
|
485
|
+
*
|
|
486
|
+
* return NextResponse.next();
|
|
487
|
+
* }
|
|
488
|
+
*
|
|
489
|
+
* export const config = {
|
|
490
|
+
* matcher: [
|
|
491
|
+
* "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
|
|
492
|
+
* ],
|
|
493
|
+
* };
|
|
494
|
+
* ```
|
|
495
|
+
*
|
|
496
|
+
* @param request Incoming Next.js middleware request used to resolve authentication state.
|
|
497
|
+
* @param event Next.js middleware event providing lifecycle hooks such as `waitUntil`.
|
|
498
|
+
* @returns Returns a Next.js middleware result (`NextResponse`, redirect, or `undefined` to continue processing).
|
|
499
|
+
*
|
|
500
|
+
* @category Functions
|
|
501
|
+
*/
|
|
502
|
+
declare function authMiddleware(request: NextRequest$1, event: NextFetchEvent$1): Promise<NextMiddlewareResult> | NextMiddlewareResult;
|
|
503
|
+
/**
|
|
504
|
+
* Retrieves the current user's session using the active server request context.
|
|
505
|
+
*
|
|
506
|
+
* Intended for Server Components, Server Actions, Route Handlers, and Middleware where the request is implicitly available.
|
|
507
|
+
*
|
|
508
|
+
* @example Server Component
|
|
509
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="session-ssr"
|
|
510
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
511
|
+
*
|
|
512
|
+
* export default async function Home() {
|
|
513
|
+
* const session = await getSession();
|
|
514
|
+
*
|
|
515
|
+
* return <div>{session?.user.name}</div>;
|
|
516
|
+
* }
|
|
517
|
+
* ```
|
|
518
|
+
*
|
|
519
|
+
* @example Server Action
|
|
520
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="session-ssr"
|
|
521
|
+
* "use server";
|
|
522
|
+
*
|
|
523
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
524
|
+
*
|
|
525
|
+
* export async function getUserAction() {
|
|
526
|
+
* const session = await getSession();
|
|
527
|
+
*
|
|
528
|
+
* return { name: session?.user.name };
|
|
529
|
+
* }
|
|
530
|
+
* ```
|
|
531
|
+
*
|
|
532
|
+
* @example API Handler
|
|
533
|
+
* ```tsx:src/app/api/user/route.ts tab="API Handler" tab-group="session-ssr"
|
|
534
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
535
|
+
* import { NextResponse } from "next/server";
|
|
536
|
+
*
|
|
537
|
+
* export const GET = async () => {
|
|
538
|
+
* const session = await getSession();
|
|
539
|
+
*
|
|
540
|
+
* return NextResponse.json({ name: session?.user.name });
|
|
541
|
+
* };
|
|
542
|
+
* ```
|
|
543
|
+
*
|
|
544
|
+
* @example Middleware
|
|
545
|
+
* ```tsx:src/proxy.ts tab="Middleware" tab-group="session-ssr"
|
|
546
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
547
|
+
* import { NextResponse } from "next/server";
|
|
548
|
+
*
|
|
549
|
+
* export default async function proxy() {
|
|
550
|
+
* const session = await getSession();
|
|
551
|
+
*
|
|
552
|
+
* if (!session) {
|
|
553
|
+
* return new NextResponse("User not signed in", { status: 401 });
|
|
554
|
+
* }
|
|
555
|
+
*
|
|
556
|
+
* return NextResponse.next();
|
|
557
|
+
* }
|
|
558
|
+
* ```
|
|
559
|
+
*
|
|
560
|
+
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
561
|
+
*
|
|
562
|
+
* @category Functions
|
|
563
|
+
*/
|
|
564
|
+
declare function getSession(): Promise<MonoCloudSession$2 | undefined>;
|
|
565
|
+
/**
|
|
566
|
+
* Retrieves the current user's session using an explicit Web or Next.js request.
|
|
567
|
+
*
|
|
568
|
+
* Use this overload when you already have access to a `Request` or `NextRequest` (for example in Middleware or Route Handlers).
|
|
569
|
+
*
|
|
570
|
+
* @example Middleware (Request)
|
|
571
|
+
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="session-route-handler"
|
|
572
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
573
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
574
|
+
*
|
|
575
|
+
* export default async function proxy(req: NextRequest) {
|
|
576
|
+
* const session = await getSession(req);
|
|
577
|
+
*
|
|
578
|
+
* if (!session) {
|
|
579
|
+
* return new NextResponse("User not signed in", { status: 401 });
|
|
580
|
+
* }
|
|
581
|
+
*
|
|
582
|
+
* return NextResponse.next();
|
|
583
|
+
* }
|
|
584
|
+
* ```
|
|
585
|
+
*
|
|
586
|
+
* @example Middleware (Response)
|
|
587
|
+
* ```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="session-route-handler"
|
|
588
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
589
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
590
|
+
*
|
|
591
|
+
* export default async function proxy(req: NextRequest) {
|
|
592
|
+
* const res = NextResponse.next();
|
|
593
|
+
*
|
|
594
|
+
* const session = await getSession(req, res);
|
|
595
|
+
*
|
|
596
|
+
* if (!session) {
|
|
597
|
+
* return new NextResponse("User not signed in", { status: 401 });
|
|
598
|
+
* }
|
|
599
|
+
*
|
|
600
|
+
* res.headers.set("x-auth-status", "active");
|
|
601
|
+
*
|
|
602
|
+
* return res;
|
|
603
|
+
* }
|
|
604
|
+
* ```
|
|
605
|
+
*
|
|
606
|
+
* @example API Handler (Request)
|
|
607
|
+
* ```tsx:src/app/api/user/route.ts tab="API Handler (Request)" tab-group="session-route-handler"
|
|
608
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
609
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
610
|
+
*
|
|
611
|
+
* export const GET = async (req: NextRequest) => {
|
|
612
|
+
* const session = await getSession(req);
|
|
613
|
+
*
|
|
614
|
+
* return NextResponse.json({ name: session?.user.name });
|
|
615
|
+
* };
|
|
616
|
+
* ```
|
|
617
|
+
*
|
|
618
|
+
* @example API Handler (Response)
|
|
619
|
+
* ```tsx:src/app/api/user/route.ts tab="API Handler (Response)" tab-group="session-route-handler"
|
|
620
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
621
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
622
|
+
*
|
|
623
|
+
* export const GET = async (req: NextRequest) => {
|
|
624
|
+
* const res = new NextResponse("YOUR CUSTOM RESPONSE");
|
|
625
|
+
*
|
|
626
|
+
* const session = await getSession(req, res);
|
|
627
|
+
*
|
|
628
|
+
* if (session?.user) {
|
|
629
|
+
* res.cookies.set("something", "important");
|
|
630
|
+
* }
|
|
631
|
+
*
|
|
632
|
+
* return res;
|
|
633
|
+
* };
|
|
634
|
+
* ```
|
|
635
|
+
*
|
|
636
|
+
* @param req Incoming request used to read authentication cookies and headers to resolve the current user's session.
|
|
637
|
+
* @param res Optional response to update if session resolution requires refreshed authentication cookies or headers.
|
|
638
|
+
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
639
|
+
*
|
|
640
|
+
* @category Functions
|
|
641
|
+
*/
|
|
642
|
+
declare function getSession(req: NextRequest$1 | Request, res?: NextResponse$1 | Response): Promise<MonoCloudSession$2 | undefined>;
|
|
643
|
+
/**
|
|
644
|
+
* Retrieves the current user's session in the Pages Router or Node.js runtime.
|
|
645
|
+
*
|
|
646
|
+
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
647
|
+
*
|
|
648
|
+
* @example Pages Router (Pages)
|
|
649
|
+
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="session-pages"
|
|
650
|
+
* import { getSession, MonoCloudSession } from "@monocloud/auth-nextjs";
|
|
651
|
+
* import { GetServerSideProps } from "next";
|
|
652
|
+
*
|
|
653
|
+
* type Props = {
|
|
654
|
+
* session?: MonoCloudSession;
|
|
655
|
+
* };
|
|
656
|
+
*
|
|
657
|
+
* export default function Home({ session }: Props) {
|
|
658
|
+
* return <pre>Session: {JSON.stringify(session, null, 2)}</pre>;
|
|
659
|
+
* }
|
|
660
|
+
*
|
|
661
|
+
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
662
|
+
* const session = await getSession(ctx.req, ctx.res);
|
|
663
|
+
*
|
|
664
|
+
* return {
|
|
665
|
+
* props: {
|
|
666
|
+
* session
|
|
667
|
+
* }
|
|
668
|
+
* };
|
|
669
|
+
* };
|
|
670
|
+
* ```
|
|
671
|
+
* @example Pages Router (API)
|
|
672
|
+
* ```tsx:src/pages/api/user.ts tab="Pages Router (API)" tab-group="session-pages"
|
|
673
|
+
* import { getSession } from "@monocloud/auth-nextjs";
|
|
674
|
+
* import { NextApiRequest, NextApiResponse } from "next";
|
|
675
|
+
*
|
|
676
|
+
* export default async function handler(
|
|
677
|
+
* req: NextApiRequest,
|
|
678
|
+
* res: NextApiResponse
|
|
679
|
+
* ) {
|
|
680
|
+
* const session = await getSession(req, res);
|
|
681
|
+
*
|
|
682
|
+
* res.status(200).json({ name: session?.user.name });
|
|
683
|
+
* }
|
|
684
|
+
* ```
|
|
685
|
+
*
|
|
686
|
+
* @param req Incoming Node.js request used to read authentication cookies and resolve the current user's session.
|
|
687
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
688
|
+
* @returns Returns the resolved session, or `undefined` if none exists.
|
|
689
|
+
*
|
|
690
|
+
* @category Functions
|
|
691
|
+
*/
|
|
692
|
+
declare function getSession(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>): Promise<MonoCloudSession$2 | undefined>;
|
|
693
|
+
/**
|
|
694
|
+
* Retrieves the current user's tokens using the active server request context.
|
|
695
|
+
*
|
|
696
|
+
* @example Server Component
|
|
697
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="tokens-ssr"
|
|
698
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
699
|
+
*
|
|
700
|
+
* export default async function Home() {
|
|
701
|
+
* const tokens = await getTokens();
|
|
702
|
+
*
|
|
703
|
+
* return <div>Expired: {tokens.isExpired.toString()}</div>;
|
|
704
|
+
* }
|
|
705
|
+
* ```
|
|
706
|
+
*
|
|
707
|
+
* @example Server Action
|
|
708
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="tokens-ssr"
|
|
709
|
+
* "use server";
|
|
710
|
+
*
|
|
711
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
712
|
+
*
|
|
713
|
+
* export async function getExpiredAction() {
|
|
714
|
+
* const tokens = await getTokens();
|
|
715
|
+
*
|
|
716
|
+
* return { expired: tokens.isExpired };
|
|
717
|
+
* }
|
|
718
|
+
* ```
|
|
719
|
+
*
|
|
720
|
+
* @example API Handler
|
|
721
|
+
* ```tsx:src/app/api/tokens/route.ts tab="API Handler" tab-group="tokens-ssr"
|
|
722
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
723
|
+
* import { NextResponse } from "next/server";
|
|
724
|
+
*
|
|
725
|
+
* export const GET = async () => {
|
|
726
|
+
* const tokens = await getTokens();
|
|
727
|
+
*
|
|
728
|
+
* return NextResponse.json({ expired: tokens.isExpired });
|
|
729
|
+
* };
|
|
730
|
+
* ```
|
|
731
|
+
*
|
|
732
|
+
* @example Middleware
|
|
733
|
+
* ```tsx:src/proxy.ts tab="Middleware" tab-group="tokens-ssr"
|
|
734
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
735
|
+
* import { NextResponse } from "next/server";
|
|
736
|
+
*
|
|
737
|
+
* export default async function proxy() {
|
|
738
|
+
* const tokens = await getTokens();
|
|
739
|
+
*
|
|
740
|
+
* if (tokens.isExpired) {
|
|
741
|
+
* return new NextResponse("Tokens expired", { status: 401 });
|
|
742
|
+
* }
|
|
743
|
+
*
|
|
744
|
+
* return NextResponse.next();
|
|
745
|
+
* }
|
|
746
|
+
* ```
|
|
747
|
+
*
|
|
748
|
+
* @example Refresh the default token
|
|
749
|
+
*
|
|
750
|
+
* The **default token** is the access token associated with your default authorization parameters:
|
|
751
|
+
* - Scopes: `MONOCLOUD_AUTH_SCOPES` or `options.defaultAuthParams.scopes`
|
|
752
|
+
* - Resource: `MONOCLOUD_AUTH_RESOURCE` or `options.defaultAuthParams.resource`
|
|
753
|
+
*
|
|
754
|
+
* Calling `getTokens()` returns the current token set and **refreshes the default token automatically when needed** (for example, if it has expired). To force a refresh even when it isn’t expired, use `forceRefresh: true`.
|
|
755
|
+
*
|
|
756
|
+
* ```tsx:src/app/page.tsx title="Refresh default token"
|
|
757
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
758
|
+
* import { NextResponse } from "next/server";
|
|
759
|
+
*
|
|
760
|
+
* export const GET = async () => {
|
|
761
|
+
* // Forces a refresh of the default token
|
|
762
|
+
* const tokens = await getTokens({ forceRefresh: true });
|
|
763
|
+
*
|
|
764
|
+
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
765
|
+
* };
|
|
766
|
+
* ```
|
|
767
|
+
*
|
|
768
|
+
* @example Request an access token for resource(s)
|
|
769
|
+
*
|
|
770
|
+
* Use `resource` and `scopes` to request an access token for one or more resources.
|
|
771
|
+
*
|
|
772
|
+
* > The requested resource and scopes must be included in the initial authorization flow (so the user has consented / the session is eligible to mint that token).
|
|
773
|
+
*
|
|
774
|
+
* ```tsx:src/app/page.tsx title="Request a new access token for resource(s)"
|
|
775
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
776
|
+
* import { NextResponse } from "next/server";
|
|
777
|
+
*
|
|
778
|
+
* export const GET = async () => {
|
|
779
|
+
* const tokens = await getTokens({
|
|
780
|
+
* resource: "https://first.example.com https://second.example.com",
|
|
781
|
+
* scopes: "read:first read:second shared",
|
|
782
|
+
* });
|
|
783
|
+
*
|
|
784
|
+
* return NextResponse.json({ accessToken: tokens?.accessToken });
|
|
785
|
+
* };
|
|
786
|
+
* ```
|
|
787
|
+
*
|
|
788
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
789
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
790
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
791
|
+
*
|
|
792
|
+
* @category Functions
|
|
793
|
+
*/
|
|
794
|
+
declare function getTokens(options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
795
|
+
/**
|
|
796
|
+
* Retrieves the current user's tokens using an explicit Web or Next.js request.
|
|
797
|
+
*
|
|
798
|
+
* Use this overload when you already have access to a `Request` or `NextRequest` (for example, in Middleware or Route Handlers).
|
|
799
|
+
*
|
|
800
|
+
* @example Middleware (Request)
|
|
801
|
+
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="tokens-route-handler-request"
|
|
802
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
803
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
804
|
+
*
|
|
805
|
+
* export default async function proxy(req: NextRequest) {
|
|
806
|
+
* const tokens = await getTokens(req);
|
|
807
|
+
*
|
|
808
|
+
* if (tokens.isExpired) {
|
|
809
|
+
* return new NextResponse("Tokens expired", { status: 401 });
|
|
810
|
+
* }
|
|
811
|
+
*
|
|
812
|
+
* return NextResponse.next();
|
|
813
|
+
* }
|
|
814
|
+
* ```
|
|
815
|
+
*
|
|
816
|
+
* @example API Handler (Request)
|
|
817
|
+
* ```tsx:src/app/api/tokens/route.ts tab="API Handler (Request)" tab-group="tokens-route-handler-request"
|
|
818
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
819
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
820
|
+
*
|
|
821
|
+
* export const GET = async (req: NextRequest) => {
|
|
822
|
+
* const tokens = await getTokens(req);
|
|
823
|
+
*
|
|
824
|
+
* return NextResponse.json({ expired: tokens?.isExpired });
|
|
825
|
+
* };
|
|
826
|
+
* ```
|
|
827
|
+
*
|
|
828
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
829
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
830
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
831
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
832
|
+
*
|
|
833
|
+
* @category Functions
|
|
834
|
+
*/
|
|
835
|
+
declare function getTokens(req: NextRequest$1 | Request, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
836
|
+
/**
|
|
837
|
+
* Retrieves the current user's tokens using an explicit request and response.
|
|
838
|
+
*
|
|
839
|
+
* Use this overload when you have already created a response and want refreshed authentication cookies or headers applied to it.
|
|
840
|
+
*
|
|
841
|
+
* @example Middleware (Response)
|
|
842
|
+
*```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="tokens-route-handler-response"
|
|
843
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
844
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
845
|
+
*
|
|
846
|
+
* export default async function proxy(req: NextRequest) {
|
|
847
|
+
* const res = NextResponse.next();
|
|
848
|
+
*
|
|
849
|
+
* const tokens = await getTokens(req, res);
|
|
850
|
+
*
|
|
851
|
+
* res.headers.set("x-tokens-expired", tokens.isExpired.toString());
|
|
852
|
+
*
|
|
853
|
+
* return res;
|
|
854
|
+
* }
|
|
855
|
+
* ```
|
|
856
|
+
*
|
|
857
|
+
* @example API Handler (Response)
|
|
858
|
+
* ```tsx:src/app/api/tokens/route.ts tab="API Handler (Response)" tab-group="tokens-route-handler-response"
|
|
859
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
860
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
861
|
+
*
|
|
862
|
+
* export const GET = async (req: NextRequest) => {
|
|
863
|
+
* const res = new NextResponse("Custom Body");
|
|
864
|
+
*
|
|
865
|
+
* const tokens = await getTokens(req, res);
|
|
866
|
+
*
|
|
867
|
+
* if (!tokens.isExpired) {
|
|
868
|
+
* res.headers.set("x-auth-status", "active");
|
|
869
|
+
* }
|
|
870
|
+
*
|
|
871
|
+
* return res;
|
|
872
|
+
* };
|
|
873
|
+
* ```
|
|
874
|
+
*
|
|
875
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
876
|
+
* @param res Existing response to update with refreshed authentication cookies or headers.
|
|
877
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
878
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
879
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
880
|
+
*
|
|
881
|
+
* @category Functions
|
|
882
|
+
*/
|
|
883
|
+
declare function getTokens(req: NextRequest$1 | Request, res: NextResponse$1 | Response, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
884
|
+
/**
|
|
885
|
+
* Retrieves the current user's tokens in the Pages Router or Node.js runtime.
|
|
886
|
+
*
|
|
887
|
+
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
888
|
+
*
|
|
889
|
+
* @example Pages Router (Pages)
|
|
890
|
+
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="tokens-pages"
|
|
891
|
+
* import { getTokens, MonoCloudTokens } from "@monocloud/auth-nextjs";
|
|
892
|
+
* import { GetServerSideProps } from "next";
|
|
893
|
+
*
|
|
894
|
+
* type Props = {
|
|
895
|
+
* tokens: MonoCloudTokens;
|
|
896
|
+
* };
|
|
897
|
+
*
|
|
898
|
+
* export default function Home({ tokens }: Props) {
|
|
899
|
+
* return <pre>Tokens: {JSON.stringify(tokens, null, 2)}</pre>;
|
|
900
|
+
* }
|
|
901
|
+
*
|
|
902
|
+
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
903
|
+
* const tokens = await getTokens(ctx.req, ctx.res);
|
|
904
|
+
*
|
|
905
|
+
* return {
|
|
906
|
+
* props: {
|
|
907
|
+
* tokens: tokens
|
|
908
|
+
* }
|
|
909
|
+
* };
|
|
910
|
+
* };
|
|
911
|
+
* ```
|
|
912
|
+
*
|
|
913
|
+
* @example Pages Router (API)
|
|
914
|
+
* ```tsx:src/pages/api/tokens.ts tab="Pages Router (API)" tab-group="tokens-pages"
|
|
915
|
+
* import { getTokens } from "@monocloud/auth-nextjs";
|
|
916
|
+
* import { NextApiRequest, NextApiResponse } from "next";
|
|
917
|
+
*
|
|
918
|
+
* export default async function handler(
|
|
919
|
+
* req: NextApiRequest,
|
|
920
|
+
* res: NextApiResponse
|
|
921
|
+
* ) {
|
|
922
|
+
* const tokens = await getTokens(req, res);
|
|
923
|
+
*
|
|
924
|
+
* res.status(200).json({ accessToken: tokens?.accessToken });
|
|
925
|
+
* }
|
|
926
|
+
* ```
|
|
927
|
+
*
|
|
928
|
+
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
929
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
930
|
+
* @param options Optional configuration controlling refresh behavior and resource/scope selection.
|
|
931
|
+
* @returns The current user's tokens, refreshed if necessary.
|
|
932
|
+
* @throws {@link MonoCloudValidationError} If no valid session exists.
|
|
933
|
+
*
|
|
934
|
+
* @category Functions
|
|
935
|
+
*/
|
|
936
|
+
declare function getTokens(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>, options?: GetTokensOptions$1): Promise<MonoCloudTokens$1>;
|
|
937
|
+
/**
|
|
938
|
+
* Checks whether the current user is authenticated using the active server request context.
|
|
939
|
+
*
|
|
940
|
+
* Intended for Server Components, Server Actions, Route Handlers, and Middleware where the request is implicitly available.
|
|
941
|
+
*
|
|
942
|
+
* @example Server Component
|
|
943
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="is-authenticated-ssr"
|
|
944
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
945
|
+
*
|
|
946
|
+
* export default async function Home() {
|
|
947
|
+
* const authenticated = await isAuthenticated();
|
|
948
|
+
*
|
|
949
|
+
* return <div>Authenticated: {authenticated.toString()}</div>;
|
|
950
|
+
* }
|
|
951
|
+
* ```
|
|
952
|
+
*
|
|
953
|
+
* @example Server Action
|
|
954
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="is-authenticated-ssr"
|
|
955
|
+
* "use server";
|
|
956
|
+
*
|
|
957
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
958
|
+
*
|
|
959
|
+
* export async function checkAuthAction() {
|
|
960
|
+
* const authenticated = await isAuthenticated();
|
|
961
|
+
*
|
|
962
|
+
* return { authenticated };
|
|
963
|
+
* }
|
|
964
|
+
* ```
|
|
965
|
+
*
|
|
966
|
+
* @example API Handler
|
|
967
|
+
* ```tsx:src/app/api/authenticated/route.ts tab="API Handler" tab-group="is-authenticated-ssr"
|
|
968
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
969
|
+
* import { NextResponse } from "next/server";
|
|
970
|
+
*
|
|
971
|
+
* export const GET = async () => {
|
|
972
|
+
* const authenticated = await isAuthenticated();
|
|
973
|
+
*
|
|
974
|
+
* return NextResponse.json({ authenticated });
|
|
975
|
+
* };
|
|
976
|
+
* ```
|
|
977
|
+
*
|
|
978
|
+
* @example Middleware
|
|
979
|
+
* ```tsx:src/proxy.ts tab="Middleware" tab-group="is-authenticated-ssr"
|
|
980
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
981
|
+
* import { NextResponse } from "next/server";
|
|
982
|
+
*
|
|
983
|
+
* export default async function proxy() {
|
|
984
|
+
* const authenticated = await isAuthenticated();
|
|
985
|
+
*
|
|
986
|
+
* if (!authenticated) {
|
|
987
|
+
* return new NextResponse("User not signed in", { status: 401 });
|
|
988
|
+
* }
|
|
989
|
+
*
|
|
990
|
+
* return NextResponse.next();
|
|
991
|
+
* }
|
|
992
|
+
* ```
|
|
993
|
+
*
|
|
994
|
+
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
995
|
+
*
|
|
996
|
+
* @category Functions
|
|
997
|
+
*/
|
|
998
|
+
declare function isAuthenticated(): Promise<boolean>;
|
|
999
|
+
/**
|
|
1000
|
+
* Checks whether the current user is authenticated using an explicit Web or Next.js request.
|
|
1001
|
+
*
|
|
1002
|
+
* Use this overload when you already have access to a `Request` or `NextRequest` (for example, in Middleware or Route Handlers).
|
|
1003
|
+
*
|
|
1004
|
+
* @example Middleware (Request)
|
|
1005
|
+
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="is-authenticated-route-handler"
|
|
1006
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1007
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1008
|
+
*
|
|
1009
|
+
* export default async function proxy(req: NextRequest) {
|
|
1010
|
+
* const authenticated = await isAuthenticated(req);
|
|
1011
|
+
*
|
|
1012
|
+
* if (!authenticated) {
|
|
1013
|
+
* return new NextResponse("User not signed in", { status: 401 });
|
|
1014
|
+
* }
|
|
1015
|
+
*
|
|
1016
|
+
* return NextResponse.next();
|
|
1017
|
+
* }
|
|
1018
|
+
* ```
|
|
1019
|
+
*
|
|
1020
|
+
* @example Middleware (Response)
|
|
1021
|
+
* ```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="is-authenticated-route-handler"
|
|
1022
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1023
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1024
|
+
*
|
|
1025
|
+
* export default async function proxy(req: NextRequest) {
|
|
1026
|
+
* const res = NextResponse.next();
|
|
1027
|
+
*
|
|
1028
|
+
* const authenticated = await isAuthenticated(req, res);
|
|
1029
|
+
*
|
|
1030
|
+
* if (!authenticated) {
|
|
1031
|
+
* return new NextResponse("User not signed in", { status: 401 });
|
|
1032
|
+
* }
|
|
1033
|
+
*
|
|
1034
|
+
* res.headers.set("x-authenticated", "true");
|
|
1035
|
+
*
|
|
1036
|
+
* return res;
|
|
1037
|
+
* }
|
|
1038
|
+
* ```
|
|
1039
|
+
*
|
|
1040
|
+
* @example API Handler (Request)
|
|
1041
|
+
* ```tsx:src/app/api/authenticated/route.ts tab="API Handler (Request)" tab-group="is-authenticated-route-handler"
|
|
1042
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1043
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1044
|
+
*
|
|
1045
|
+
* export const GET = async (req: NextRequest) => {
|
|
1046
|
+
* const authenticated = await isAuthenticated(req);
|
|
1047
|
+
*
|
|
1048
|
+
* return NextResponse.json({ authenticated });
|
|
1049
|
+
* };
|
|
1050
|
+
* ```
|
|
1051
|
+
*
|
|
1052
|
+
* @example API Handler (Response)
|
|
1053
|
+
* ```tsx:src/app/api/authenticated/route.ts tab="API Handler (Response)" tab-group="is-authenticated-route-handler"
|
|
1054
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1055
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1056
|
+
*
|
|
1057
|
+
* export const GET = async (req: NextRequest) => {
|
|
1058
|
+
* const res = new NextResponse("YOUR CUSTOM RESPONSE");
|
|
1059
|
+
*
|
|
1060
|
+
* const authenticated = await isAuthenticated(req, res);
|
|
1061
|
+
*
|
|
1062
|
+
* if (authenticated) {
|
|
1063
|
+
* res.cookies.set("something", "important");
|
|
1064
|
+
* }
|
|
1065
|
+
*
|
|
1066
|
+
* return res;
|
|
1067
|
+
* };
|
|
1068
|
+
* ```
|
|
1069
|
+
*
|
|
1070
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
1071
|
+
* @param res Optional response to update if refreshed authentication cookies or headers are required.
|
|
1072
|
+
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
1073
|
+
*
|
|
1074
|
+
* @category Functions
|
|
1075
|
+
*/
|
|
1076
|
+
declare function isAuthenticated(req: NextRequest$1 | Request, res?: NextResponse$1 | Response): Promise<boolean>;
|
|
1077
|
+
/**
|
|
1078
|
+
* Checks whether the current user is authenticated in the Pages Router or Node.js runtime.
|
|
1079
|
+
*
|
|
1080
|
+
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
1081
|
+
*
|
|
1082
|
+
* @example Pages Router (Pages)
|
|
1083
|
+
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="is-authenticated-pages"
|
|
1084
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1085
|
+
* import { GetServerSideProps } from "next";
|
|
1086
|
+
*
|
|
1087
|
+
* type Props = {
|
|
1088
|
+
* authenticated: boolean;
|
|
1089
|
+
* };
|
|
1090
|
+
*
|
|
1091
|
+
* export default function Home({ authenticated }: Props) {
|
|
1092
|
+
* return <pre>User is {authenticated ? "logged in" : "guest"}</pre>;
|
|
1093
|
+
* }
|
|
1094
|
+
*
|
|
1095
|
+
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
1096
|
+
* const authenticated = await isAuthenticated(ctx.req, ctx.res);
|
|
1097
|
+
*
|
|
1098
|
+
* return {
|
|
1099
|
+
* props: {
|
|
1100
|
+
* authenticated
|
|
1101
|
+
* }
|
|
1102
|
+
* };
|
|
1103
|
+
* };
|
|
1104
|
+
* ```
|
|
1105
|
+
*
|
|
1106
|
+
* @example Pages Router (API)
|
|
1107
|
+
* ```tsx:src/pages/api/authenticated.ts tab="Pages Router (API)" tab-group="is-authenticated-pages"
|
|
1108
|
+
* import { isAuthenticated } from "@monocloud/auth-nextjs";
|
|
1109
|
+
* import { NextApiRequest, NextApiResponse } from "next";
|
|
1110
|
+
*
|
|
1111
|
+
* export default async function handler(
|
|
1112
|
+
* req: NextApiRequest,
|
|
1113
|
+
* res: NextApiResponse
|
|
1114
|
+
* ) {
|
|
1115
|
+
* const authenticated = await isAuthenticated(req, res);
|
|
1116
|
+
*
|
|
1117
|
+
* res.status(200).json({ authenticated });
|
|
1118
|
+
* }
|
|
1119
|
+
* ```
|
|
1120
|
+
*
|
|
1121
|
+
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
1122
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
1123
|
+
* @returns Returns `true` if a valid session exists; otherwise `false`.
|
|
1124
|
+
*
|
|
1125
|
+
* @category Functions
|
|
1126
|
+
*/
|
|
1127
|
+
declare function isAuthenticated(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>): Promise<boolean>;
|
|
1128
|
+
/**
|
|
1129
|
+
* Ensures the current user is authenticated. If not, redirects to the sign-in flow.
|
|
1130
|
+
*
|
|
1131
|
+
* > **App Router only.** Intended for Server Components, Route Handlers, and Server Actions.
|
|
1132
|
+
*
|
|
1133
|
+
* @example Server Component
|
|
1134
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="protect"
|
|
1135
|
+
* import { protect } from "@monocloud/auth-nextjs";
|
|
1136
|
+
*
|
|
1137
|
+
* export default async function Home() {
|
|
1138
|
+
* await protect();
|
|
1139
|
+
*
|
|
1140
|
+
* return <>You are signed in.</>;
|
|
1141
|
+
* }
|
|
1142
|
+
* ```
|
|
1143
|
+
*
|
|
1144
|
+
* @example Server Action
|
|
1145
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="protect"
|
|
1146
|
+
* "use server";
|
|
1147
|
+
*
|
|
1148
|
+
* import { protect } from "@monocloud/auth-nextjs";
|
|
1149
|
+
*
|
|
1150
|
+
* export async function getMessage() {
|
|
1151
|
+
* await protect();
|
|
1152
|
+
*
|
|
1153
|
+
* return { secret: "sssshhhhh!!!" };
|
|
1154
|
+
* }
|
|
1155
|
+
* ```
|
|
1156
|
+
*
|
|
1157
|
+
* @example API Handler
|
|
1158
|
+
* ```tsx:src/app/api/protected/route.ts tab="API Handler" tab-group="protect"
|
|
1159
|
+
* import { protect } from "@monocloud/auth-nextjs";
|
|
1160
|
+
* import { NextResponse } from "next/server";
|
|
1161
|
+
*
|
|
1162
|
+
* export const GET = async () => {
|
|
1163
|
+
* await protect();
|
|
1164
|
+
*
|
|
1165
|
+
* return NextResponse.json({ secret: "ssshhhh!!!" });
|
|
1166
|
+
* };
|
|
1167
|
+
* ```
|
|
1168
|
+
*
|
|
1169
|
+
* @param options Optional configuration for redirect behavior (for example, return URL or sign-in parameters).
|
|
1170
|
+
* @returns Resolves if the user is authenticated; otherwise triggers a redirect.
|
|
1171
|
+
*
|
|
1172
|
+
* @category Functions
|
|
1173
|
+
*/
|
|
1174
|
+
declare function protect(options?: ProtectOptions): Promise<void>;
|
|
1175
|
+
/**
|
|
1176
|
+
* Wraps an App Router API route handler and ensures that only authenticated (and optionally authorized) requests can access the route.
|
|
1177
|
+
*
|
|
1178
|
+
* Intended for Next.js App Router Route Handlers.
|
|
1179
|
+
*
|
|
1180
|
+
* @example
|
|
1181
|
+
* ```tsx:src/app/api/protected/route.ts
|
|
1182
|
+
* import { protectApi } from "@monocloud/auth-nextjs";
|
|
1183
|
+
* import { NextResponse } from "next/server";
|
|
1184
|
+
*
|
|
1185
|
+
* export const GET = protectApi(async () => {
|
|
1186
|
+
* return NextResponse.json({
|
|
1187
|
+
* message: "You accessed a protected endpoint",
|
|
1188
|
+
* });
|
|
1189
|
+
* });
|
|
1190
|
+
* ```
|
|
1191
|
+
*
|
|
1192
|
+
* @param handler The route handler to protect.
|
|
1193
|
+
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
1194
|
+
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
1195
|
+
*
|
|
1196
|
+
* @category Functions
|
|
1197
|
+
*/
|
|
1198
|
+
declare function protectApi(handler: AppRouterApiHandlerFn, options?: ProtectApiAppOptions): AppRouterApiHandlerFn;
|
|
1199
|
+
/**
|
|
1200
|
+
* Wraps a Pages Router API route handler and ensures that only authenticated (and optionally authorized) requests can access the route.
|
|
1201
|
+
*
|
|
1202
|
+
* Intended for Next.js Pages Router API routes.
|
|
1203
|
+
*
|
|
1204
|
+
* @example
|
|
1205
|
+
* ```tsx:src/pages/api/protected.ts
|
|
1206
|
+
* import { protectApi } from "@monocloud/auth-nextjs";
|
|
1207
|
+
* import { NextApiRequest, NextApiResponse } from "next";
|
|
1208
|
+
*
|
|
1209
|
+
* export default protectApi(
|
|
1210
|
+
* async (req: NextApiRequest, res: NextApiResponse) => {
|
|
1211
|
+
* return res.json({
|
|
1212
|
+
* message: "You accessed a protected endpoint",
|
|
1213
|
+
* });
|
|
1214
|
+
* }
|
|
1215
|
+
* );
|
|
1216
|
+
* ```
|
|
1217
|
+
*
|
|
1218
|
+
* @param handler - The route handler to protect.
|
|
1219
|
+
* @param options Optional configuration controlling authentication and authorization behavior.
|
|
1220
|
+
* @returns Returns a wrapped handler that enforces authentication (and optional authorization) before invoking the original handler.
|
|
1221
|
+
*
|
|
1222
|
+
* @category Functions
|
|
1223
|
+
*/
|
|
1224
|
+
declare function protectApi(handler: NextApiHandler$1, options?: ProtectApiPageOptions): NextApiHandler$1;
|
|
1225
|
+
/**
|
|
1226
|
+
* Restricts access to App Router server-rendered pages.
|
|
1227
|
+
*
|
|
1228
|
+
* **Access control**
|
|
1229
|
+
* - If the user is not authenticated, `onAccessDenied` is invoked (or default behavior applies).
|
|
1230
|
+
* - If the user is authenticated but fails group checks, `onGroupAccessDenied` is invoked (or the default "Access Denied" view is rendered).
|
|
1231
|
+
*
|
|
1232
|
+
* Both behaviors can be customized via options.
|
|
1233
|
+
*
|
|
1234
|
+
* @example Basic Usage
|
|
1235
|
+
* ```tsx:src/app/page.tsx tab="Basic Usage" tab-group="protectPage-app"
|
|
1236
|
+
* import { protectPage } from "@monocloud/auth-nextjs";
|
|
1237
|
+
*
|
|
1238
|
+
* export default protectPage(async function Home({ user }) {
|
|
1239
|
+
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
1240
|
+
* });
|
|
1241
|
+
* ```
|
|
1242
|
+
*
|
|
1243
|
+
* @example With Options
|
|
1244
|
+
* ```tsx:src/app/page.tsx tab="With Options" tab-group="protectPage-app"
|
|
1245
|
+
* import { protectPage } from "@monocloud/auth-nextjs";
|
|
1246
|
+
*
|
|
1247
|
+
* export default protectPage(
|
|
1248
|
+
* async function Home({ user }) {
|
|
1249
|
+
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
1250
|
+
* },
|
|
1251
|
+
* {
|
|
1252
|
+
* returnUrl: "/dashboard",
|
|
1253
|
+
* groups: ["admin"],
|
|
1254
|
+
* }
|
|
1255
|
+
* );
|
|
1256
|
+
* ```
|
|
1257
|
+
*
|
|
1258
|
+
* @param component The App Router server component to protect.
|
|
1259
|
+
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
1260
|
+
* @returns A wrapped page component that enforces authentication before rendering.
|
|
1261
|
+
*
|
|
1262
|
+
* @category Functions
|
|
1263
|
+
*/
|
|
1264
|
+
declare function protectPage(component: ProtectedAppServerComponent, options?: ProtectAppPageOptions): AppRouterPageHandler;
|
|
1265
|
+
/**
|
|
1266
|
+
* Restricts access to Pages Router server-rendered pages using `getServerSideProps`.
|
|
1267
|
+
*
|
|
1268
|
+
* **Access control**
|
|
1269
|
+
* - If the user is not authenticated, `onAccessDenied` is invoked (or default behavior applies).
|
|
1270
|
+
* - 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.
|
|
1271
|
+
*
|
|
1272
|
+
* Both behaviors can be customized via options.
|
|
1273
|
+
*
|
|
1274
|
+
* @example Basic Usage
|
|
1275
|
+
* ```tsx:src/pages/index.tsx tab="Basic Usage" tab-group="protectPage-page"
|
|
1276
|
+
* import { protectPage, MonoCloudUser } from "@monocloud/auth-nextjs";
|
|
1277
|
+
*
|
|
1278
|
+
* type Props = {
|
|
1279
|
+
* user: MonoCloudUser;
|
|
1280
|
+
* };
|
|
1281
|
+
*
|
|
1282
|
+
* export default function Home({ user }: Props) {
|
|
1283
|
+
* return <>Hi {user.email}. You accessed a protected page.</>;
|
|
1284
|
+
* }
|
|
1285
|
+
*
|
|
1286
|
+
* export const getServerSideProps = protectPage();
|
|
1287
|
+
* ```
|
|
1288
|
+
*
|
|
1289
|
+
* @example With Options
|
|
1290
|
+
* ```tsx:src/pages/index.tsx tab="With Options" tab-group="protectPage-page"
|
|
1291
|
+
* import { protectPage, MonoCloudUser } from "@monocloud/auth-nextjs";
|
|
1292
|
+
* import { GetServerSidePropsContext } from "next";
|
|
1293
|
+
*
|
|
1294
|
+
* type Props = {
|
|
1295
|
+
* user: MonoCloudUser;
|
|
1296
|
+
* url: string;
|
|
1297
|
+
* };
|
|
1298
|
+
*
|
|
1299
|
+
* export default function Home({ user, url }: Props) {
|
|
1300
|
+
* console.log(url);
|
|
1301
|
+
* return <div>Hi {user?.email}. You accessed a protected page.</div>;
|
|
1302
|
+
* }
|
|
1303
|
+
*
|
|
1304
|
+
* export const getServerSideProps = protectPage({
|
|
1305
|
+
* returnUrl: "/dashboard",
|
|
1306
|
+
* groups: ["admin"],
|
|
1307
|
+
* getServerSideProps: async (context: GetServerSidePropsContext) => ({
|
|
1308
|
+
* props: { url: context.resolvedUrl }
|
|
1309
|
+
* })
|
|
1310
|
+
* });
|
|
1311
|
+
* ```
|
|
1312
|
+
*
|
|
1313
|
+
* @param options Optional configuration for authentication, authorization, and custom access handling (`onAccessDenied`, `onGroupAccessDenied`).
|
|
1314
|
+
* @typeParam P - Props returned from `getServerSideProps`.
|
|
1315
|
+
* @typeParam Q - Query parameters parsed from the URL.
|
|
1316
|
+
* @returns A getServerSideProps wrapper that enforces authentication before executing the page logic.
|
|
1317
|
+
*
|
|
1318
|
+
* @category Functions
|
|
1319
|
+
*/
|
|
1320
|
+
declare function protectPage<P extends Record<string, any> = Record<string, any>, Q extends ParsedUrlQuery = ParsedUrlQuery>(options?: ProtectPagePageOptions<P, Q>): ProtectPagePageReturnType<P, Q>;
|
|
1321
|
+
/**
|
|
1322
|
+
* Checks whether the currently authenticated user is a member of **any** of the specified groups.
|
|
1323
|
+
*
|
|
1324
|
+
* The `groups` parameter accepts group identifiers (IDs or names).
|
|
1325
|
+
*
|
|
1326
|
+
* The authenticated user's session may contain groups represented as:
|
|
1327
|
+
* - Group IDs
|
|
1328
|
+
* - Group names
|
|
1329
|
+
* - `Group` objects (for example, `{ id: string; name: string }`)
|
|
1330
|
+
*
|
|
1331
|
+
* Matching is always performed against the group's **ID** and **name**, regardless of how the group is represented in the session.
|
|
1332
|
+
*
|
|
1333
|
+
* @example Server Component
|
|
1334
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="is-user-in-group-rsc"
|
|
1335
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1336
|
+
*
|
|
1337
|
+
* export default async function AdminPanel() {
|
|
1338
|
+
* const isAdmin = await isUserInGroup(["admin"]);
|
|
1339
|
+
*
|
|
1340
|
+
* if (!isAdmin) {
|
|
1341
|
+
* return <div>Access Denied</div>;
|
|
1342
|
+
* }
|
|
1343
|
+
*
|
|
1344
|
+
* return <div>Admin Control Panel</div>;
|
|
1345
|
+
* }
|
|
1346
|
+
* ```
|
|
1347
|
+
*
|
|
1348
|
+
* @example Server Action
|
|
1349
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="is-user-in-group-rsc"
|
|
1350
|
+
* "use server";
|
|
1351
|
+
*
|
|
1352
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1353
|
+
*
|
|
1354
|
+
* export async function deletePostAction() {
|
|
1355
|
+
* const canDelete = await isUserInGroup(["admin", "editor"]);
|
|
1356
|
+
*
|
|
1357
|
+
* if (!canDelete) {
|
|
1358
|
+
* return { success: false };
|
|
1359
|
+
* }
|
|
1360
|
+
*
|
|
1361
|
+
* return { success: true };
|
|
1362
|
+
* }
|
|
1363
|
+
* ```
|
|
1364
|
+
*
|
|
1365
|
+
* @example API Handler
|
|
1366
|
+
* ```tsx:src/app/api/group-check/route.ts tab="API Handler" tab-group="is-user-in-group-rsc"
|
|
1367
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1368
|
+
* import { NextResponse } from "next/server";
|
|
1369
|
+
*
|
|
1370
|
+
* export const GET = async () => {
|
|
1371
|
+
* const allowed = await isUserInGroup(["admin", "editor"]);
|
|
1372
|
+
*
|
|
1373
|
+
* if (!allowed) {
|
|
1374
|
+
* return new NextResponse("Forbidden", { status: 403 });
|
|
1375
|
+
* }
|
|
1376
|
+
*
|
|
1377
|
+
* return NextResponse.json({ status: "success" });
|
|
1378
|
+
* };
|
|
1379
|
+
* ```
|
|
1380
|
+
*
|
|
1381
|
+
* @example Middleware
|
|
1382
|
+
* ```tsx:src/proxy.ts tab="Middleware" tab-group="is-user-in-group-rsc"
|
|
1383
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1384
|
+
* import { NextResponse } from "next/server";
|
|
1385
|
+
*
|
|
1386
|
+
* export default async function proxy() {
|
|
1387
|
+
* const isAdmin = await isUserInGroup(["admin"]);
|
|
1388
|
+
*
|
|
1389
|
+
* if (!isAdmin) {
|
|
1390
|
+
* return new NextResponse("User is not admin", { status: 403 });
|
|
1391
|
+
* }
|
|
1392
|
+
*
|
|
1393
|
+
* return NextResponse.next();
|
|
1394
|
+
* }
|
|
1395
|
+
* ```
|
|
1396
|
+
*
|
|
1397
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1398
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1399
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1400
|
+
*
|
|
1401
|
+
* @category Functions
|
|
1402
|
+
*/
|
|
1403
|
+
declare function isUserInGroup(groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1404
|
+
/**
|
|
1405
|
+
* Checks group membership using an explicit Web or Next.js request.
|
|
1406
|
+
*
|
|
1407
|
+
* Use this overload when you already have access to a `Request` or `NextRequest` (for example, in Middleware or Route Handlers).
|
|
1408
|
+
*
|
|
1409
|
+
* @example Middleware (Request)
|
|
1410
|
+
* ```tsx:src/proxy.ts tab="Middleware (Request)" tab-group="is-user-in-group-request"
|
|
1411
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1412
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1413
|
+
*
|
|
1414
|
+
* export default async function proxy(req: NextRequest) {
|
|
1415
|
+
* const isAdmin = await isUserInGroup(req, ["admin"]);
|
|
1416
|
+
*
|
|
1417
|
+
* if (!isAdmin) {
|
|
1418
|
+
* return new NextResponse("User is not admin", { status: 403 });
|
|
1419
|
+
* }
|
|
1420
|
+
*
|
|
1421
|
+
* return NextResponse.next();
|
|
1422
|
+
* }
|
|
1423
|
+
* ```
|
|
1424
|
+
*
|
|
1425
|
+
* @example API Handler (Request)
|
|
1426
|
+
* ```tsx:src/app/api/group-check/route.ts tab="API Handler (Request)" tab-group="is-user-in-group-request"
|
|
1427
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1428
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1429
|
+
*
|
|
1430
|
+
* export const GET = async (req: NextRequest) => {
|
|
1431
|
+
* const isMember = await isUserInGroup(req, ["admin", "editor"]);
|
|
1432
|
+
*
|
|
1433
|
+
* return NextResponse.json({ isMember });
|
|
1434
|
+
* };
|
|
1435
|
+
* ```
|
|
1436
|
+
*
|
|
1437
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
1438
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1439
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1440
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1441
|
+
*
|
|
1442
|
+
* @category Functions
|
|
1443
|
+
*/
|
|
1444
|
+
declare function isUserInGroup(req: NextRequest$1 | Request, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1445
|
+
/**
|
|
1446
|
+
* Checks group membership using an explicit request and response.
|
|
1447
|
+
*
|
|
1448
|
+
* Use this overload when you have already created a response and want refreshed authentication cookies or headers applied to it.
|
|
1449
|
+
*
|
|
1450
|
+
* @example Middleware (Response)
|
|
1451
|
+
* ```tsx:src/proxy.ts tab="Middleware (Response)" tab-group="is-user-in-group-response"
|
|
1452
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1453
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1454
|
+
*
|
|
1455
|
+
* export default async function proxy(req: NextRequest) {
|
|
1456
|
+
* const res = NextResponse.next();
|
|
1457
|
+
*
|
|
1458
|
+
* const isAdmin = await isUserInGroup(req, res, ["admin"]);
|
|
1459
|
+
*
|
|
1460
|
+
* if (!isAdmin) {
|
|
1461
|
+
* return new NextResponse("User is not admin", { status: 403 });
|
|
1462
|
+
* }
|
|
1463
|
+
*
|
|
1464
|
+
* res.headers.set("x-user", "admin");
|
|
1465
|
+
*
|
|
1466
|
+
* return res;
|
|
1467
|
+
* }
|
|
1468
|
+
* ```
|
|
1469
|
+
*
|
|
1470
|
+
* @example API Handler (Response)
|
|
1471
|
+
* ```tsx:src/app/api/group-check/route.ts tab="API Handler (Response)" tab-group="is-user-in-group-response"
|
|
1472
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1473
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
1474
|
+
*
|
|
1475
|
+
* export const GET = async (req: NextRequest) => {
|
|
1476
|
+
* const res = new NextResponse("Restricted Content");
|
|
1477
|
+
*
|
|
1478
|
+
* const allowed = await isUserInGroup(req, res, ["admin"]);
|
|
1479
|
+
*
|
|
1480
|
+
* if (!allowed) {
|
|
1481
|
+
* return new NextResponse("Not Allowed", res);
|
|
1482
|
+
* }
|
|
1483
|
+
*
|
|
1484
|
+
* return res;
|
|
1485
|
+
* };
|
|
1486
|
+
* ```
|
|
1487
|
+
*
|
|
1488
|
+
* @param req Incoming request used to resolve authentication from cookies and headers.
|
|
1489
|
+
* @param res Existing response to update with refreshed authentication cookies or headers when required.
|
|
1490
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1491
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1492
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1493
|
+
*
|
|
1494
|
+
* @category Functions
|
|
1495
|
+
*/
|
|
1496
|
+
declare function isUserInGroup(req: NextRequest$1 | Request, res: NextResponse$1 | Response, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1497
|
+
/**
|
|
1498
|
+
* Checks group membership in the Pages Router or Node.js runtime.
|
|
1499
|
+
*
|
|
1500
|
+
* Use this overload in API routes or `getServerSideProps`, where Node.js request and response objects are available.
|
|
1501
|
+
*
|
|
1502
|
+
* @example Pages Router (Pages)
|
|
1503
|
+
* ```tsx:src/pages/index.tsx tab="Pages Router (Pages)" tab-group="is-user-in-group-pages"
|
|
1504
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1505
|
+
* import { GetServerSideProps } from "next";
|
|
1506
|
+
*
|
|
1507
|
+
* type Props = {
|
|
1508
|
+
* isAdmin: boolean;
|
|
1509
|
+
* };
|
|
1510
|
+
*
|
|
1511
|
+
* export default function Home({ isAdmin }: Props) {
|
|
1512
|
+
* return <div>User is admin: {isAdmin.toString()}</div>;
|
|
1513
|
+
* }
|
|
1514
|
+
*
|
|
1515
|
+
* export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
|
|
1516
|
+
* const isAdmin = await isUserInGroup(ctx.req, ctx.res, ["admin"]);
|
|
1517
|
+
*
|
|
1518
|
+
* return {
|
|
1519
|
+
* props: {
|
|
1520
|
+
* isAdmin
|
|
1521
|
+
* }
|
|
1522
|
+
* };
|
|
1523
|
+
* };
|
|
1524
|
+
* ```
|
|
1525
|
+
*
|
|
1526
|
+
* @example Pages Router (API)
|
|
1527
|
+
* ```tsx:src/pages/api/group-check.ts tab="Pages Router (API)" tab-group="is-user-in-group-pages"
|
|
1528
|
+
* import { isUserInGroup } from "@monocloud/auth-nextjs";
|
|
1529
|
+
* import { NextApiRequest, NextApiResponse } from "next";
|
|
1530
|
+
*
|
|
1531
|
+
* export default async function handler(
|
|
1532
|
+
* req: NextApiRequest,
|
|
1533
|
+
* res: NextApiResponse
|
|
1534
|
+
* ) {
|
|
1535
|
+
* const isAdmin = await isUserInGroup(req, res, ["admin"]);
|
|
1536
|
+
*
|
|
1537
|
+
* if (!isAdmin) {
|
|
1538
|
+
* return res.status(403).json({ error: "Forbidden" });
|
|
1539
|
+
* }
|
|
1540
|
+
*
|
|
1541
|
+
* res.status(200).json({ message: "Welcome Admin" });
|
|
1542
|
+
* }
|
|
1543
|
+
* ```
|
|
1544
|
+
*
|
|
1545
|
+
* @param req Incoming Node.js request used to resolve authentication from cookies.
|
|
1546
|
+
* @param res Outgoing Node.js response used to apply refreshed authentication cookies when required.
|
|
1547
|
+
* @param groups Group IDs or names to check against the user's group memberships.
|
|
1548
|
+
* @param options Optional configuration controlling how group membership is evaluated.
|
|
1549
|
+
* @returns Returns `true` if the user belongs to at least one specified group; otherwise `false`.
|
|
1550
|
+
*
|
|
1551
|
+
* @category Functions
|
|
1552
|
+
*/
|
|
1553
|
+
declare function isUserInGroup(req: NextApiRequest$1 | IncomingMessage, res: NextApiResponse$1 | ServerResponse<IncomingMessage>, groups: string[], options?: IsUserInGroupOptions): Promise<boolean>;
|
|
1554
|
+
/**
|
|
1555
|
+
* Redirects the user to the sign-in flow.
|
|
1556
|
+
*
|
|
1557
|
+
* > **App Router only**. Intended for use in Server Components, Route Handlers, and Server Actions.
|
|
1558
|
+
*
|
|
1559
|
+
* This helper performs a server-side redirect to the configured sign-in route. Execution does not continue after the redirect is triggered.
|
|
1560
|
+
*
|
|
1561
|
+
* @example Server Component
|
|
1562
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="redirect-to-sign-in"
|
|
1563
|
+
* import { isUserInGroup, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1564
|
+
*
|
|
1565
|
+
* export default async function Home() {
|
|
1566
|
+
* const allowed = await isUserInGroup(["admin"]);
|
|
1567
|
+
*
|
|
1568
|
+
* if (!allowed) {
|
|
1569
|
+
* await redirectToSignIn({ returnUrl: "/home" });
|
|
1570
|
+
* }
|
|
1571
|
+
*
|
|
1572
|
+
* return <>You are signed in.</>;
|
|
1573
|
+
* }
|
|
1574
|
+
* ```
|
|
1575
|
+
*
|
|
1576
|
+
* @example Server Action
|
|
1577
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="redirect-to-sign-in"
|
|
1578
|
+
* "use server";
|
|
1579
|
+
*
|
|
1580
|
+
* import { getSession, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1581
|
+
*
|
|
1582
|
+
* export async function protectedAction() {
|
|
1583
|
+
* const session = await getSession();
|
|
1584
|
+
*
|
|
1585
|
+
* if (!session) {
|
|
1586
|
+
* await redirectToSignIn();
|
|
1587
|
+
* }
|
|
1588
|
+
*
|
|
1589
|
+
* return { data: "Sensitive Data" };
|
|
1590
|
+
* }
|
|
1591
|
+
* ```
|
|
1592
|
+
*
|
|
1593
|
+
* @example API Handler
|
|
1594
|
+
* ```tsx:src/app/api/protected/route.ts tab="API Handler" tab-group="redirect-to-sign-in"
|
|
1595
|
+
* import { getSession, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1596
|
+
* import { NextResponse } from "next/server";
|
|
1597
|
+
*
|
|
1598
|
+
* export const GET = async () => {
|
|
1599
|
+
* const session = await getSession();
|
|
1600
|
+
*
|
|
1601
|
+
* if (!session) {
|
|
1602
|
+
* await redirectToSignIn({
|
|
1603
|
+
* returnUrl: "/dashboard",
|
|
1604
|
+
* });
|
|
1605
|
+
* }
|
|
1606
|
+
*
|
|
1607
|
+
* return NextResponse.json({ data: "Protected content" });
|
|
1608
|
+
* };
|
|
1609
|
+
* ```
|
|
1610
|
+
*
|
|
1611
|
+
* @param options Optional configuration for the redirect, such as `returnUrl` or additional sign-in parameters.
|
|
1612
|
+
* @returns Never resolves. Triggers a redirect to the sign-in flow.
|
|
1613
|
+
*
|
|
1614
|
+
* @category Functions
|
|
1615
|
+
*/
|
|
1616
|
+
declare function redirectToSignIn(options?: RedirectToSignInOptions): Promise<void>;
|
|
1617
|
+
/**
|
|
1618
|
+
* Redirects the user to the sign-out flow.
|
|
1619
|
+
*
|
|
1620
|
+
* > **App Router only**. Intended for use in Server Components, Route Handlers, and Server Actions.
|
|
1621
|
+
*
|
|
1622
|
+
* This helper performs a server-side redirect to the configured sign-out route. Execution does not continue after the redirect is triggered.
|
|
1623
|
+
*
|
|
1624
|
+
* @example Server Component
|
|
1625
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="redirect-to-sign-out"
|
|
1626
|
+
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1627
|
+
*
|
|
1628
|
+
* export default async function Page() {
|
|
1629
|
+
* const session = await getSession();
|
|
1630
|
+
*
|
|
1631
|
+
* // Example: Force sign-out if a specific condition is met (e.g., account suspended)
|
|
1632
|
+
* if (session?.user.isSuspended) {
|
|
1633
|
+
* await redirectToSignOut();
|
|
1634
|
+
* }
|
|
1635
|
+
*
|
|
1636
|
+
* return <>Welcome User</>;
|
|
1637
|
+
* }
|
|
1638
|
+
* ```
|
|
1639
|
+
*
|
|
1640
|
+
* @example Server Action
|
|
1641
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="redirect-to-sign-out"
|
|
1642
|
+
* "use server";
|
|
1643
|
+
*
|
|
1644
|
+
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1645
|
+
*
|
|
1646
|
+
* export async function signOutAction() {
|
|
1647
|
+
* const session = await getSession();
|
|
1648
|
+
*
|
|
1649
|
+
* if (session) {
|
|
1650
|
+
* await redirectToSignOut();
|
|
1651
|
+
* }
|
|
1652
|
+
* }
|
|
1653
|
+
* ```
|
|
1654
|
+
*
|
|
1655
|
+
* @example API Handler
|
|
1656
|
+
* ```tsx:src/app/api/signout/route.ts tab="API Handler" tab-group="redirect-to-sign-out"
|
|
1657
|
+
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1658
|
+
* import { NextResponse } from "next/server";
|
|
1659
|
+
*
|
|
1660
|
+
* export const GET = async () => {
|
|
1661
|
+
* const session = await getSession();
|
|
1662
|
+
*
|
|
1663
|
+
* if (session) {
|
|
1664
|
+
* await redirectToSignOut({
|
|
1665
|
+
* postLogoutRedirectUri: "/goodbye",
|
|
1666
|
+
* });
|
|
1667
|
+
* }
|
|
1668
|
+
*
|
|
1669
|
+
* return NextResponse.json({ status: "already_signed_out" });
|
|
1670
|
+
* };
|
|
1671
|
+
* ```
|
|
1672
|
+
*
|
|
1673
|
+
* @param options Optional configuration for the redirect, such as `postLogoutRedirectUri` or additional sign-out parameters.
|
|
1674
|
+
* @returns Never resolves. Triggers a redirect to the sign-out flow.
|
|
1675
|
+
*
|
|
1676
|
+
* @category Functions
|
|
1677
|
+
*/
|
|
1678
|
+
declare function redirectToSignOut(options?: RedirectToSignOutOptions): Promise<void>;
|
|
1679
|
+
//#endregion
|
|
1680
|
+
export { type AccessToken, type Address, type AppOnError, type AppRouterApiHandlerFn, type AppRouterApiOnAccessDeniedHandler, type AppRouterApiOnGroupAccessDeniedHandler, type AppRouterContext, type AppRouterPageHandler, type ApplicationState, type Authenticators, type AuthorizationParams, type CodeChallengeMethod, type CustomProtectedRouteMatcher, type DisplayOptions, type ExtraAuthParams, type GetTokensOptions, type Group, type GroupOptions, type IdTokenClaims, type Indicator, type IsUserInGroupOptions, type Jwk, MonoCloudAuthBaseError, type MonoCloudAuthHandler, type MonoCloudAuthOptions, type MonoCloudCookieOptions, MonoCloudHttpError, type MonoCloudMiddlewareOptions, MonoCloudNextClient, MonoCloudOPError, type MonoCloudOptions, type MonoCloudRequest, type MonoCloudRoutes, type MonoCloudSession, type MonoCloudSessionOptions, type MonoCloudSessionOptionsBase, type MonoCloudSessionStore, type MonoCloudStateOptions, type MonoCloudStatePartialOptions, MonoCloudTokenError, type MonoCloudTokens, type MonoCloudUser, MonoCloudValidationError, type NextMiddlewareOnAccessDenied, type NextMiddlewareOnGroupAccessDenied, type NextMiddlewareResult, type OnBackChannelLogout, type OnError, type OnSessionCreating, type OnSetApplicationState, type PageOnError, type PageRouterApiOnAccessDeniedHandler, type PageRouterApiOnGroupAccessDeniedHandler, type Prompt, type ProtectApiAppOptions, type ProtectApiPageOptions, type ProtectAppPageOptions, type ProtectOptions, type ProtectPageGetServerSidePropsContext, type ProtectPagePageOnAccessDeniedType, type ProtectPagePageOnGroupAccessDeniedType, type ProtectPagePageOptions, type ProtectPagePageReturnType, type ProtectedAppServerComponent, type ProtectedAppServerComponentProps, type ProtectedRouteMatcher, type ProtectedRoutes, type RedirectToSignInOptions, type RedirectToSignOutOptions, type ResponseModes, type ResponseTypes, type SameSiteValues, type SecurityAlgorithms, type SessionLifetime, type UserinfoResponse, authMiddleware, getSession, getTokens, isAuthenticated, isUserInGroup, monoCloudAuth, protect, protectApi, protectPage, redirectToSignIn, redirectToSignOut };
|
|
1969
1681
|
//# sourceMappingURL=index.d.mts.map
|