@monocloud/auth-nextjs 0.1.3 → 0.1.5

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,4 +1,4 @@
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-CsBjAJce.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
4
  import { IncomingMessage, ServerResponse } from "node:http";
@@ -268,8 +268,8 @@ declare class MonoCloudNextClient {
268
268
  *
269
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
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.**
271
+ * **Note⚠️ - When using groups to protect a page, 'Access Denied' is rendered by default when the user does not belong to the groups.
272
+ * To display a custom component, pass the `onGroupAccessDenied` parameter.**
273
273
  *
274
274
  * @param component The App Router server component that protectPage wraps and secures
275
275
  * @param options App Router `protectPage()` configuration options
@@ -308,9 +308,9 @@ declare class MonoCloudNextClient {
308
308
  *
309
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
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.**
311
+ * **Note⚠️ - When using groups to protect a page, the page will be rendered even if the user does not belong to the groups.
312
+ * You should check the props for `groupAccessDenied` boolean value to determine whether the user is
313
+ * allowed to access the page. Alternatively, you can pass `onGroupAccessDenied` parameter to return custom props.**
314
314
  *
315
315
  * @param options Pages Router `protectPage()` configuration options
316
316
  *
@@ -764,8 +764,12 @@ declare class MonoCloudNextClient {
764
764
  * @example SSR Component
765
765
  *
766
766
  * ```typescript
767
- * import { monoCloud } from "@/lib/monocloud";
768
- * import type { GetServerSideProps, InferGetServerSidePropsType } from "next";
767
+ * import { monoCloud } from "@/monocloud";
768
+ * import type {
769
+ * GetServerSideProps,
770
+ * GetServerSidePropsContext,
771
+ * InferGetServerSidePropsType,
772
+ * } from "next";
769
773
  *
770
774
  * type HomeProps = InferGetServerSidePropsType<typeof getServerSideProps>;
771
775
  *
@@ -773,15 +777,20 @@ declare class MonoCloudNextClient {
773
777
  * return <pre>Session: {JSON.stringify(session, null, 2)}</pre>;
774
778
  * }
775
779
  *
776
- * export const getServerSideProps: GetServerSideProps = async (context) => {
777
- * const session = await monoCloud.getSession(context.req, context.res);
780
+ * export const getServerSideProps = (async (
781
+ * context: GetServerSidePropsContext,
782
+ * ) => {
783
+ * const session = await monoCloud.getSession(
784
+ * context.req,
785
+ * context.res,
786
+ * );
778
787
  *
779
788
  * return {
780
789
  * props: {
781
790
  * session: session ?? null,
782
791
  * },
783
792
  * };
784
- * };
793
+ * }) satisfies GetServerSideProps;
785
794
  * ```
786
795
  */
787
796
  getSession(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>): Promise<MonoCloudSession$1 | undefined>;
@@ -868,7 +877,7 @@ declare class MonoCloudNextClient {
868
877
  *
869
878
  * The default token is an access token with scopes set through `MONOCLOUD_AUTH_SCOPES` or
870
879
  * `options.defaultAuthParams.scopes`, and resources set through `MONOCLOUD_AUTH_RESOURCE` or
871
- * `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without parameters.
880
+ * `options.defaultAuthParams.resource`. This token is refreshed when calling getTokens without resource and scopes parameters.
872
881
  *
873
882
  * ```typescript
874
883
  * import { NextResponse } from "next/server";
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
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
- import { ensureLeadingSlash, isAbsoluteUrl } from "@monocloud/auth-node-core/internal";
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
6
  import { IncomingMessage, ServerResponse } from "node:http";
@@ -221,11 +221,28 @@ const isMonoCloudResponse = (res) => res instanceof MonoCloudAppRouterResponse |
221
221
  const isAppRouter = (req) => req instanceof Request || req.headers instanceof Headers || typeof req.bodyUsed === "boolean";
222
222
  const getNextRequest = (req) => {
223
223
  if (req instanceof NextRequest) return req;
224
- return new NextRequest(req);
224
+ return new NextRequest(req.url, {
225
+ method: req.method,
226
+ headers: req.headers,
227
+ body: req.body,
228
+ duplex: req.duplex ?? "half"
229
+ });
225
230
  };
226
231
  const getNextResponse = (res) => {
227
232
  if (res instanceof NextResponse) return res;
228
- if (res instanceof Response) return new NextResponse(res.body, res);
233
+ if (res instanceof Response) {
234
+ const nextResponse = new NextResponse(res.body, {
235
+ status: res.status,
236
+ statusText: res.statusText,
237
+ headers: res.headers,
238
+ url: res.url
239
+ });
240
+ try {
241
+ /* v8 ignore else -- @preserve */
242
+ if (!isPresent(nextResponse.url)) nextResponse.url = res.url;
243
+ } catch {}
244
+ return nextResponse;
245
+ }
229
246
  return new NextResponse();
230
247
  };
231
248
  const getMonoCloudCookieReqRes = (req, resOrCtx) => {
@@ -441,7 +458,7 @@ var MonoCloudNextClient = class {
441
458
  constructor(options) {
442
459
  const opt = {
443
460
  ...options ?? {},
444
- userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.3`,
461
+ userAgent: (options === null || options === void 0 ? void 0 : options.userAgent) ?? `@monocloud/auth-nextjs@0.1.5`,
445
462
  debugger: (options === null || options === void 0 ? void 0 : options.debugger) ?? "@monocloud:auth-nextjs"
446
463
  };
447
464
  this.registerPublicEnvVariables();
@@ -560,7 +577,7 @@ var MonoCloudNextClient = class {
560
577
  return redirect(signInRoute.toString());
561
578
  }
562
579
  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)) {
563
- if (options.onAccessDenied) return options.onAccessDenied({
580
+ if (options.onGroupAccessDenied) return options.onGroupAccessDenied({
564
581
  ...params,
565
582
  user: session.user
566
583
  });
@@ -602,11 +619,11 @@ var MonoCloudNextClient = class {
602
619
  } };
603
620
  }
604
621
  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)) {
605
- var _options$onAccessDeni;
606
- const customProps$1 = await ((_options$onAccessDeni = options.onAccessDenied) === null || _options$onAccessDeni === void 0 ? void 0 : _options$onAccessDeni.call(options, {
622
+ var _options$onGroupAcces;
623
+ const customProps$1 = await ((_options$onGroupAcces = options.onGroupAccessDenied) === null || _options$onGroupAcces === void 0 ? void 0 : _options$onGroupAcces.call(options, {
607
624
  ...context,
608
625
  user: session.user
609
- })) ?? { props: { accessDenied: true } };
626
+ })) ?? { props: { groupAccessDenied: true } };
610
627
  return {
611
628
  ...customProps$1,
612
629
  props: { ...customProps$1.props ?? {} }
@@ -648,8 +665,8 @@ var MonoCloudNextClient = class {
648
665
  return mergeResponse([res, NextResponse.json({ message: "unauthorized" }, { status: 401 })]);
649
666
  }
650
667
  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)) {
651
- if (options.onAccessDenied) {
652
- const result = await options.onAccessDenied(req, ctx);
668
+ if (options.onGroupAccessDenied) {
669
+ const result = await options.onGroupAccessDenied(req, ctx, session.user);
653
670
  if (result instanceof NextResponse) return mergeResponse([res, result]);
654
671
  return mergeResponse([res, new NextResponse(result.body, result)]);
655
672
  }
@@ -666,7 +683,7 @@ var MonoCloudNextClient = class {
666
683
  return res.status(401).json({ message: "unauthorized" });
667
684
  }
668
685
  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)) {
669
- if (options.onAccessDenied) return options.onAccessDenied(req, res, session.user);
686
+ if (options.onGroupAccessDenied) return options.onGroupAccessDenied(req, res, session.user);
670
687
  return res.status(403).json({ message: "forbidden" });
671
688
  }
672
689
  return handler(req, res);
@@ -730,10 +747,9 @@ var MonoCloudNextClient = class {
730
747
  return mergeResponse([nxtResp, NextResponse.redirect(signInRoute)]);
731
748
  }
732
749
  const groupsClaim = (options === null || options === void 0 ? void 0 : options.groupsClaim) ?? process.env.MONOCLOUD_AUTH_GROUPS_CLAIM;
733
- const onAccessDenied = options === null || options === void 0 ? void 0 : options.onAccessDenied;
734
750
  if (allowedGroups && !isUserInGroup(session.user, allowedGroups, groupsClaim)) {
735
- if (onAccessDenied) {
736
- const result = await onAccessDenied(req, evt, session.user);
751
+ if (options === null || options === void 0 ? void 0 : options.onGroupAccessDenied) {
752
+ const result = await options.onGroupAccessDenied(req, evt, session.user);
737
753
  if (result instanceof NextResponse) return mergeResponse([nxtResp, result]);
738
754
  if (result) return mergeResponse([nxtResp, new NextResponse(result.body, result)]);
739
755
  return NextResponse.next(nxtResp);