@monocloud/auth-nextjs 0.1.4 → 0.1.6

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/index.d.mts CHANGED
@@ -1,12 +1,11 @@
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";
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-Cx32VRoI.mjs";
2
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
3
  import { NextFetchEvent, NextMiddleware, NextProxy, NextRequest, NextResponse } from "next/server.js";
4
- import { IncomingMessage, ServerResponse } from "node:http";
5
4
  import { NextApiHandler, NextApiRequest, NextApiResponse } from "next/types";
6
5
  import { ParsedUrlQuery } from "node:querystring";
6
+ import { IncomingMessage, ServerResponse } from "node:http";
7
7
 
8
8
  //#region src/monocloud-next-client.d.ts
9
-
10
9
  /**
11
10
  * The MonoCloud Next.js Client.
12
11
  *
@@ -268,8 +267,8 @@ declare class MonoCloudNextClient {
268
267
  *
269
268
  * 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
269
  *
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.**
270
+ * **Note⚠️ - When using groups to protect a page, 'Access Denied' is rendered by default when the user does not belong to the groups.
271
+ * To display a custom component, pass the `onGroupAccessDenied` parameter.**
273
272
  *
274
273
  * @param component The App Router server component that protectPage wraps and secures
275
274
  * @param options App Router `protectPage()` configuration options
@@ -308,9 +307,9 @@ declare class MonoCloudNextClient {
308
307
  *
309
308
  * 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
309
  *
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.**
310
+ * **Note⚠️ - When using groups to protect a page, the page will be rendered even if the user does not belong to the groups.
311
+ * You should check the props for `groupAccessDenied` boolean value to determine whether the user is
312
+ * allowed to access the page. Alternatively, you can pass `onGroupAccessDenied` parameter to return custom props.**
314
313
  *
315
314
  * @param options Pages Router `protectPage()` configuration options
316
315
  *
@@ -764,8 +763,12 @@ declare class MonoCloudNextClient {
764
763
  * @example SSR Component
765
764
  *
766
765
  * ```typescript
767
- * import { monoCloud } from "@/lib/monocloud";
768
- * import type { GetServerSideProps, InferGetServerSidePropsType } from "next";
766
+ * import { monoCloud } from "@/monocloud";
767
+ * import type {
768
+ * GetServerSideProps,
769
+ * GetServerSidePropsContext,
770
+ * InferGetServerSidePropsType,
771
+ * } from "next";
769
772
  *
770
773
  * type HomeProps = InferGetServerSidePropsType<typeof getServerSideProps>;
771
774
  *
@@ -773,15 +776,20 @@ declare class MonoCloudNextClient {
773
776
  * return <pre>Session: {JSON.stringify(session, null, 2)}</pre>;
774
777
  * }
775
778
  *
776
- * export const getServerSideProps: GetServerSideProps = async (context) => {
777
- * const session = await monoCloud.getSession(context.req, context.res);
779
+ * export const getServerSideProps = (async (
780
+ * context: GetServerSidePropsContext,
781
+ * ) => {
782
+ * const session = await monoCloud.getSession(
783
+ * context.req,
784
+ * context.res,
785
+ * );
778
786
  *
779
787
  * return {
780
788
  * props: {
781
789
  * session: session ?? null,
782
790
  * },
783
791
  * };
784
- * };
792
+ * }) satisfies GetServerSideProps;
785
793
  * ```
786
794
  */
787
795
  getSession(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>): Promise<MonoCloudSession$1 | undefined>;
@@ -868,7 +876,7 @@ declare class MonoCloudNextClient {
868
876
  *
869
877
  * The default token is an access token with scopes set through `MONOCLOUD_AUTH_SCOPES` or
870
878
  * `options.defaultAuthParams.scopes`, and resources set through `MONOCLOUD_AUTH_RESOURCE` or
871
- * `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without parameters.
879
+ * `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without resource and scopes parameters.
872
880
  *
873
881
  * ```typescript
874
882
  * import { NextResponse } from "next/server";
package/dist/index.mjs CHANGED
@@ -3,7 +3,6 @@ import { NextRequest, NextResponse } from "next/server.js";
3
3
  import { ensureLeadingSlash, isAbsoluteUrl, isPresent } from "@monocloud/auth-node-core/internal";
4
4
  import { isUserInGroup } from "@monocloud/auth-node-core/utils";
5
5
  import { serialize } from "cookie";
6
- import { IncomingMessage, ServerResponse } from "node:http";
7
6
 
8
7
  //#region src/requests/monocloud-app-router-request.ts
9
8
  var MonoCloudAppRouterRequest = class {
@@ -219,6 +218,12 @@ var MonoCloudCookieRequest = class {
219
218
  const isMonoCloudRequest = (req) => req instanceof MonoCloudAppRouterRequest || req instanceof MonoCloudPageRouterRequest || req instanceof MonoCloudCookieRequest;
220
219
  const isMonoCloudResponse = (res) => res instanceof MonoCloudAppRouterResponse || res instanceof MonoCloudPageRouterResponse || res instanceof MonoCloudCookieResponse;
221
220
  const isAppRouter = (req) => req instanceof Request || req.headers instanceof Headers || typeof req.bodyUsed === "boolean";
221
+ const isNodeRequest = (req) => {
222
+ return !!(req && typeof req === "object" && "headers" in req && !("bodyUsed" in req) && typeof req.on === "function");
223
+ };
224
+ const isNodeResponse = (res) => {
225
+ return !!(res && typeof res === "object" && "setHeader" in res && typeof res.setHeader === "function" && "end" in res && typeof res.end === "function");
226
+ };
222
227
  const getNextRequest = (req) => {
223
228
  if (req instanceof NextRequest) return req;
224
229
  return new NextRequest(req.url, {
@@ -252,7 +257,7 @@ const getMonoCloudCookieReqRes = (req, resOrCtx) => {
252
257
  request = new MonoCloudAppRouterRequest(getNextRequest(req));
253
258
  response = resOrCtx instanceof Response ? new MonoCloudAppRouterResponse(getNextResponse(resOrCtx)) : new MonoCloudCookieResponse();
254
259
  } else {
255
- if (!(req instanceof IncomingMessage) || !(resOrCtx instanceof ServerResponse)) throw new MonoCloudValidationError$1("Invalid pages router request and response");
260
+ if (!isNodeRequest(req) || !isNodeResponse(resOrCtx)) throw new MonoCloudValidationError$1("Invalid pages router request and response");
256
261
  request = new MonoCloudPageRouterRequest(req);
257
262
  response = new MonoCloudPageRouterResponse(resOrCtx);
258
263
  }
@@ -458,7 +463,7 @@ var MonoCloudNextClient = class {
458
463
  constructor(options) {
459
464
  const opt = {
460
465
  ...options ?? {},
461
- userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.4`,
466
+ userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.6`,
462
467
  debugger: (options === null || options === void 0 ? void 0 : options.debugger) ?? "@monocloud:auth-nextjs"
463
468
  };
464
469
  this.registerPublicEnvVariables();
@@ -577,7 +582,7 @@ var MonoCloudNextClient = class {
577
582
  return redirect(signInRoute.toString());
578
583
  }
579
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)) {
580
- if (options.onAccessDenied) return options.onAccessDenied({
585
+ if (options.onGroupAccessDenied) return options.onGroupAccessDenied({
581
586
  ...params,
582
587
  user: session.user
583
588
  });
@@ -595,10 +600,10 @@ var MonoCloudNextClient = class {
595
600
  if (!session) {
596
601
  var _options$authParams10, _options$authParams11, _options$authParams12, _options$authParams13, _options$authParams14, _options$authParams15, _options$authParams16, _options$authParams17, _options$authParams18;
597
602
  if (options === null || options === void 0 ? void 0 : options.onAccessDenied) {
598
- const customProps$1 = await options.onAccessDenied({ ...context });
603
+ const customProps = await options.onAccessDenied({ ...context });
599
604
  return {
600
- ...customProps$1 ?? {},
601
- props: { ...(customProps$1 === null || customProps$1 === void 0 ? void 0 : customProps$1.props) ?? {} }
605
+ ...customProps ?? {},
606
+ props: { ...(customProps === null || customProps === void 0 ? void 0 : customProps.props) ?? {} }
602
607
  };
603
608
  }
604
609
  const { routes, appUrl } = this.getOptions();
@@ -619,14 +624,14 @@ var MonoCloudNextClient = class {
619
624
  } };
620
625
  }
621
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)) {
622
- var _options$onAccessDeni;
623
- const customProps$1 = await ((_options$onAccessDeni = options.onAccessDenied) === null || _options$onAccessDeni === void 0 ? void 0 : _options$onAccessDeni.call(options, {
627
+ var _options$onGroupAcces;
628
+ const customProps = await ((_options$onGroupAcces = options.onGroupAccessDenied) === null || _options$onGroupAcces === void 0 ? void 0 : _options$onGroupAcces.call(options, {
624
629
  ...context,
625
630
  user: session.user
626
- })) ?? { props: { accessDenied: true } };
631
+ })) ?? { props: { groupAccessDenied: true } };
627
632
  return {
628
- ...customProps$1,
629
- props: { ...customProps$1.props ?? {} }
633
+ ...customProps,
634
+ props: { ...customProps.props ?? {} }
630
635
  };
631
636
  }
632
637
  const customProps = (options === null || options === void 0 ? void 0 : options.getServerSideProps) ? await options.getServerSideProps(context) : {};
@@ -665,8 +670,8 @@ var MonoCloudNextClient = class {
665
670
  return mergeResponse([res, NextResponse.json({ message: "unauthorized" }, { status: 401 })]);
666
671
  }
667
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)) {
668
- if (options.onAccessDenied) {
669
- const result = await options.onAccessDenied(req, ctx);
673
+ if (options.onGroupAccessDenied) {
674
+ const result = await options.onGroupAccessDenied(req, ctx, session.user);
670
675
  if (result instanceof NextResponse) return mergeResponse([res, result]);
671
676
  return mergeResponse([res, new NextResponse(result.body, result)]);
672
677
  }
@@ -683,7 +688,7 @@ var MonoCloudNextClient = class {
683
688
  return res.status(401).json({ message: "unauthorized" });
684
689
  }
685
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)) {
686
- if (options.onAccessDenied) return options.onAccessDenied(req, res, session.user);
691
+ if (options.onGroupAccessDenied) return options.onGroupAccessDenied(req, res, session.user);
687
692
  return res.status(403).json({ message: "forbidden" });
688
693
  }
689
694
  return handler(req, res);
@@ -747,10 +752,9 @@ var MonoCloudNextClient = class {
747
752
  return mergeResponse([nxtResp, NextResponse.redirect(signInRoute)]);
748
753
  }
749
754
  const groupsClaim = (options === null || options === void 0 ? void 0 : options.groupsClaim) ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM;
750
- const onAccessDenied = options === null || options === void 0 ? void 0 : options.onAccessDenied;
751
755
  if (allowedGroups && !isUserInGroup(session.user, allowedGroups, groupsClaim)) {
752
- if (onAccessDenied) {
753
- const result = await onAccessDenied(req, evt, session.user);
756
+ if (options === null || options === void 0 ? void 0 : options.onGroupAccessDenied) {
757
+ const result = await options.onGroupAccessDenied(req, evt, session.user);
754
758
  if (result instanceof NextResponse) return mergeResponse([nxtResp, result]);
755
759
  if (result) return mergeResponse([nxtResp, new NextResponse(result.body, result)]);
756
760
  return NextResponse.next(nxtResp);
@@ -800,7 +804,7 @@ var MonoCloudNextClient = class {
800
804
  ({request, response} = getMonoCloudCookieReqRes(args[0], void 0));
801
805
  options = args[1];
802
806
  }
803
- else if (args.length === 2 && args[0] instanceof IncomingMessage && args[1] instanceof ServerResponse) ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
807
+ else if (args.length === 2 && isNodeRequest(args[0]) && isNodeResponse(args[1])) ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
804
808
  else {
805
809
  ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
806
810
  options = args[2];
@@ -913,7 +917,7 @@ var MonoCloudNextClient = class {
913
917
  groups = args[1];
914
918
  options = args[2];
915
919
  }
916
- if (args[0] instanceof IncomingMessage && args[1] instanceof ServerResponse) {
920
+ if (isNodeRequest(args[0]) && isNodeResponse(args[1])) {
917
921
  ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
918
922
  groups = args[2];
919
923
  }