@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.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MonoCloudAuthBaseError, MonoCloudCoreClient, MonoCloudHttpError, MonoCloudOPError, MonoCloudTokenError, MonoCloudValidationError, MonoCloudValidationError as MonoCloudValidationError$1 } from "@monocloud/auth-node-core";
|
|
2
2
|
import { NextRequest, NextResponse } from "next/server.js";
|
|
3
3
|
import { ensureLeadingSlash, isAbsoluteUrl, isPresent } from "@monocloud/auth-node-core/internal";
|
|
4
|
-
import { isUserInGroup } from "@monocloud/auth-node-core/utils";
|
|
4
|
+
import { isUserInGroup as isUserInGroup$1 } from "@monocloud/auth-node-core/utils";
|
|
5
5
|
import { serialize } from "cookie";
|
|
6
6
|
|
|
7
7
|
//#region src/requests/monocloud-app-router-request.ts
|
|
@@ -284,35 +284,32 @@ const mergeResponse = (responses) => {
|
|
|
284
284
|
//#endregion
|
|
285
285
|
//#region src/monocloud-next-client.ts
|
|
286
286
|
/**
|
|
287
|
-
*
|
|
287
|
+
* `MonoCloudNextClient` is the core SDK entry point for integrating MonoCloud authentication into a Next.js application.
|
|
288
288
|
*
|
|
289
|
-
*
|
|
289
|
+
* It provides:
|
|
290
|
+
* - Authentication middleware
|
|
291
|
+
* - Route protection helpers
|
|
292
|
+
* - Session and token access
|
|
293
|
+
* - Redirect utilities
|
|
294
|
+
* - Server-side enforcement helpers
|
|
290
295
|
*
|
|
291
|
-
* 1. Add
|
|
296
|
+
* ## 1. Add environment variables
|
|
292
297
|
*
|
|
293
|
-
* ```bash
|
|
298
|
+
* ```bash:.env.local
|
|
294
299
|
* MONOCLOUD_AUTH_TENANT_DOMAIN=<tenant-domain>
|
|
295
300
|
* MONOCLOUD_AUTH_CLIENT_ID=<client-id>
|
|
296
301
|
* MONOCLOUD_AUTH_CLIENT_SECRET=<client-secret>
|
|
297
|
-
* MONOCLOUD_AUTH_SCOPES=openid profile email
|
|
302
|
+
* MONOCLOUD_AUTH_SCOPES=openid profile email
|
|
298
303
|
* MONOCLOUD_AUTH_APP_URL=http://localhost:3000
|
|
299
304
|
* MONOCLOUD_AUTH_COOKIE_SECRET=<cookie-secret>
|
|
300
305
|
* ```
|
|
301
306
|
*
|
|
302
|
-
* 2.
|
|
307
|
+
* ## 2. Register middleware
|
|
303
308
|
*
|
|
304
|
-
* ```typescript
|
|
305
|
-
* import {
|
|
309
|
+
* ```typescript:src/proxy.ts
|
|
310
|
+
* import { authMiddleware } from "@monocloud/auth-nextjs";
|
|
306
311
|
*
|
|
307
|
-
* export
|
|
308
|
-
* ```
|
|
309
|
-
*
|
|
310
|
-
* 3. Add MonoCloud middleware/proxy
|
|
311
|
-
*
|
|
312
|
-
* ```typescript
|
|
313
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
314
|
-
*
|
|
315
|
-
* export default monoCloud.authMiddleware();
|
|
312
|
+
* export default authMiddleware();
|
|
316
313
|
*
|
|
317
314
|
* export const config = {
|
|
318
315
|
* matcher: [
|
|
@@ -321,218 +318,115 @@ const mergeResponse = (responses) => {
|
|
|
321
318
|
* };
|
|
322
319
|
* ```
|
|
323
320
|
*
|
|
324
|
-
*
|
|
321
|
+
* ## Advanced usage
|
|
322
|
+
*
|
|
323
|
+
* ### Create a shared client instance
|
|
324
|
+
*
|
|
325
|
+
* By default, the SDK exposes function exports (for example, `authMiddleware()`, `getSession()`, `getTokens()`) that internally use a shared singleton `MonoCloudNextClient`.
|
|
326
|
+
*
|
|
327
|
+
* Create your own `MonoCloudNextClient` instance when you need multiple configurations, dependency injection, or explicit control over initialization.
|
|
328
|
+
*
|
|
329
|
+
* ```ts:src/monocloud.ts
|
|
330
|
+
* import { MonoCloudNextClient } from "@monocloud/auth-nextjs";
|
|
331
|
+
*
|
|
332
|
+
* export const monoCloud = new MonoCloudNextClient();
|
|
333
|
+
* ```
|
|
334
|
+
*
|
|
335
|
+
* ### Using instance methods
|
|
325
336
|
*
|
|
326
|
-
*
|
|
337
|
+
* Once you create a client instance, call methods directly on it instead of using the default function exports.
|
|
327
338
|
*
|
|
328
|
-
*
|
|
339
|
+
* ```ts:src/app/page.tsx
|
|
340
|
+
* import { monoCloud } from "@/monocloud";
|
|
329
341
|
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
342
|
+
* export default async function Page() {
|
|
343
|
+
* const session = await monoCloud.getSession();
|
|
344
|
+
*
|
|
345
|
+
* if (!session) {
|
|
346
|
+
* return <>Not signed in</>;
|
|
347
|
+
* }
|
|
348
|
+
*
|
|
349
|
+
* return <>Hello {session.user.name}</>;
|
|
350
|
+
* }
|
|
351
|
+
* ```
|
|
352
|
+
*
|
|
353
|
+
* #### Using constructor options
|
|
354
|
+
*
|
|
355
|
+
* 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.
|
|
356
|
+
*
|
|
357
|
+
* ```ts:src/monocloud.ts
|
|
358
|
+
* import { MonoCloudNextClient } from "@monocloud/auth-nextjs";
|
|
332
359
|
*
|
|
333
360
|
* export const monoCloud = new MonoCloudNextClient({
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
361
|
+
* tenantDomain: "<tenant-domain>",
|
|
362
|
+
* clientId: "<client-id>",
|
|
363
|
+
* clientSecret: "<client-secret>",
|
|
364
|
+
* appUrl: "http://localhost:3000",
|
|
365
|
+
* cookieSecret: "<cookie-secret>",
|
|
366
|
+
* defaultAuthParams: {
|
|
367
|
+
* scopes: "openid profile email",
|
|
368
|
+
* },
|
|
340
369
|
* });
|
|
341
370
|
* ```
|
|
342
|
-
* 2. Add MonoCloud middleware/proxy
|
|
343
371
|
*
|
|
344
|
-
*
|
|
345
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
372
|
+
* ### Modifying default routes
|
|
346
373
|
*
|
|
347
|
-
*
|
|
374
|
+
* If you customize any of the default auth route paths:
|
|
348
375
|
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
376
|
+
* - Also set the corresponding `NEXT_PUBLIC_` environment variables so client-side helpers
|
|
377
|
+
* (for example `<SignIn />`, `<SignOut />`, and `useAuth()`) can discover the correct URLs.
|
|
378
|
+
* - Update the **Application URLs** in your MonoCloud Dashboard to match the new paths.
|
|
379
|
+
*
|
|
380
|
+
* Example:
|
|
381
|
+
*
|
|
382
|
+
* ```bash:.env.local
|
|
383
|
+
* MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
384
|
+
* NEXT_PUBLIC_MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
354
385
|
* ```
|
|
355
386
|
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
* <h4>Core Configuration (Required)</h4>
|
|
359
|
-
*
|
|
360
|
-
* <ul>
|
|
361
|
-
* <li><strong>MONOCLOUD_AUTH_CLIENT_ID : </strong>Unique identifier for your application/client.</li>
|
|
362
|
-
* <li><strong>MONOCLOUD_AUTH_CLIENT_SECRET : </strong>Application/client secret.</li>
|
|
363
|
-
* <li><strong>MONOCLOUD_AUTH_TENANT_DOMAIN : </strong>The domain of your MonoCloud tenant (e.g., https://your-tenant.us.monocloud.com).</li>
|
|
364
|
-
* <li><strong>MONOCLOUD_AUTH_APP_URL : </strong>The base URL where your application is hosted.</li>
|
|
365
|
-
* <li><strong>MONOCLOUD_AUTH_COOKIE_SECRET : </strong>A long, random string used to encrypt and sign session cookies.</li>
|
|
366
|
-
* </ul>
|
|
367
|
-
*
|
|
368
|
-
* <h4>Authentication & Security</h4>
|
|
369
|
-
*
|
|
370
|
-
* <ul>
|
|
371
|
-
* <li><strong>MONOCLOUD_AUTH_SCOPES : </strong>A space-separated list of OIDC scopes to request (e.g., openid profile email).</li>
|
|
372
|
-
* <li><strong>MONOCLOUD_AUTH_RESOURCE : </strong>The default resource/audience identifier for access tokens.</li>
|
|
373
|
-
* <li><strong>MONOCLOUD_AUTH_USE_PAR : </strong>Enables Pushed Authorization Requests.</li>
|
|
374
|
-
* <li><strong>MONOCLOUD_AUTH_CLOCK_SKEW : </strong>The allowed clock drift in seconds when validating token timestamps.</li>
|
|
375
|
-
* <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>
|
|
376
|
-
* <li><strong>MONOCLOUD_AUTH_RESPONSE_TIMEOUT : </strong>The maximum time in milliseconds to wait for a response.</li>
|
|
377
|
-
* <li><strong>MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES : </strong>Allows dynamic overrides of auth parameters via URL query strings.</li>
|
|
378
|
-
* <li><strong>MONOCLOUD_AUTH_POST_LOGOUT_REDIRECT_URI : </strong>The URL users are sent to after a successful logout.</li>
|
|
379
|
-
* <li><strong>MONOCLOUD_AUTH_USER_INFO : </strong>Determines if user profile data from the UserInfo endpoint should be fetched after authorization code exchange.</li>
|
|
380
|
-
* <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>
|
|
381
|
-
* <li><strong>MONOCLOUD_AUTH_ID_TOKEN_SIGNING_ALG : </strong>The expected algorithm for signing ID tokens (e.g., RS256).</li>
|
|
382
|
-
* <li><strong>MONOCLOUD_AUTH_FILTERED_ID_TOKEN_CLAIMS : </strong>A space-separated list of claims to exclude from the session object.</li>
|
|
383
|
-
* </ul>
|
|
384
|
-
*
|
|
385
|
-
* <h4>Routes</h4>
|
|
386
|
-
*
|
|
387
|
-
* <aside>
|
|
388
|
-
* <strong>⚠️ Important: Modifying Default Routes</strong>
|
|
389
|
-
* <p>If you choose to customize any of the default route paths, you must adhere to the following requirements:</p>
|
|
390
|
-
* <ul>
|
|
391
|
-
* <li>
|
|
392
|
-
* <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.
|
|
393
|
-
* </li>
|
|
394
|
-
* <li>
|
|
395
|
-
* <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.
|
|
396
|
-
* </li>
|
|
397
|
-
* </ul>
|
|
398
|
-
* <p><em>Example:</em></p>
|
|
399
|
-
* <code>
|
|
400
|
-
* MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback<br />
|
|
401
|
-
* NEXT_PUBLIC_MONOCLOUD_AUTH_CALLBACK_URL=/api/custom_callback
|
|
402
|
-
* </code>
|
|
403
|
-
* <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>
|
|
404
|
-
* </aside>
|
|
405
|
-
*
|
|
406
|
-
* <ul>
|
|
407
|
-
* <li><strong>MONOCLOUD_AUTH_CALLBACK_URL : </strong>The application path where MonoCloud sends the user after authentication.</li>
|
|
408
|
-
* <li><strong>MONOCLOUD_AUTH_SIGNIN_URL : </strong>The internal route path to trigger the sign-in.</li>
|
|
409
|
-
* <li><strong>MONOCLOUD_AUTH_SIGNOUT_URL : </strong>The internal route path to trigger the sign-out.</li>
|
|
410
|
-
* <li><strong>MONOCLOUD_AUTH_USER_INFO_URL : </strong>The route that exposes the current user's profile from userinfo endpoint.</li>
|
|
411
|
-
* </ul>
|
|
412
|
-
*
|
|
413
|
-
* <h4>Session Cookie Settings</h4>
|
|
414
|
-
*
|
|
415
|
-
* <ul>
|
|
416
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_NAME : </strong>The name of the cookie used to store the user session.</li>
|
|
417
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_PATH : </strong>The scope path for the session cookie.</li>
|
|
418
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_DOMAIN : </strong>The domain scope for the session cookie.</li>
|
|
419
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_HTTP_ONLY : </strong>Prevents client-side scripts from accessing the session cookie.</li>
|
|
420
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_SECURE : </strong>Ensures the session cookie is only sent over HTTPS.</li>
|
|
421
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_SAME_SITE : </strong>The SameSite policy for the session cookie (Lax, Strict, or None).</li>
|
|
422
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_COOKIE_PERSISTENT : </strong>If true, the session survives browser restarts.</li>
|
|
423
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_SLIDING : </strong>If true, the session will be a sliding session instead of absolute.</li>
|
|
424
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_DURATION : </strong>The session lifetime in seconds.</li>
|
|
425
|
-
* <li><strong>MONOCLOUD_AUTH_SESSION_MAX_DURATION : </strong>The absolute maximum lifetime of a session in seconds.</li>
|
|
426
|
-
* </ul>
|
|
427
|
-
*
|
|
428
|
-
* <h4>State Cookie Settings</h4>
|
|
429
|
-
*
|
|
430
|
-
* <ul>
|
|
431
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_NAME : </strong>The name of the cookie used to store OpenID state/nonce.</li>
|
|
432
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_PATH : </strong>The scope path for the state cookie.</li>
|
|
433
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_DOMAIN : </strong>The domain scope for the state cookie.</li>
|
|
434
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_SECURE : </strong>Ensures the state cookie is only sent over HTTPS</li>
|
|
435
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_SAME_SITE : </strong>The SameSite policy for the state cookie.</li>
|
|
436
|
-
* <li><strong>MONOCLOUD_AUTH_STATE_COOKIE_PERSISTENT : </strong>Whether the state cookie is persistent.</li>
|
|
437
|
-
* </ul>
|
|
438
|
-
*
|
|
439
|
-
* <h4>Caching</h4>
|
|
440
|
-
*
|
|
441
|
-
* <ul>
|
|
442
|
-
* <li><strong>MONOCLOUD_AUTH_JWKS_CACHE_DURATION : </strong>Duration in seconds to cache the JSON Web Key Set.</li>
|
|
443
|
-
* <li><strong>MONOCLOUD_AUTH_METADATA_CACHE_DURATION : </strong>Duration in seconds to cache the OpenID discovery metadata.</li>
|
|
444
|
-
* </ul>
|
|
445
|
-
* </details>
|
|
387
|
+
* When routes are overridden, the Redirect URI configured in the dashboard
|
|
388
|
+
* must reflect the new path. For example, during local development:
|
|
446
389
|
*
|
|
390
|
+
* `http://localhost:3000/api/custom_callback`
|
|
447
391
|
*
|
|
392
|
+
* @category Classes
|
|
448
393
|
*/
|
|
449
394
|
var MonoCloudNextClient = class {
|
|
450
395
|
/**
|
|
451
|
-
*
|
|
396
|
+
* This exposes the framework-agnostic MonoCloud client used internally by the Next.js SDK.
|
|
397
|
+
* Use it if you need access to lower-level functionality not directly exposed by MonoCloudNextClient.
|
|
452
398
|
*
|
|
453
|
-
* @
|
|
454
|
-
|
|
455
|
-
|
|
399
|
+
* @returns Returns the underlying **Node client** instance.
|
|
400
|
+
*/
|
|
401
|
+
get coreClient() {
|
|
402
|
+
return this._coreClient;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* This is intended for advanced scenarios requiring direct control over the authorization or token flow.
|
|
406
|
+
*
|
|
407
|
+
* @returns Returns the underlying **OIDC client** used for OpenID Connect operations.
|
|
456
408
|
*/
|
|
457
409
|
get oidcClient() {
|
|
458
410
|
return this.coreClient.oidcClient;
|
|
459
411
|
}
|
|
460
412
|
/**
|
|
461
|
-
*
|
|
413
|
+
* Creates a new client instance.
|
|
414
|
+
*
|
|
415
|
+
* @param options Optional configuration for initializing the MonoCloud client. If not provided, settings are automatically resolved from environment variables.
|
|
462
416
|
*/
|
|
463
417
|
constructor(options) {
|
|
464
418
|
const opt = {
|
|
465
419
|
...options ?? {},
|
|
466
|
-
userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.
|
|
420
|
+
userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.8`,
|
|
467
421
|
debugger: (options === null || options === void 0 ? void 0 : options.debugger) ?? "@monocloud:auth-nextjs"
|
|
468
422
|
};
|
|
469
423
|
this.registerPublicEnvVariables();
|
|
470
|
-
this.
|
|
424
|
+
this._coreClient = new MonoCloudCoreClient(opt);
|
|
471
425
|
}
|
|
472
426
|
/**
|
|
473
|
-
*
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
* @param options Authentication configuration routes.
|
|
478
|
-
*
|
|
479
|
-
* **Note:** If you are already using `authMiddleware()`, you typically do **not**
|
|
480
|
-
* need this API route handler. This function is intended for applications where
|
|
481
|
-
* middleware cannot be used—such as statically generated (SSG) deployments that still
|
|
482
|
-
* require server-side authentication flows.
|
|
483
|
-
*
|
|
484
|
-
* @example App Router
|
|
485
|
-
*
|
|
486
|
-
* ```typescript
|
|
487
|
-
* // app/api/auth/[...monocloud]/route.ts
|
|
488
|
-
*
|
|
489
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
490
|
-
*
|
|
491
|
-
* export const GET = monoCloud.monoCloudAuth();
|
|
492
|
-
*```
|
|
493
|
-
*
|
|
494
|
-
* @example App Router with Response
|
|
495
|
-
*
|
|
496
|
-
* ```typescript
|
|
497
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
498
|
-
* import { NextRequest, NextResponse } from "next/server";
|
|
499
|
-
*
|
|
500
|
-
* export const GET = (req: NextRequest) => {
|
|
501
|
-
* const authHandler = monoCloud.monoCloudAuth();
|
|
502
|
-
*
|
|
503
|
-
* const res = new NextResponse();
|
|
504
|
-
*
|
|
505
|
-
* res.cookies.set("last_auth_requested", `${Date.now()}`);
|
|
506
|
-
*
|
|
507
|
-
* return authHandler(req, res);
|
|
508
|
-
* };
|
|
509
|
-
* ```
|
|
510
|
-
*
|
|
511
|
-
* @example Pages Router
|
|
512
|
-
*
|
|
513
|
-
* ```typescript
|
|
514
|
-
* // pages/api/auth/[...monocloud].ts
|
|
515
|
-
*
|
|
516
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
517
|
-
*
|
|
518
|
-
* export default monoCloud.monoCloudAuth();
|
|
519
|
-
*```
|
|
520
|
-
*
|
|
521
|
-
* @example Page Router with Response
|
|
522
|
-
*
|
|
523
|
-
* ```typescript
|
|
524
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
525
|
-
* import { NextApiRequest, NextApiResponse } from "next";
|
|
526
|
-
*
|
|
527
|
-
* export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
528
|
-
* const authHandler = monoCloud.monoCloudAuth();
|
|
529
|
-
*
|
|
530
|
-
* res.setHeader("last_auth_requested", `${Date.now()}`);
|
|
531
|
-
*
|
|
532
|
-
* return authHandler(req, res);
|
|
533
|
-
* }
|
|
534
|
-
* ```
|
|
535
|
-
*
|
|
427
|
+
* @see {@link monoCloudAuth} for full docs and examples.
|
|
428
|
+
* @param options Optional configuration for the auth handler.
|
|
429
|
+
* @returns Returns a Next.js-compatible handler for App Router route handlers or Pages Router API routes.
|
|
536
430
|
*/
|
|
537
431
|
monoCloudAuth(options) {
|
|
538
432
|
return (req, resOrCtx) => {
|
|
@@ -581,7 +475,7 @@ var MonoCloudNextClient = class {
|
|
|
581
475
|
const { redirect } = await import("next/navigation");
|
|
582
476
|
return redirect(signInRoute.toString());
|
|
583
477
|
}
|
|
584
|
-
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
478
|
+
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup$1(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
585
479
|
if (options.onGroupAccessDenied) return options.onGroupAccessDenied({
|
|
586
480
|
...params,
|
|
587
481
|
user: session.user
|
|
@@ -623,7 +517,7 @@ var MonoCloudNextClient = class {
|
|
|
623
517
|
permanent: false
|
|
624
518
|
} };
|
|
625
519
|
}
|
|
626
|
-
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
520
|
+
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup$1(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
627
521
|
var _options$onGroupAcces;
|
|
628
522
|
const customProps = await ((_options$onGroupAcces = options.onGroupAccessDenied) === null || _options$onGroupAcces === void 0 ? void 0 : _options$onGroupAcces.call(options, {
|
|
629
523
|
...context,
|
|
@@ -669,7 +563,7 @@ var MonoCloudNextClient = class {
|
|
|
669
563
|
}
|
|
670
564
|
return mergeResponse([res, NextResponse.json({ message: "unauthorized" }, { status: 401 })]);
|
|
671
565
|
}
|
|
672
|
-
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
566
|
+
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup$1(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
673
567
|
if (options.onGroupAccessDenied) {
|
|
674
568
|
const result = await options.onGroupAccessDenied(req, ctx, session.user);
|
|
675
569
|
if (result instanceof NextResponse) return mergeResponse([res, result]);
|
|
@@ -687,7 +581,7 @@ var MonoCloudNextClient = class {
|
|
|
687
581
|
if (options === null || options === void 0 ? void 0 : options.onAccessDenied) return options.onAccessDenied(req, res);
|
|
688
582
|
return res.status(401).json({ message: "unauthorized" });
|
|
689
583
|
}
|
|
690
|
-
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
584
|
+
if ((options === null || options === void 0 ? void 0 : options.groups) && !isUserInGroup$1(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
|
|
691
585
|
if (options.onGroupAccessDenied) return options.onGroupAccessDenied(req, res, session.user);
|
|
692
586
|
return res.status(403).json({ message: "forbidden" });
|
|
693
587
|
}
|
|
@@ -752,7 +646,7 @@ var MonoCloudNextClient = class {
|
|
|
752
646
|
return mergeResponse([nxtResp, NextResponse.redirect(signInRoute)]);
|
|
753
647
|
}
|
|
754
648
|
const groupsClaim = (options === null || options === void 0 ? void 0 : options.groupsClaim) ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM;
|
|
755
|
-
if (allowedGroups && !isUserInGroup(session.user, allowedGroups, groupsClaim)) {
|
|
649
|
+
if (allowedGroups && !isUserInGroup$1(session.user, allowedGroups, groupsClaim)) {
|
|
756
650
|
if (options === null || options === void 0 ? void 0 : options.onGroupAccessDenied) {
|
|
757
651
|
const result = await options.onGroupAccessDenied(req, evt, session.user);
|
|
758
652
|
if (result instanceof NextResponse) return mergeResponse([nxtResp, result]);
|
|
@@ -824,52 +718,9 @@ var MonoCloudNextClient = class {
|
|
|
824
718
|
return await this.coreClient.isAuthenticated(request, response);
|
|
825
719
|
}
|
|
826
720
|
/**
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
*
|
|
830
|
-
*
|
|
831
|
-
* @param options Options to customize the sign-in.
|
|
832
|
-
*
|
|
833
|
-
* @returns
|
|
834
|
-
*
|
|
835
|
-
* @example React Server Component
|
|
836
|
-
*
|
|
837
|
-
* ```tsx
|
|
838
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
839
|
-
*
|
|
840
|
-
* export default async function Home() {
|
|
841
|
-
* await monoCloud.protect();
|
|
842
|
-
*
|
|
843
|
-
* return <>You are signed in.</>;
|
|
844
|
-
* }
|
|
845
|
-
* ```
|
|
846
|
-
*
|
|
847
|
-
* @example API Handler
|
|
848
|
-
*
|
|
849
|
-
* ```typescript
|
|
850
|
-
* import { NextResponse } from "next/server";
|
|
851
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
852
|
-
*
|
|
853
|
-
* export const GET = async () => {
|
|
854
|
-
* await monoCloud.protect();
|
|
855
|
-
*
|
|
856
|
-
* return NextResponse.json({ secret: "ssshhhh!!!" });
|
|
857
|
-
* };
|
|
858
|
-
* ```
|
|
859
|
-
*
|
|
860
|
-
* @example Server Action
|
|
861
|
-
*
|
|
862
|
-
* ```typescript
|
|
863
|
-
* "use server";
|
|
864
|
-
*
|
|
865
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
866
|
-
*
|
|
867
|
-
* export async function getMessage() {
|
|
868
|
-
* await monoCloud.protect();
|
|
869
|
-
*
|
|
870
|
-
* return { secret: "sssshhhhh!!!" };
|
|
871
|
-
* }
|
|
872
|
-
* ```
|
|
721
|
+
* @see {@link protect} for full docs and examples.
|
|
722
|
+
* @param options Optional configuration for redirect behavior (for example, return URL or sign-in parameters).
|
|
723
|
+
* @returns Resolves if the user is authenticated; otherwise triggers a redirect.
|
|
873
724
|
*/
|
|
874
725
|
async protect(options) {
|
|
875
726
|
var _options$authParams19, _options$authParams20, _options$authParams21, _options$authParams22, _options$authParams23, _options$authParams24, _options$authParams25, _options$authParams26, _options$authParams27;
|
|
@@ -878,7 +729,7 @@ var MonoCloudNextClient = class {
|
|
|
878
729
|
try {
|
|
879
730
|
const session = await this.getSession();
|
|
880
731
|
if (session && !(options === null || options === void 0 ? void 0 : options.groups)) return;
|
|
881
|
-
if (session && options
|
|
732
|
+
if (session && (options === null || options === void 0 ? void 0 : options.groups) && isUserInGroup$1(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) return;
|
|
882
733
|
const { headers } = await import("next/headers");
|
|
883
734
|
path = (await headers()).get("x-monocloud-path") ?? "/";
|
|
884
735
|
} catch {
|
|
@@ -943,66 +794,9 @@ var MonoCloudNextClient = class {
|
|
|
943
794
|
return await this.coreClient.isUserInGroup(request, response, groups, (options === null || options === void 0 ? void 0 : options.groupsClaim) ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options === null || options === void 0 ? void 0 : options.matchAll);
|
|
944
795
|
}
|
|
945
796
|
/**
|
|
946
|
-
*
|
|
947
|
-
*
|
|
948
|
-
*
|
|
949
|
-
*
|
|
950
|
-
* @param options Options to customize the sign-in.
|
|
951
|
-
*
|
|
952
|
-
* @returns
|
|
953
|
-
*
|
|
954
|
-
* @example React Server Component
|
|
955
|
-
*
|
|
956
|
-
* ```tsx
|
|
957
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
958
|
-
*
|
|
959
|
-
* export default async function Home() {
|
|
960
|
-
* const allowed = await monoCloud.isUserInGroup(["admin"]);
|
|
961
|
-
*
|
|
962
|
-
* if (!allowed) {
|
|
963
|
-
* await monoCloud.redirectToSignIn({ returnUrl: "/home" });
|
|
964
|
-
* }
|
|
965
|
-
*
|
|
966
|
-
* return <>You are signed in.</>;
|
|
967
|
-
* }
|
|
968
|
-
* ```
|
|
969
|
-
*
|
|
970
|
-
* @example Server Action
|
|
971
|
-
*
|
|
972
|
-
* ```typescript
|
|
973
|
-
* "use server";
|
|
974
|
-
*
|
|
975
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
976
|
-
*
|
|
977
|
-
* export async function protectedAction() {
|
|
978
|
-
* const session = await monoCloud.getSession();
|
|
979
|
-
*
|
|
980
|
-
* if (!session) {
|
|
981
|
-
* await monoCloud.redirectToSignIn();
|
|
982
|
-
* }
|
|
983
|
-
*
|
|
984
|
-
* return { data: "Sensitive Data" };
|
|
985
|
-
* }
|
|
986
|
-
* ```
|
|
987
|
-
*
|
|
988
|
-
* @example API Handler
|
|
989
|
-
*
|
|
990
|
-
* ```typescript
|
|
991
|
-
* import { NextResponse } from "next/server";
|
|
992
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
993
|
-
*
|
|
994
|
-
* export const GET = async () => {
|
|
995
|
-
* const session = await monoCloud.getSession();
|
|
996
|
-
*
|
|
997
|
-
* if (!session) {
|
|
998
|
-
* await monoCloud.redirectToSignIn({
|
|
999
|
-
* returnUrl: "/dashboard",
|
|
1000
|
-
* });
|
|
1001
|
-
* }
|
|
1002
|
-
*
|
|
1003
|
-
* return NextResponse.json({ data: "Protected content" });
|
|
1004
|
-
* };
|
|
1005
|
-
* ```
|
|
797
|
+
* @see {@link redirectToSignIn} for full docs and examples.
|
|
798
|
+
* @param options Optional configuration for the redirect, such as `returnUrl` or additional sign-in parameters.
|
|
799
|
+
* @returns Never resolves. Triggers a redirect to the sign-in flow.
|
|
1006
800
|
*/
|
|
1007
801
|
async redirectToSignIn(options) {
|
|
1008
802
|
const { routes, appUrl } = this.coreClient.getOptions();
|
|
@@ -1016,8 +810,8 @@ var MonoCloudNextClient = class {
|
|
|
1016
810
|
if (options === null || options === void 0 ? void 0 : options.returnUrl) signInRoute.searchParams.set("return_url", options.returnUrl);
|
|
1017
811
|
if (options === null || options === void 0 ? void 0 : options.maxAge) signInRoute.searchParams.set("max_age", options.maxAge.toString());
|
|
1018
812
|
if (options === null || options === void 0 ? void 0 : options.authenticatorHint) signInRoute.searchParams.set("authenticator_hint", options.authenticatorHint);
|
|
1019
|
-
if (
|
|
1020
|
-
if (
|
|
813
|
+
if (options === null || options === void 0 ? void 0 : options.scopes) signInRoute.searchParams.set("scope", options.scopes);
|
|
814
|
+
if (options === null || options === void 0 ? void 0 : options.resource) signInRoute.searchParams.set("resource", options.resource);
|
|
1021
815
|
if (options === null || options === void 0 ? void 0 : options.display) signInRoute.searchParams.set("display", options.display);
|
|
1022
816
|
if (options === null || options === void 0 ? void 0 : options.uiLocales) signInRoute.searchParams.set("ui_locales", options.uiLocales);
|
|
1023
817
|
if (Array.isArray(options === null || options === void 0 ? void 0 : options.acrValues)) signInRoute.searchParams.set("acr_values", options.acrValues.join(" "));
|
|
@@ -1027,65 +821,9 @@ var MonoCloudNextClient = class {
|
|
|
1027
821
|
redirect(signInRoute.toString());
|
|
1028
822
|
}
|
|
1029
823
|
/**
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1032
|
-
*
|
|
1033
|
-
*
|
|
1034
|
-
* @param options Options to customize the sign out.
|
|
1035
|
-
*
|
|
1036
|
-
* @returns
|
|
1037
|
-
*
|
|
1038
|
-
* @example React Server Component
|
|
1039
|
-
*
|
|
1040
|
-
* ```tsx
|
|
1041
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1042
|
-
*
|
|
1043
|
-
* export default async function Page() {
|
|
1044
|
-
* const session = await monoCloud.getSession();
|
|
1045
|
-
*
|
|
1046
|
-
* // Example: Force sign-out if a specific condition is met (e.g., account suspended)
|
|
1047
|
-
* if (session?.user.isSuspended) {
|
|
1048
|
-
* await monoCloud.redirectToSignOut();
|
|
1049
|
-
* }
|
|
1050
|
-
*
|
|
1051
|
-
* return <>Welcome User</>;
|
|
1052
|
-
* }
|
|
1053
|
-
* ```
|
|
1054
|
-
*
|
|
1055
|
-
* @example Server Action
|
|
1056
|
-
*
|
|
1057
|
-
* ```typescript
|
|
1058
|
-
* "use server";
|
|
1059
|
-
*
|
|
1060
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1061
|
-
*
|
|
1062
|
-
* export async function signOutAction() {
|
|
1063
|
-
* const session = await monoCloud.getSession();
|
|
1064
|
-
*
|
|
1065
|
-
* if (session) {
|
|
1066
|
-
* await monoCloud.redirectToSignOut();
|
|
1067
|
-
* }
|
|
1068
|
-
* }
|
|
1069
|
-
* ```
|
|
1070
|
-
*
|
|
1071
|
-
* @example API Handler
|
|
1072
|
-
*
|
|
1073
|
-
* ```typescript
|
|
1074
|
-
* import { monoCloud } from "@/lib/monocloud";
|
|
1075
|
-
* import { NextResponse } from "next/server";
|
|
1076
|
-
*
|
|
1077
|
-
* export const GET = async () => {
|
|
1078
|
-
* const session = await monoCloud.getSession();
|
|
1079
|
-
*
|
|
1080
|
-
* if (session) {
|
|
1081
|
-
* await monoCloud.redirectToSignOut({
|
|
1082
|
-
* postLogoutRedirectUri: "/goodbye",
|
|
1083
|
-
* });
|
|
1084
|
-
* }
|
|
1085
|
-
*
|
|
1086
|
-
* return NextResponse.json({ status: "already_signed_out" });
|
|
1087
|
-
* };
|
|
1088
|
-
* ```
|
|
824
|
+
* @see {@link redirectToSignOut} for full docs and examples.
|
|
825
|
+
* @param options Optional configuration for the redirect, such as `postLogoutRedirectUri` or additional sign-out parameters.
|
|
826
|
+
* @returns Never resolves. Triggers a redirect to the sign-out flow.
|
|
1089
827
|
*/
|
|
1090
828
|
async redirectToSignOut(options) {
|
|
1091
829
|
var _options$postLogoutRe;
|
|
@@ -1114,5 +852,275 @@ var MonoCloudNextClient = class {
|
|
|
1114
852
|
};
|
|
1115
853
|
|
|
1116
854
|
//#endregion
|
|
1117
|
-
|
|
855
|
+
//#region src/initialize.ts
|
|
856
|
+
let instance;
|
|
857
|
+
/**
|
|
858
|
+
* Retrieves the singleton instance of the MonoCloudNextClient.
|
|
859
|
+
* Initializes it lazily on the first call.
|
|
860
|
+
*/
|
|
861
|
+
const getInstance = () => {
|
|
862
|
+
instance ??= new MonoCloudNextClient();
|
|
863
|
+
return instance;
|
|
864
|
+
};
|
|
865
|
+
/**
|
|
866
|
+
* Creates a Next.js catch-all auth route handler (Pages Router and App Router) for the built-in routes (`/signin`, `/callback`, `/userinfo`, `/signout`).
|
|
867
|
+
*
|
|
868
|
+
* Mount this handler on a catch-all route (e.g. `/api/auth/[...monocloud]`).
|
|
869
|
+
*
|
|
870
|
+
* > 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.
|
|
871
|
+
*
|
|
872
|
+
* @example App Router
|
|
873
|
+
* ```tsx:src/app/api/auth/[...monocloud]/route.ts tab="App Router" tab-group="monoCloudAuth"
|
|
874
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
875
|
+
*
|
|
876
|
+
* export const GET = monoCloudAuth();
|
|
877
|
+
*```
|
|
878
|
+
*
|
|
879
|
+
* @example App Router (Response)
|
|
880
|
+
* ```tsx:src/app/api/auth/[...monocloud]/route.ts tab="App Router (Response)" tab-group="monoCloudAuth"
|
|
881
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
882
|
+
* import { NextRequest, NextResponse } from "next/server";
|
|
883
|
+
*
|
|
884
|
+
* export const GET = (req: NextRequest) => {
|
|
885
|
+
* const authHandler = monoCloudAuth();
|
|
886
|
+
*
|
|
887
|
+
* const res = new NextResponse();
|
|
888
|
+
*
|
|
889
|
+
* res.cookies.set("last_auth_requested", `${Date.now()}`);
|
|
890
|
+
*
|
|
891
|
+
* return authHandler(req, res);
|
|
892
|
+
* };
|
|
893
|
+
* ```
|
|
894
|
+
*
|
|
895
|
+
* @example Pages Router
|
|
896
|
+
* ```tsx:src/pages/api/auth/[...monocloud].ts tab="Pages Router" tab-group="monoCloudAuth"
|
|
897
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
898
|
+
*
|
|
899
|
+
* export default monoCloudAuth();
|
|
900
|
+
*```
|
|
901
|
+
*
|
|
902
|
+
* @example Pages Router (Response)
|
|
903
|
+
* ```tsx:src/pages/api/auth/[...monocloud].ts tab="Pages Router (Response)" tab-group="monoCloudAuth"
|
|
904
|
+
* import { monoCloudAuth } from "@monocloud/auth-nextjs";
|
|
905
|
+
* import { NextApiRequest, NextApiResponse } from "next";
|
|
906
|
+
*
|
|
907
|
+
* export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
908
|
+
* const authHandler = monoCloudAuth();
|
|
909
|
+
*
|
|
910
|
+
* res.setHeader("last_auth_requested", `${Date.now()}`);
|
|
911
|
+
*
|
|
912
|
+
* return authHandler(req, res);
|
|
913
|
+
* }
|
|
914
|
+
* ```
|
|
915
|
+
*
|
|
916
|
+
* @param options Optional configuration for the auth handler.
|
|
917
|
+
* @returns Returns a Next.js-compatible handler for App Router route handlers or Pages Router API routes.
|
|
918
|
+
*
|
|
919
|
+
* @category Functions
|
|
920
|
+
*/
|
|
921
|
+
function monoCloudAuth(options) {
|
|
922
|
+
return getInstance().monoCloudAuth(options);
|
|
923
|
+
}
|
|
924
|
+
function authMiddleware(...args) {
|
|
925
|
+
return getInstance().authMiddleware(...args);
|
|
926
|
+
}
|
|
927
|
+
function getSession(...args) {
|
|
928
|
+
return getInstance().getSession(...args);
|
|
929
|
+
}
|
|
930
|
+
function getTokens(...args) {
|
|
931
|
+
return getInstance().getTokens(...args);
|
|
932
|
+
}
|
|
933
|
+
function isAuthenticated(...args) {
|
|
934
|
+
return getInstance().isAuthenticated(...args);
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* Ensures the current user is authenticated. If not, redirects to the sign-in flow.
|
|
938
|
+
*
|
|
939
|
+
* > **App Router only.** Intended for Server Components, Route Handlers, and Server Actions.
|
|
940
|
+
*
|
|
941
|
+
* @example Server Component
|
|
942
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="protect"
|
|
943
|
+
* import { protect } from "@monocloud/auth-nextjs";
|
|
944
|
+
*
|
|
945
|
+
* export default async function Home() {
|
|
946
|
+
* await protect();
|
|
947
|
+
*
|
|
948
|
+
* return <>You are signed in.</>;
|
|
949
|
+
* }
|
|
950
|
+
* ```
|
|
951
|
+
*
|
|
952
|
+
* @example Server Action
|
|
953
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="protect"
|
|
954
|
+
* "use server";
|
|
955
|
+
*
|
|
956
|
+
* import { protect } from "@monocloud/auth-nextjs";
|
|
957
|
+
*
|
|
958
|
+
* export async function getMessage() {
|
|
959
|
+
* await protect();
|
|
960
|
+
*
|
|
961
|
+
* return { secret: "sssshhhhh!!!" };
|
|
962
|
+
* }
|
|
963
|
+
* ```
|
|
964
|
+
*
|
|
965
|
+
* @example API Handler
|
|
966
|
+
* ```tsx:src/app/api/protected/route.ts tab="API Handler" tab-group="protect"
|
|
967
|
+
* import { protect } from "@monocloud/auth-nextjs";
|
|
968
|
+
* import { NextResponse } from "next/server";
|
|
969
|
+
*
|
|
970
|
+
* export const GET = async () => {
|
|
971
|
+
* await protect();
|
|
972
|
+
*
|
|
973
|
+
* return NextResponse.json({ secret: "ssshhhh!!!" });
|
|
974
|
+
* };
|
|
975
|
+
* ```
|
|
976
|
+
*
|
|
977
|
+
* @param options Optional configuration for redirect behavior (for example, return URL or sign-in parameters).
|
|
978
|
+
* @returns Resolves if the user is authenticated; otherwise triggers a redirect.
|
|
979
|
+
*
|
|
980
|
+
* @category Functions
|
|
981
|
+
*/
|
|
982
|
+
function protect(options) {
|
|
983
|
+
return getInstance().protect(options);
|
|
984
|
+
}
|
|
985
|
+
function protectApi(handler, options) {
|
|
986
|
+
return getInstance().protectApi(handler, options);
|
|
987
|
+
}
|
|
988
|
+
function protectPage(...args) {
|
|
989
|
+
return getInstance().protectPage(...args);
|
|
990
|
+
}
|
|
991
|
+
function isUserInGroup(...args) {
|
|
992
|
+
return getInstance().isUserInGroup(...args);
|
|
993
|
+
}
|
|
994
|
+
/**
|
|
995
|
+
* Redirects the user to the sign-in flow.
|
|
996
|
+
*
|
|
997
|
+
* > **App Router only**. Intended for use in Server Components, Route Handlers, and Server Actions.
|
|
998
|
+
*
|
|
999
|
+
* This helper performs a server-side redirect to the configured sign-in route. Execution does not continue after the redirect is triggered.
|
|
1000
|
+
*
|
|
1001
|
+
* @example Server Component
|
|
1002
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="redirect-to-sign-in"
|
|
1003
|
+
* import { isUserInGroup, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1004
|
+
*
|
|
1005
|
+
* export default async function Home() {
|
|
1006
|
+
* const allowed = await isUserInGroup(["admin"]);
|
|
1007
|
+
*
|
|
1008
|
+
* if (!allowed) {
|
|
1009
|
+
* await redirectToSignIn({ returnUrl: "/home" });
|
|
1010
|
+
* }
|
|
1011
|
+
*
|
|
1012
|
+
* return <>You are signed in.</>;
|
|
1013
|
+
* }
|
|
1014
|
+
* ```
|
|
1015
|
+
*
|
|
1016
|
+
* @example Server Action
|
|
1017
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="redirect-to-sign-in"
|
|
1018
|
+
* "use server";
|
|
1019
|
+
*
|
|
1020
|
+
* import { getSession, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1021
|
+
*
|
|
1022
|
+
* export async function protectedAction() {
|
|
1023
|
+
* const session = await getSession();
|
|
1024
|
+
*
|
|
1025
|
+
* if (!session) {
|
|
1026
|
+
* await redirectToSignIn();
|
|
1027
|
+
* }
|
|
1028
|
+
*
|
|
1029
|
+
* return { data: "Sensitive Data" };
|
|
1030
|
+
* }
|
|
1031
|
+
* ```
|
|
1032
|
+
*
|
|
1033
|
+
* @example API Handler
|
|
1034
|
+
* ```tsx:src/app/api/protected/route.ts tab="API Handler" tab-group="redirect-to-sign-in"
|
|
1035
|
+
* import { getSession, redirectToSignIn } from "@monocloud/auth-nextjs";
|
|
1036
|
+
* import { NextResponse } from "next/server";
|
|
1037
|
+
*
|
|
1038
|
+
* export const GET = async () => {
|
|
1039
|
+
* const session = await getSession();
|
|
1040
|
+
*
|
|
1041
|
+
* if (!session) {
|
|
1042
|
+
* await redirectToSignIn({
|
|
1043
|
+
* returnUrl: "/dashboard",
|
|
1044
|
+
* });
|
|
1045
|
+
* }
|
|
1046
|
+
*
|
|
1047
|
+
* return NextResponse.json({ data: "Protected content" });
|
|
1048
|
+
* };
|
|
1049
|
+
* ```
|
|
1050
|
+
*
|
|
1051
|
+
* @param options Optional configuration for the redirect, such as `returnUrl` or additional sign-in parameters.
|
|
1052
|
+
* @returns Never resolves. Triggers a redirect to the sign-in flow.
|
|
1053
|
+
*
|
|
1054
|
+
* @category Functions
|
|
1055
|
+
*/
|
|
1056
|
+
function redirectToSignIn(options) {
|
|
1057
|
+
return getInstance().redirectToSignIn(options);
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Redirects the user to the sign-out flow.
|
|
1061
|
+
*
|
|
1062
|
+
* > **App Router only**. Intended for use in Server Components, Route Handlers, and Server Actions.
|
|
1063
|
+
*
|
|
1064
|
+
* This helper performs a server-side redirect to the configured sign-out route. Execution does not continue after the redirect is triggered.
|
|
1065
|
+
*
|
|
1066
|
+
* @example Server Component
|
|
1067
|
+
* ```tsx:src/app/page.tsx tab="Server Component" tab-group="redirect-to-sign-out"
|
|
1068
|
+
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1069
|
+
*
|
|
1070
|
+
* export default async function Page() {
|
|
1071
|
+
* const session = await getSession();
|
|
1072
|
+
*
|
|
1073
|
+
* // Example: Force sign-out if a specific condition is met (e.g., account suspended)
|
|
1074
|
+
* if (session?.user.isSuspended) {
|
|
1075
|
+
* await redirectToSignOut();
|
|
1076
|
+
* }
|
|
1077
|
+
*
|
|
1078
|
+
* return <>Welcome User</>;
|
|
1079
|
+
* }
|
|
1080
|
+
* ```
|
|
1081
|
+
*
|
|
1082
|
+
* @example Server Action
|
|
1083
|
+
* ```tsx:src/action.ts tab="Server Action" tab-group="redirect-to-sign-out"
|
|
1084
|
+
* "use server";
|
|
1085
|
+
*
|
|
1086
|
+
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1087
|
+
*
|
|
1088
|
+
* export async function signOutAction() {
|
|
1089
|
+
* const session = await getSession();
|
|
1090
|
+
*
|
|
1091
|
+
* if (session) {
|
|
1092
|
+
* await redirectToSignOut();
|
|
1093
|
+
* }
|
|
1094
|
+
* }
|
|
1095
|
+
* ```
|
|
1096
|
+
*
|
|
1097
|
+
* @example API Handler
|
|
1098
|
+
* ```tsx:src/app/api/signout/route.ts tab="API Handler" tab-group="redirect-to-sign-out"
|
|
1099
|
+
* import { getSession, redirectToSignOut } from "@monocloud/auth-nextjs";
|
|
1100
|
+
* import { NextResponse } from "next/server";
|
|
1101
|
+
*
|
|
1102
|
+
* export const GET = async () => {
|
|
1103
|
+
* const session = await getSession();
|
|
1104
|
+
*
|
|
1105
|
+
* if (session) {
|
|
1106
|
+
* await redirectToSignOut({
|
|
1107
|
+
* postLogoutRedirectUri: "/goodbye",
|
|
1108
|
+
* });
|
|
1109
|
+
* }
|
|
1110
|
+
*
|
|
1111
|
+
* return NextResponse.json({ status: "already_signed_out" });
|
|
1112
|
+
* };
|
|
1113
|
+
* ```
|
|
1114
|
+
*
|
|
1115
|
+
* @param options Optional configuration for the redirect, such as `postLogoutRedirectUri` or additional sign-out parameters.
|
|
1116
|
+
* @returns Never resolves. Triggers a redirect to the sign-out flow.
|
|
1117
|
+
*
|
|
1118
|
+
* @category Functions
|
|
1119
|
+
*/
|
|
1120
|
+
function redirectToSignOut(options) {
|
|
1121
|
+
return getInstance().redirectToSignOut(options);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
//#endregion
|
|
1125
|
+
export { MonoCloudAuthBaseError, MonoCloudHttpError, MonoCloudNextClient, MonoCloudOPError, MonoCloudTokenError, MonoCloudValidationError, authMiddleware, getSession, getTokens, isAuthenticated, isUserInGroup, monoCloudAuth, protect, protectApi, protectPage, redirectToSignIn, redirectToSignOut };
|
|
1118
1126
|
//# sourceMappingURL=index.mjs.map
|