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