@monocloud/auth-nextjs 0.1.6 → 0.1.7

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 CHANGED
@@ -58,7 +58,7 @@ npm install @monocloud/auth-nextjs
58
58
 
59
59
  Create a `.env.local` file in your project root. The SDK automatically reads variables prefixed with `MONOCLOUD_AUTH__`.
60
60
 
61
- ```env
61
+ ```bash
62
62
  MONOCLOUD_AUTH_TENANT_DOMAIN=https://<your-tenant-domain>
63
63
  MONOCLOUD_AUTH_CLIENT_ID=<your-client-id>
64
64
  MONOCLOUD_AUTH_CLIENT_SECRET=<your-client-secret>
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_protect = require('../protect-BCIji2i7.cjs');
2
+ const require_protect_client_page = require('../protect-client-page-B1fOU4Zl.cjs');
3
3
 
4
- exports.protectPage = require_protect.protectPage;
5
- exports.useAuth = require_protect.useAuth;
4
+ exports.protectClientPage = require_protect_client_page.protectClientPage;
5
+ exports.useAuth = require_protect_client_page.useAuth;
@@ -1,12 +1,14 @@
1
- import { a as GroupOptions, i as ExtraAuthParams } from "../types-Cx32VRoI.mjs";
1
+ import { c as ExtraAuthParams, l as GroupOptions } from "../types-xS_Me3Qg.mjs";
2
2
  import { MonoCloudUser } from "@monocloud/auth-node-core";
3
3
  import React, { ComponentType } from "react";
4
4
 
5
5
  //#region src/client/use-auth.d.ts
6
6
  /**
7
7
  * Authentication State returned by `useAuth` hook.
8
+ *
9
+ * @category Types
8
10
  */
9
- interface AuthState {
11
+ interface AuthenticationState {
10
12
  /**
11
13
  * Flag indicating if the authentication state is still loading.
12
14
  */
@@ -31,32 +33,33 @@ interface AuthState {
31
33
  }
32
34
  /**
33
35
  *
34
- * Hook for getting the user's profile on client components
35
- *
36
- * @returns Authentication State
36
+ * `useAuth()` is a client-side hook that provides access to the current authentication state.
37
37
  *
38
- * @example App Router
38
+ * It can only be used inside **Client Components**.
39
39
  *
40
- * ```tsx
40
+ * @example Basic Usage
41
+ * ```tsx title="Basic Usage"
41
42
  * "use client";
42
43
  *
43
44
  * import { useAuth } from "@monocloud/auth-nextjs/client";
44
45
  *
45
46
  * export default function Home() {
46
- * const { user } = useAuth();
47
+ * const { user, isAuthenticated } = useAuth();
48
+ *
49
+ * if (!isAuthenticated) {
50
+ * return <>Not signed in</>;
51
+ * }
47
52
  *
48
53
  * return <>User Id: {user?.sub}</>;
49
54
  * }
50
55
  * ```
51
56
  *
52
- * @example App Router - Refetch user from Userinfo endpoint
53
- *
54
- * Calling `refetch(true)` will force refresh the user's profile from the userinfo endpoint.
55
- * If you do not intent to refersh from your tenants userinfo endpoint, use just `refetch()`
57
+ * @example Refetch user
56
58
  *
57
- * **Note⚠️: You need to set the env `MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES=true` or `allowQueryParamOverrides` should be `true` in the client initialization for force refresh to work**
59
+ * Calling `refetch(true)` forces a refresh of the user profile from the UserInfo endpoint.
60
+ * Calling `refetch()` refreshes authentication state without forcing a UserInfo request.
58
61
  *
59
- * ```tsx
62
+ * ```tsx title="Refetch User"
60
63
  * "use client";
61
64
  *
62
65
  * import { useAuth } from "@monocloud/auth-nextjs/client";
@@ -66,57 +69,28 @@ interface AuthState {
66
69
  *
67
70
  * return (
68
71
  * <>
69
- * <pre>{JSON.stringify(user)}</pre>
70
- * <button onClick={() => refetch(true)}>Refresh</button>
72
+ * <pre>{JSON.stringify(user, null, 2)}</pre>
73
+ * <button onClick={() => refetch(true)}>Refresh Profile</button>
71
74
  * </>
72
75
  * );
73
76
  * }
74
77
  * ```
75
78
  *
76
- * @example Pages Router
77
- *
78
- * ```tsx
79
- * import { useAuth } from "@monocloud/auth-nextjs/client";
80
- *
81
- * export default function Home() {
82
- * const { user } = useAuth();
83
- *
84
- * return <>User Id: {user?.sub}</>;
85
- * }
86
- * ```
87
- *
88
- * @example Pages Router - Refetch user from Userinfo endpoint
89
- *
90
- * Calling `refetch(true)` will force refresh the user's profile from the userinfo endpoint.
91
- * If you do not intent to refersh from your tenants userinfo endpoint, use just `refetch()`
92
- *
93
- * **Note⚠️: You need to set the env `MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES=true` or `allowQueryParamOverrides` should be `true` in the client initialization for force refresh to work**
94
- *
95
- * ```tsx
96
- * import { useAuth } from "@monocloud/auth-nextjs/client";
97
- *
98
- * export default function Home() {
99
- * const { user, refetch } = useAuth();
100
- *
101
- * return (
102
- * <>
103
- * <pre>{JSON.stringify(user)}</pre>
104
- * <button onClick={() => refetch(true)}>Refresh</button>
105
- * </>
106
- * );
107
- * }
108
- * ```
79
+ * @returns
109
80
  *
81
+ * @category Hooks
110
82
  */
111
- declare const useAuth: () => AuthState;
83
+ declare const useAuth: () => AuthenticationState;
112
84
  //#endregion
113
- //#region src/client/protect.d.ts
85
+ //#region src/client/protect-client-page.d.ts
114
86
  /**
115
87
  * Options for configuring page protection.
88
+ *
89
+ * @category Types
116
90
  */
117
- type ProtectPageOptions = {
91
+ interface ProtectClientPageOptions extends GroupOptions {
118
92
  /**
119
- *The url where the user will be redirected to after sign in
93
+ * The URL where the user will be redirected to after sign in.
120
94
  */
121
95
  returnUrl?: string;
122
96
  /**
@@ -139,56 +113,55 @@ type ProtectPageOptions = {
139
113
  * @returns JSX element to handle the error.
140
114
  */
141
115
  onError?: (error: Error) => React.ReactNode;
142
- } & GroupOptions;
116
+ }
143
117
  /**
144
- * Function to protect a client rendered page component.
145
- * Ensures that only authenticated users can access the component.
146
- *
147
- * **Note⚠️: Since `window.location` is set as `returnUrl` query param by default, you need to set the env `MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES=true` or `allowQueryParamOverrides` should be `true` in the client initialization for returning to the same page.**
118
+ * `protectClientPage()` wraps a **client-rendered page component** and ensures that only authenticated users can access it.
148
119
  *
149
- * @param Component - The component to protect.
150
- * @param options - The options.
120
+ * If the user is authenticated, the wrapped component receives a `user` prop.
151
121
  *
152
- * @returns Protected client rendered page component.
122
+ * > This function runs on the client and controls rendering only.
123
+ * > To enforce access before rendering (server-side), use the server {@link MonoCloudNextClient.protectPage | protectPage()} method on {@link MonoCloudNextClient}.
153
124
  *
154
- * @example App Router
125
+ * @example Basic Usage
155
126
  *
156
- * ```tsx
127
+ * ```tsx title="Basic Usage"
157
128
  * "use client";
158
129
  *
159
- * import { protectPage } from "@monocloud/auth-nextjs/client";
130
+ * import { protectClientPage } from "@monocloud/auth-nextjs/client";
160
131
  *
161
- * export default protectPage(function Home() {
162
- * return <>You are signed in</>;
132
+ * export default protectClientPage(function Home({ user }) {
133
+ * return <>Signed in as {user.email}</>;
163
134
  * });
164
135
  * ```
165
136
  *
166
- * @example App Router with options
137
+ * @example With Options
167
138
  *
168
- * See {@link ProtectPageOptions} for more options.
169
- *
170
- * ```tsx
139
+ * ```tsx title="With Options"
171
140
  * "use client";
172
141
  *
173
- * import { protectPage } from "@monocloud/auth-nextjs/client";
142
+ * import { protectClientPage } from "@monocloud/auth-nextjs/client";
174
143
  *
175
- * export default protectPage(
176
- * function Home() {
177
- * return <>You are signed in</>;
144
+ * export default protectClientPage(
145
+ * function Home({ user }) {
146
+ * return <>Signed in as {user.email}</>;
178
147
  * },
179
- * { returnUrl: "/dashboard", authParams: { loginHint: "username" } }
148
+ * {
149
+ * returnUrl: "/dashboard",
150
+ * authParams: { loginHint: "user@example.com" }
151
+ * }
180
152
  * );
181
153
  * ```
182
- * @example Fallback with onAccessDenied
183
154
  *
184
- * ```tsx
155
+ * @example Custom access denied UI
156
+ *
157
+ * ```tsx title="Custom access denied UI"
185
158
  * "use client";
186
159
  *
187
- * import { protectPage } from "@monocloud/auth-nextjs/client";
160
+ * import { protectClientPage } from "@monocloud/auth-nextjs/client";
188
161
  *
189
- * export default protectPage(
190
- * function Home() {
191
- * return <>You are signed in</>;
162
+ * export default protectClientPage(
163
+ * function Home({ user }) {
164
+ * return <>Signed in as {user.email}</>;
192
165
  * },
193
166
  * {
194
167
  * onAccessDenied: () => <div>Please sign in to continue</div>
@@ -196,16 +169,16 @@ type ProtectPageOptions = {
196
169
  * );
197
170
  * ```
198
171
  *
199
- * @example Group Protection with onGroupAccessDenied
172
+ * @example Group protection
200
173
  *
201
- * ```tsx
174
+ * ```tsx title="Group protection"
202
175
  * "use client";
203
176
  *
204
- * import { protectPage } from "@monocloud/auth-nextjs/client";
177
+ * import { protectClientPage } from "@monocloud/auth-nextjs/client";
205
178
  *
206
- * export default protectPage(
207
- * function Home() {
208
- * return <>Welcome Admin</>;
179
+ * export default protectClientPage(
180
+ * function Home({ user }) {
181
+ * return <>Welcome Admin {user.email}</>;
209
182
  * },
210
183
  * {
211
184
  * groups: ["admin"],
@@ -214,34 +187,16 @@ type ProtectPageOptions = {
214
187
  * );
215
188
  * ```
216
189
  *
217
- * @example Pages Router
218
- *
219
- * ```tsx
220
- * import { protectPage } from "@monocloud/auth-nextjs/client";
221
- *
222
- * export default protectPage(function Home() {
223
- * return <>You are signed in</>;
224
- * });
225
- * ```
190
+ * @param Component - The page component to protect
191
+ * @param options - Optional configuration
192
+ * @returns A protected React component.
226
193
  *
227
- * @example Pages Router with options
194
+ * @category Functions
228
195
  *
229
- * See {@link ProtectPageOptions} for more options.
230
- *
231
- * ```tsx
232
- * import { protectPage } from "@monocloud/auth-nextjs/client";
233
- *
234
- * export default protectPage(
235
- * function Home() {
236
- * return <>You are signed in</>;
237
- * },
238
- * { returnUrl: "/dashboard", authParams: { loginHint: "username" } }
239
- * );
240
- * ```
241
196
  */
242
- declare const protectPage: <P extends object>(Component: ComponentType<P & {
197
+ declare const protectClientPage: <P extends object>(Component: ComponentType<P & {
243
198
  user: MonoCloudUser;
244
- }>, options?: ProtectPageOptions) => React.FC<P>;
199
+ }>, options?: ProtectClientPageOptions) => React.FC<P>;
245
200
  //#endregion
246
- export { type AuthState, type ProtectPageOptions, protectPage, useAuth };
201
+ export { type AuthenticationState, type ProtectClientPageOptions, protectClientPage, useAuth };
247
202
  //# sourceMappingURL=index.d.mts.map
@@ -1,3 +1,3 @@
1
- import { r as useAuth, t as protectPage } from "../protect-K9srvUkq.mjs";
1
+ import { r as useAuth, t as protectClientPage } from "../protect-client-page-BFlGkK8F.mjs";
2
2
 
3
- export { protectPage, useAuth };
3
+ export { protectClientPage, useAuth };
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_chunk = require('../../chunk-C0xms8kb.cjs');
3
- const require_protect = require('../../protect-BCIji2i7.cjs');
3
+ const require_protect_client_page = require('../../protect-client-page-B1fOU4Zl.cjs');
4
4
  require('../../client/index.cjs');
5
5
  let _monocloud_auth_node_core_utils = require("@monocloud/auth-node-core/utils");
6
6
  let react = require("react");
@@ -8,17 +8,15 @@ react = require_chunk.__toESM(react);
8
8
 
9
9
  //#region src/components/client/redirect-to-signin.tsx
10
10
  /**
11
- * A client side component that will redirect users to the sign in page.
11
+ * `<RedirectToSignIn>` is a **client-side component** that immediately redirects the user to the MonoCloud sign-in page when it is rendered.
12
12
  *
13
- * **Note⚠️: Since `window.location` is set as `returnUrl` query param by default, you need to set the env `MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES=true` or `allowQueryParamOverrides` should be `true` in the client initialization for returning to the same page.**
13
+ * It does not render any UI.
14
14
  *
15
- * @param props - The props for customizing RedirectToSignIn
15
+ * > This component must be used inside a Client Component (`"use client"`).
16
16
  *
17
- * @returns
18
- *
19
- * @example App Router
17
+ * @example Basic Usage
20
18
  *
21
- * ```tsx
19
+ * ```tsx title="Basic Usage"
22
20
  * "use client";
23
21
  *
24
22
  * import { useAuth } from "@monocloud/auth-nextjs/client";
@@ -35,11 +33,11 @@ react = require_chunk.__toESM(react);
35
33
  * }
36
34
  * ```
37
35
  *
38
- * @example App Router with options
36
+ * @example With Options
39
37
  *
40
- * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
38
+ * You can customize the authorization request by passing in props.
41
39
  *
42
- * ```tsx
40
+ * ```tsx title="With options"
43
41
  * "use client";
44
42
  *
45
43
  * import { useAuth } from "@monocloud/auth-nextjs/client";
@@ -49,52 +47,26 @@ react = require_chunk.__toESM(react);
49
47
  * const { isLoading, isAuthenticated } = useAuth();
50
48
  *
51
49
  * if (!isLoading && !isAuthenticated) {
52
- * return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
53
- * }
54
- *
55
- * return <>You are signed in</>;
56
- * }
57
- * ```
58
- *
59
- * @example Pages Router
60
- *
61
- * ```tsx
62
- * import { useAuth } from "@monocloud/auth-nextjs/client";
63
- * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
64
- *
65
- * export default function Home() {
66
- * const { isLoading, isAuthenticated } = useAuth();
67
- *
68
- * if (!isLoading && !isAuthenticated) {
69
- * return <RedirectToSignIn />;
50
+ * return (
51
+ * <RedirectToSignIn
52
+ * returnUrl="/dashboard"
53
+ * loginHint="user@example.com"
54
+ * />
55
+ * );
70
56
  * }
71
57
  *
72
58
  * return <>You are signed in</>;
73
59
  * }
74
60
  * ```
75
61
  *
76
- * @example Pages Router with options
77
- *
78
- * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
79
- *
80
- * ```tsx
81
- * import { useAuth } from "@monocloud/auth-nextjs/client";
82
- * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
83
- *
84
- * export default function Home() {
85
- * const { isLoading, isAuthenticated } = useAuth();
86
- *
87
- * if (!isLoading && !isAuthenticated) {
88
- * return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
89
- * }
62
+ * @param props - The props for customizing RedirectToSignIn.
63
+ * @returns
90
64
  *
91
- * return <>You are signed in</>;
92
- * }
93
- * ```
65
+ * @category Components
94
66
  */
95
67
  const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
96
68
  (0, react.useEffect)(() => {
97
- require_protect.redirectToSignIn({
69
+ require_protect_client_page.redirectToSignIn({
98
70
  returnUrl,
99
71
  ...authParams
100
72
  });
@@ -105,19 +77,14 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
105
77
  //#endregion
106
78
  //#region src/components/client/protected.tsx
107
79
  /**
108
- * A wrapper component that conditionally renders its children based on the user's authentication
109
- * status and group membership.
110
- *
111
- * **Note⚠️: The component is hidden from view. The data is present in the browser. Use server side `protectPage()` for protecting components before that data is sent to the client.**
80
+ * `<Protected>` conditionally renders its children based on the users authentication state and (optionally) group membership.
112
81
  *
113
- * @param props - Props for customizing the Protected component.
82
+ * > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser.
83
+ * > To enforce access before content is rendered or sent to the client, use server-side protection such as {@link MonoCloudNextClient.protectPage | protectPage()}, or {@link MonoCloudNextClient.protect | protect()}.
114
84
  *
115
- * @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized,
116
- * or `null` while loading.
85
+ * @example Basic Usage
117
86
  *
118
- * @example App Router
119
- *
120
- * ```tsx
87
+ * ```tsx title="Basic Usage"
121
88
  * "use client";
122
89
  *
123
90
  * import { Protected } from "@monocloud/auth-nextjs/components/client";
@@ -125,17 +92,15 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
125
92
  * export default function Home() {
126
93
  * return (
127
94
  * <Protected fallback={<>Sign in to view the message.</>}>
128
- * <>You are breathtaking</>
95
+ * <>This is the protected content.</>
129
96
  * </Protected>
130
97
  * );
131
98
  * }
132
99
  * ```
133
100
  *
134
- * @example App Router with group options
135
- *
136
- * See {@link ProtectedComponentProps}.
101
+ * @example With Groups
137
102
  *
138
- * ```tsx
103
+ * ```tsx title="With Groups"
139
104
  * "use client";
140
105
  *
141
106
  * import { Protected } from "@monocloud/auth-nextjs/components/client";
@@ -143,8 +108,8 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
143
108
  * export default function Home() {
144
109
  * return (
145
110
  * <Protected
146
- * onGroupAccessDenied={() => <>Only admins are allowed.</>}
147
111
  * groups={["admin"]}
112
+ * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}
148
113
  * >
149
114
  * <>Signed in as admin</>
150
115
  * </Protected>
@@ -152,42 +117,33 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
152
117
  * }
153
118
  * ```
154
119
  *
155
- * @example Pages Router
156
- *
157
- * ```tsx
158
- * import { Protected } from "@monocloud/auth-nextjs/components/client";
120
+ * @example Requiring all groups
159
121
  *
160
- * export default function Home() {
161
- * return (
162
- * <Protected fallback={<>Sign in to view the message.</>}>
163
- * <>You are breathtaking</>
164
- * </Protected>
165
- * );
166
- * }
167
- * ```
168
- *
169
- * @example Pages Router with group options
170
- *
171
- * See {@link ProtectedComponentProps}.
122
+ * ```tsx title="Requiring all groups"
123
+ * "use client";
172
124
  *
173
- * ```tsx
174
125
  * import { Protected } from "@monocloud/auth-nextjs/components/client";
175
126
  *
176
127
  * export default function Home() {
177
128
  * return (
178
129
  * <Protected
179
- * onGroupAccessDenied={(user) => <>User {user?.email} is not allowed.</>}
180
- * groups={["admin"]}
130
+ * groups={["admin", "billing"]}
131
+ * matchAllGroups
132
+ * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}
181
133
  * >
182
- * <>Signed in as admin</>
134
+ * <>Sensitive settings</>
183
135
  * </Protected>
184
136
  * );
185
137
  * }
186
138
  * ```
187
139
  *
140
+ * @param props - Props for customizing the Protected component.
141
+ * @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.
142
+ *
143
+ * @category Components
188
144
  */
189
145
  const Protected = ({ children, groups, groupsClaim, matchAllGroups = false, fallback = null, onGroupAccessDenied = () => /* @__PURE__ */ react.default.createElement(react.default.Fragment, null) }) => {
190
- const { isLoading, error, isAuthenticated, user } = require_protect.useAuth();
146
+ const { isLoading, error, isAuthenticated, user } = require_protect_client_page.useAuth();
191
147
  if (isLoading) return null;
192
148
  if (error || !isAuthenticated || !user) {
193
149
  if (fallback) return fallback;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["useAuth"],"sources":["../../../src/components/client/redirect-to-signin.tsx","../../../src/components/client/protected.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { redirectToSignIn } from '../../client/protect';\nimport { ExtraAuthParams } from '../../types';\n\n/**\n * Props for the `<RedirectToSignIn />` Component\n */\nexport interface RedirectToSignInProps extends ExtraAuthParams {\n /**\n * The url where the user will be redirected to after sign in.\n */\n returnUrl?: string;\n}\n\n/**\n * A client side component that will redirect users to the sign in page.\n *\n * **Note⚠️: Since `window.location` is set as `returnUrl` query param by default, you need to set the env `MONOCLOUD_AUTH_ALLOW_QUERY_PARAM_OVERRIDES=true` or `allowQueryParamOverrides` should be `true` in the client initialization for returning to the same page.**\n *\n * @param props - The props for customizing RedirectToSignIn\n *\n * @returns\n *\n * @example App Router\n *\n * ```tsx\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n * import { RedirectToSignIn } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * const { isLoading, isAuthenticated } = useAuth();\n *\n * if (!isLoading && !isAuthenticated) {\n * return <RedirectToSignIn />;\n * }\n *\n * return <>You are signed in</>;\n * }\n * ```\n *\n * @example App Router with options\n *\n * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.\n *\n * ```tsx\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n * import { RedirectToSignIn } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * const { isLoading, isAuthenticated } = useAuth();\n *\n * if (!isLoading && !isAuthenticated) {\n * return <RedirectToSignIn returnUrl=\"/dashboard\" loginHint=\"username\" />;\n * }\n *\n * return <>You are signed in</>;\n * }\n * ```\n *\n * @example Pages Router\n *\n * ```tsx\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n * import { RedirectToSignIn } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * const { isLoading, isAuthenticated } = useAuth();\n *\n * if (!isLoading && !isAuthenticated) {\n * return <RedirectToSignIn />;\n * }\n *\n * return <>You are signed in</>;\n * }\n * ```\n *\n * @example Pages Router with options\n *\n * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.\n *\n * ```tsx\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n * import { RedirectToSignIn } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * const { isLoading, isAuthenticated } = useAuth();\n *\n * if (!isLoading && !isAuthenticated) {\n * return <RedirectToSignIn returnUrl=\"/dashboard\" loginHint=\"username\" />;\n * }\n *\n * return <>You are signed in</>;\n * }\n * ```\n */\nexport const RedirectToSignIn = ({\n returnUrl,\n ...authParams\n}: RedirectToSignInProps): null => {\n useEffect(() => {\n redirectToSignIn({ returnUrl, ...authParams });\n }, [authParams, returnUrl]);\n return null;\n};\n","import { isUserInGroup } from '@monocloud/auth-node-core/utils';\nimport React from 'react';\nimport { useAuth } from '../../client';\nimport type { MonoCloudUser } from '@monocloud/auth-node-core';\n\nexport interface ProtectedComponentProps {\n /**\n * Components that should be rendered if the user is authenticated.\n */\n children: React.ReactNode;\n\n /**\n * A list of group names or IDs to which the user must belong to. The user should belong to atleast one of the specified groups.\n */\n groups?: string[];\n\n /**\n * Name of the claim of user's groups. default: `groups`.\n */\n groupsClaim?: string;\n\n /**\n * Flag indicating if all groups specified should be present in the users profile. default: false.\n */\n matchAllGroups?: boolean;\n\n /**\n * A fallback component that should render if the user is not authenticated.\n */\n fallback?: React.ReactNode;\n\n /**\n * A fallback component that should render if the user is authenticated but does not belong to the required groups.\n */\n onGroupAccessDenied?: (user?: MonoCloudUser) => React.ReactNode;\n}\n\n/**\n * A wrapper component that conditionally renders its children based on the user's authentication\n * status and group membership.\n *\n * **Note⚠️: The component is hidden from view. The data is present in the browser. Use server side `protectPage()` for protecting components before that data is sent to the client.**\n *\n * @param props - Props for customizing the Protected component.\n *\n * @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized,\n * or `null` while loading.\n *\n * @example App Router\n *\n * ```tsx\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * return (\n * <Protected fallback={<>Sign in to view the message.</>}>\n * <>You are breathtaking</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @example App Router with group options\n *\n * See {@link ProtectedComponentProps}.\n *\n * ```tsx\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * return (\n * <Protected\n * onGroupAccessDenied={() => <>Only admins are allowed.</>}\n * groups={[\"admin\"]}\n * >\n * <>Signed in as admin</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @example Pages Router\n *\n * ```tsx\n * import { Protected } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * return (\n * <Protected fallback={<>Sign in to view the message.</>}>\n * <>You are breathtaking</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @example Pages Router with group options\n *\n * See {@link ProtectedComponentProps}.\n *\n * ```tsx\n * import { Protected } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * return (\n * <Protected\n * onGroupAccessDenied={(user) => <>User {user?.email} is not allowed.</>}\n * groups={[\"admin\"]}\n * >\n * <>Signed in as admin</>\n * </Protected>\n * );\n * }\n * ```\n *\n */\nexport const Protected = ({\n children,\n groups,\n groupsClaim,\n matchAllGroups = false,\n fallback = null,\n onGroupAccessDenied = (): React.ReactNode => <></>,\n}: ProtectedComponentProps): React.ReactNode | null => {\n const { isLoading, error, isAuthenticated, user } = useAuth();\n\n if (isLoading) {\n return null;\n }\n\n if (error || !isAuthenticated || !user) {\n if (fallback) {\n return fallback;\n }\n\n return null;\n }\n\n return (\n <>\n {!groups ||\n isUserInGroup(\n user,\n groups,\n groupsClaim ?? process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_GROUPS_CLAIM,\n matchAllGroups\n )\n ? children\n : onGroupAccessDenied(user)}\n </>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqGA,MAAa,oBAAoB,EAC/B,WACA,GAAG,iBAC8B;AACjC,4BAAgB;AACd,mCAAiB;GAAE;GAAW,GAAG;GAAY,CAAC;IAC7C,CAAC,YAAY,UAAU,CAAC;AAC3B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACWT,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,WAAW,MACX,4BAA6C,yEAAK,OACG;CACrD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAASA,yBAAS;AAE7D,KAAI,UACF,QAAO;AAGT,KAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;AACtC,MAAI,SACF,QAAO;AAGT,SAAO;;AAGT,QACE,0EACG,CAAC,6DAEA,MACA,QACA,eAAe,QAAQ,IAAI,yCAC3B,eACD,GACG,WACA,oBAAoB,KAAK,CAC5B"}
1
+ {"version":3,"file":"index.cjs","names":["useAuth"],"sources":["../../../src/components/client/redirect-to-signin.tsx","../../../src/components/client/protected.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { redirectToSignIn } from '../../client/protect-client-page';\nimport { ExtraAuthParams } from '../../types';\n\n/**\n * Props for the `<RedirectToSignIn />` Component\n *\n * @category Types\n */\nexport interface RedirectToSignInProps extends ExtraAuthParams {\n /**\n * URL to redirect the user to after sign-in.\n */\n returnUrl?: string;\n}\n\n/**\n * `<RedirectToSignIn>` is a **client-side component** that immediately redirects the user to the MonoCloud sign-in page when it is rendered.\n *\n * It does not render any UI.\n *\n * > This component must be used inside a Client Component (`\"use client\"`).\n *\n * @example Basic Usage\n *\n * ```tsx title=\"Basic Usage\"\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n * import { RedirectToSignIn } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * const { isLoading, isAuthenticated } = useAuth();\n *\n * if (!isLoading && !isAuthenticated) {\n * return <RedirectToSignIn />;\n * }\n *\n * return <>You are signed in</>;\n * }\n * ```\n *\n * @example With Options\n *\n * You can customize the authorization request by passing in props.\n *\n * ```tsx title=\"With options\"\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n * import { RedirectToSignIn } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * const { isLoading, isAuthenticated } = useAuth();\n *\n * if (!isLoading && !isAuthenticated) {\n * return (\n * <RedirectToSignIn\n * returnUrl=\"/dashboard\"\n * loginHint=\"user@example.com\"\n * />\n * );\n * }\n *\n * return <>You are signed in</>;\n * }\n * ```\n *\n * @param props - The props for customizing RedirectToSignIn.\n * @returns\n *\n * @category Components\n */\nexport const RedirectToSignIn = ({\n returnUrl,\n ...authParams\n}: RedirectToSignInProps): null => {\n useEffect(() => {\n redirectToSignIn({ returnUrl, ...authParams });\n }, [authParams, returnUrl]);\n return null;\n};\n","import { isUserInGroup } from '@monocloud/auth-node-core/utils';\nimport React from 'react';\nimport { useAuth } from '../../client';\nimport type { MonoCloudUser } from '@monocloud/auth-node-core';\nimport type { MonoCloudNextClient } from '../../monocloud-next-client';\n\n/**\n * Props for the `<Protected />` component.\n *\n * @category Types\n */\nexport interface ProtectedComponentProps {\n /**\n * Content to render when access is allowed.\n */\n children: React.ReactNode;\n\n /**\n * Groups required to view the protected content. By default, the user must belong to **any** of the specified groups.\n */\n groups?: string[];\n\n /**\n * Name of the claim that contains groups in the user profile.\n * @defaultValue 'groups'\n */\n groupsClaim?: string;\n\n /**\n * If `true`, the user must belong to **all** specified `groups` (instead of any).\n * @defaultValue false\n */\n matchAllGroups?: boolean;\n\n /**\n * Content to render when the user is not authenticated.\n */\n fallback?: React.ReactNode;\n\n /**\n * Rendered when the user is authenticated but does not meet the `groups` requirement. If omitted, nothing is rendered (or `fallback` is used only for unauthenticated users).\n */\n onGroupAccessDenied?: (user: MonoCloudUser) => React.ReactNode;\n}\n\n/**\n * `<Protected>` conditionally renders its children based on the users authentication state and (optionally) group membership.\n *\n * > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser.\n * > To enforce access before content is rendered or sent to the client, use server-side protection such as {@link MonoCloudNextClient.protectPage | protectPage()}, or {@link MonoCloudNextClient.protect | protect()}.\n *\n * @example Basic Usage\n *\n * ```tsx title=\"Basic Usage\"\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * return (\n * <Protected fallback={<>Sign in to view the message.</>}>\n * <>This is the protected content.</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @example With Groups\n *\n * ```tsx title=\"With Groups\"\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * return (\n * <Protected\n * groups={[\"admin\"]}\n * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}\n * >\n * <>Signed in as admin</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @example Requiring all groups\n *\n * ```tsx title=\"Requiring all groups\"\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-nextjs/components/client\";\n *\n * export default function Home() {\n * return (\n * <Protected\n * groups={[\"admin\", \"billing\"]}\n * matchAllGroups\n * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}\n * >\n * <>Sensitive settings</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @param props - Props for customizing the Protected component.\n * @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.\n *\n * @category Components\n */\nexport const Protected = ({\n children,\n groups,\n groupsClaim,\n matchAllGroups = false,\n fallback = null,\n onGroupAccessDenied = (): React.ReactNode => <></>,\n}: ProtectedComponentProps): React.ReactNode | null => {\n const { isLoading, error, isAuthenticated, user } = useAuth();\n\n if (isLoading) {\n return null;\n }\n\n if (error || !isAuthenticated || !user) {\n if (fallback) {\n return fallback;\n }\n\n return null;\n }\n\n return (\n <>\n {!groups ||\n isUserInGroup(\n user,\n groups,\n groupsClaim ?? process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_GROUPS_CLAIM,\n matchAllGroups\n )\n ? children\n : onGroupAccessDenied(user)}\n </>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2EA,MAAa,oBAAoB,EAC/B,WACA,GAAG,iBAC8B;AACjC,4BAAgB;AACd,+CAAiB;GAAE;GAAW,GAAG;GAAY,CAAC;IAC7C,CAAC,YAAY,UAAU,CAAC;AAC3B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC6BT,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,WAAW,MACX,4BAA6C,yEAAK,OACG;CACrD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAASA,qCAAS;AAE7D,KAAI,UACF,QAAO;AAGT,KAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;AACtC,MAAI,SACF,QAAO;AAGT,SAAO;;AAGT,QACE,0EACG,CAAC,6DAEA,MACA,QACA,eAAe,QAAQ,IAAI,yCAC3B,eACD,GACG,WACA,oBAAoB,KAAK,CAC5B"}