@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.cjs CHANGED
@@ -1,10 +1,10 @@
1
- const require_chunk = require('./chunk-CbDLau6x.cjs');
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_chunk = require('./chunk-C0xms8kb.cjs');
2
3
  let _monocloud_auth_node_core = require("@monocloud/auth-node-core");
3
4
  let next_server_js = require("next/server.js");
4
5
  let _monocloud_auth_node_core_internal = require("@monocloud/auth-node-core/internal");
5
6
  let _monocloud_auth_node_core_utils = require("@monocloud/auth-node-core/utils");
6
7
  let cookie = require("cookie");
7
- let node_http = require("node:http");
8
8
 
9
9
  //#region src/requests/monocloud-app-router-request.ts
10
10
  var MonoCloudAppRouterRequest = class {
@@ -220,6 +220,12 @@ var MonoCloudCookieRequest = class {
220
220
  const isMonoCloudRequest = (req) => req instanceof MonoCloudAppRouterRequest || req instanceof MonoCloudPageRouterRequest || req instanceof MonoCloudCookieRequest;
221
221
  const isMonoCloudResponse = (res) => res instanceof MonoCloudAppRouterResponse || res instanceof MonoCloudPageRouterResponse || res instanceof MonoCloudCookieResponse;
222
222
  const isAppRouter = (req) => req instanceof Request || req.headers instanceof Headers || typeof req.bodyUsed === "boolean";
223
+ const isNodeRequest = (req) => {
224
+ return !!(req && typeof req === "object" && "headers" in req && !("bodyUsed" in req) && typeof req.on === "function");
225
+ };
226
+ const isNodeResponse = (res) => {
227
+ return !!(res && typeof res === "object" && "setHeader" in res && typeof res.setHeader === "function" && "end" in res && typeof res.end === "function");
228
+ };
223
229
  const getNextRequest = (req) => {
224
230
  if (req instanceof next_server_js.NextRequest) return req;
225
231
  return new next_server_js.NextRequest(req.url, {
@@ -253,7 +259,7 @@ const getMonoCloudCookieReqRes = (req, resOrCtx) => {
253
259
  request = new MonoCloudAppRouterRequest(getNextRequest(req));
254
260
  response = resOrCtx instanceof Response ? new MonoCloudAppRouterResponse(getNextResponse(resOrCtx)) : new MonoCloudCookieResponse();
255
261
  } else {
256
- if (!(req instanceof node_http.IncomingMessage) || !(resOrCtx instanceof node_http.ServerResponse)) throw new _monocloud_auth_node_core.MonoCloudValidationError("Invalid pages router request and response");
262
+ if (!isNodeRequest(req) || !isNodeResponse(resOrCtx)) throw new _monocloud_auth_node_core.MonoCloudValidationError("Invalid pages router request and response");
257
263
  request = new MonoCloudPageRouterRequest(req);
258
264
  response = new MonoCloudPageRouterResponse(resOrCtx);
259
265
  }
@@ -459,7 +465,7 @@ var MonoCloudNextClient = class {
459
465
  constructor(options) {
460
466
  const opt = {
461
467
  ...options ?? {},
462
- userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.4`,
468
+ userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.6`,
463
469
  debugger: (options === null || options === void 0 ? void 0 : options.debugger) ?? "@monocloud:auth-nextjs"
464
470
  };
465
471
  this.registerPublicEnvVariables();
@@ -578,7 +584,7 @@ var MonoCloudNextClient = class {
578
584
  return redirect(signInRoute.toString());
579
585
  }
580
586
  if ((options === null || options === void 0 ? void 0 : options.groups) && !(0, _monocloud_auth_node_core_utils.isUserInGroup)(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
581
- if (options.onAccessDenied) return options.onAccessDenied({
587
+ if (options.onGroupAccessDenied) return options.onGroupAccessDenied({
582
588
  ...params,
583
589
  user: session.user
584
590
  });
@@ -596,10 +602,10 @@ var MonoCloudNextClient = class {
596
602
  if (!session) {
597
603
  var _options$authParams10, _options$authParams11, _options$authParams12, _options$authParams13, _options$authParams14, _options$authParams15, _options$authParams16, _options$authParams17, _options$authParams18;
598
604
  if (options === null || options === void 0 ? void 0 : options.onAccessDenied) {
599
- const customProps$1 = await options.onAccessDenied({ ...context });
605
+ const customProps = await options.onAccessDenied({ ...context });
600
606
  return {
601
- ...customProps$1 ?? {},
602
- props: { ...(customProps$1 === null || customProps$1 === void 0 ? void 0 : customProps$1.props) ?? {} }
607
+ ...customProps ?? {},
608
+ props: { ...(customProps === null || customProps === void 0 ? void 0 : customProps.props) ?? {} }
603
609
  };
604
610
  }
605
611
  const { routes, appUrl } = this.getOptions();
@@ -620,14 +626,14 @@ var MonoCloudNextClient = class {
620
626
  } };
621
627
  }
622
628
  if ((options === null || options === void 0 ? void 0 : options.groups) && !(0, _monocloud_auth_node_core_utils.isUserInGroup)(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
623
- var _options$onAccessDeni;
624
- const customProps$1 = await ((_options$onAccessDeni = options.onAccessDenied) === null || _options$onAccessDeni === void 0 ? void 0 : _options$onAccessDeni.call(options, {
629
+ var _options$onGroupAcces;
630
+ const customProps = await ((_options$onGroupAcces = options.onGroupAccessDenied) === null || _options$onGroupAcces === void 0 ? void 0 : _options$onGroupAcces.call(options, {
625
631
  ...context,
626
632
  user: session.user
627
- })) ?? { props: { accessDenied: true } };
633
+ })) ?? { props: { groupAccessDenied: true } };
628
634
  return {
629
- ...customProps$1,
630
- props: { ...customProps$1.props ?? {} }
635
+ ...customProps,
636
+ props: { ...customProps.props ?? {} }
631
637
  };
632
638
  }
633
639
  const customProps = (options === null || options === void 0 ? void 0 : options.getServerSideProps) ? await options.getServerSideProps(context) : {};
@@ -666,8 +672,8 @@ var MonoCloudNextClient = class {
666
672
  return mergeResponse([res, next_server_js.NextResponse.json({ message: "unauthorized" }, { status: 401 })]);
667
673
  }
668
674
  if ((options === null || options === void 0 ? void 0 : options.groups) && !(0, _monocloud_auth_node_core_utils.isUserInGroup)(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
669
- if (options.onAccessDenied) {
670
- const result = await options.onAccessDenied(req, ctx);
675
+ if (options.onGroupAccessDenied) {
676
+ const result = await options.onGroupAccessDenied(req, ctx, session.user);
671
677
  if (result instanceof next_server_js.NextResponse) return mergeResponse([res, result]);
672
678
  return mergeResponse([res, new next_server_js.NextResponse(result.body, result)]);
673
679
  }
@@ -684,7 +690,7 @@ var MonoCloudNextClient = class {
684
690
  return res.status(401).json({ message: "unauthorized" });
685
691
  }
686
692
  if ((options === null || options === void 0 ? void 0 : options.groups) && !(0, _monocloud_auth_node_core_utils.isUserInGroup)(session.user, options.groups, options.groupsClaim ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM, options.matchAll)) {
687
- if (options.onAccessDenied) return options.onAccessDenied(req, res, session.user);
693
+ if (options.onGroupAccessDenied) return options.onGroupAccessDenied(req, res, session.user);
688
694
  return res.status(403).json({ message: "forbidden" });
689
695
  }
690
696
  return handler(req, res);
@@ -748,10 +754,9 @@ var MonoCloudNextClient = class {
748
754
  return mergeResponse([nxtResp, next_server_js.NextResponse.redirect(signInRoute)]);
749
755
  }
750
756
  const groupsClaim = (options === null || options === void 0 ? void 0 : options.groupsClaim) ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM;
751
- const onAccessDenied = options === null || options === void 0 ? void 0 : options.onAccessDenied;
752
757
  if (allowedGroups && !(0, _monocloud_auth_node_core_utils.isUserInGroup)(session.user, allowedGroups, groupsClaim)) {
753
- if (onAccessDenied) {
754
- const result = await onAccessDenied(req, evt, session.user);
758
+ if (options === null || options === void 0 ? void 0 : options.onGroupAccessDenied) {
759
+ const result = await options.onGroupAccessDenied(req, evt, session.user);
755
760
  if (result instanceof next_server_js.NextResponse) return mergeResponse([nxtResp, result]);
756
761
  if (result) return mergeResponse([nxtResp, new next_server_js.NextResponse(result.body, result)]);
757
762
  return next_server_js.NextResponse.next(nxtResp);
@@ -801,7 +806,7 @@ var MonoCloudNextClient = class {
801
806
  ({request, response} = getMonoCloudCookieReqRes(args[0], void 0));
802
807
  options = args[1];
803
808
  }
804
- else if (args.length === 2 && args[0] instanceof node_http.IncomingMessage && args[1] instanceof node_http.ServerResponse) ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
809
+ else if (args.length === 2 && isNodeRequest(args[0]) && isNodeResponse(args[1])) ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
805
810
  else {
806
811
  ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
807
812
  options = args[2];
@@ -914,7 +919,7 @@ var MonoCloudNextClient = class {
914
919
  groups = args[1];
915
920
  options = args[2];
916
921
  }
917
- if (args[0] instanceof node_http.IncomingMessage && args[1] instanceof node_http.ServerResponse) {
922
+ if (isNodeRequest(args[0]) && isNodeResponse(args[1])) {
918
923
  ({request, response} = getMonoCloudCookieReqRes(args[0], args[1]));
919
924
  groups = args[2];
920
925
  }