@monocloud/auth-nextjs 0.1.5 → 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 +21 -4
- package/dist/{chunk-CbDLau6x.cjs → chunk-C0xms8kb.cjs} +1 -1
- package/dist/client/index.cjs +4 -3
- package/dist/client/index.d.mts +73 -118
- package/dist/client/index.mjs +2 -2
- package/dist/components/client/index.cjs +46 -88
- package/dist/components/client/index.cjs.map +1 -1
- package/dist/components/client/index.d.mts +59 -93
- package/dist/components/client/index.mjs +42 -85
- package/dist/components/client/index.mjs.map +1 -1
- package/dist/components/index.cjs +46 -42
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.mts +72 -49
- package/dist/components/index.mjs +44 -41
- package/dist/components/index.mjs.map +1 -1
- package/dist/index.cjs +399 -374
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +1546 -1835
- package/dist/index.mjs +393 -380
- package/dist/index.mjs.map +1 -1
- package/dist/{client-xfBYYato.cjs → protect-client-page-B1fOU4Zl.cjs} +65 -114
- package/dist/protect-client-page-B1fOU4Zl.cjs.map +1 -0
- package/dist/{client-D-3RMRNY.mjs → protect-client-page-BFlGkK8F.mjs} +63 -112
- package/dist/protect-client-page-BFlGkK8F.mjs.map +1 -0
- package/dist/types-xS_Me3Qg.d.mts +563 -0
- package/package.json +8 -7
- package/dist/client-D-3RMRNY.mjs.map +0 -1
- package/dist/client-xfBYYato.cjs.map +0 -1
- package/dist/types-CsBjAJce.d.mts +0 -410
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { c as ExtraAuthParams } from "../../types-xS_Me3Qg.mjs";
|
|
2
|
+
import { MonoCloudUser } from "@monocloud/auth-node-core";
|
|
3
|
+
import React from "react";
|
|
3
4
|
|
|
4
5
|
//#region src/components/client/redirect-to-signin.d.ts
|
|
5
6
|
/**
|
|
6
7
|
* Props for the `<RedirectToSignIn />` Component
|
|
8
|
+
*
|
|
9
|
+
* @category Types
|
|
7
10
|
*/
|
|
8
11
|
interface RedirectToSignInProps extends ExtraAuthParams {
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* URL to redirect the user to after sign-in.
|
|
11
14
|
*/
|
|
12
15
|
returnUrl?: string;
|
|
13
16
|
}
|
|
14
17
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* **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.**
|
|
18
|
+
* `<RedirectToSignIn>` is a **client-side component** that immediately redirects the user to the MonoCloud sign-in page when it is rendered.
|
|
18
19
|
*
|
|
19
|
-
*
|
|
20
|
+
* It does not render any UI.
|
|
20
21
|
*
|
|
21
|
-
*
|
|
22
|
+
* > This component must be used inside a Client Component (`"use client"`).
|
|
22
23
|
*
|
|
23
|
-
* @example
|
|
24
|
+
* @example Basic Usage
|
|
24
25
|
*
|
|
25
|
-
* ```tsx
|
|
26
|
+
* ```tsx title="Basic Usage"
|
|
26
27
|
* "use client";
|
|
27
28
|
*
|
|
28
29
|
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
@@ -39,11 +40,11 @@ interface RedirectToSignInProps extends ExtraAuthParams {
|
|
|
39
40
|
* }
|
|
40
41
|
* ```
|
|
41
42
|
*
|
|
42
|
-
* @example
|
|
43
|
+
* @example With Options
|
|
43
44
|
*
|
|
44
|
-
* You can customize the authorization request by passing in props.
|
|
45
|
+
* You can customize the authorization request by passing in props.
|
|
45
46
|
*
|
|
46
|
-
* ```tsx
|
|
47
|
+
* ```tsx title="With options"
|
|
47
48
|
* "use client";
|
|
48
49
|
*
|
|
49
50
|
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
@@ -53,48 +54,22 @@ interface RedirectToSignInProps extends ExtraAuthParams {
|
|
|
53
54
|
* const { isLoading, isAuthenticated } = useAuth();
|
|
54
55
|
*
|
|
55
56
|
* if (!isLoading && !isAuthenticated) {
|
|
56
|
-
* return
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @example Pages Router
|
|
64
|
-
*
|
|
65
|
-
* ```tsx
|
|
66
|
-
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
67
|
-
* import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
|
|
68
|
-
*
|
|
69
|
-
* export default function Home() {
|
|
70
|
-
* const { isLoading, isAuthenticated } = useAuth();
|
|
71
|
-
*
|
|
72
|
-
* if (!isLoading && !isAuthenticated) {
|
|
73
|
-
* return <RedirectToSignIn />;
|
|
57
|
+
* return (
|
|
58
|
+
* <RedirectToSignIn
|
|
59
|
+
* returnUrl="/dashboard"
|
|
60
|
+
* loginHint="user@example.com"
|
|
61
|
+
* />
|
|
62
|
+
* );
|
|
74
63
|
* }
|
|
75
64
|
*
|
|
76
65
|
* return <>You are signed in</>;
|
|
77
66
|
* }
|
|
78
67
|
* ```
|
|
79
68
|
*
|
|
80
|
-
* @
|
|
81
|
-
*
|
|
82
|
-
* You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
|
|
83
|
-
*
|
|
84
|
-
* ```tsx
|
|
85
|
-
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
86
|
-
* import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
|
|
87
|
-
*
|
|
88
|
-
* export default function Home() {
|
|
89
|
-
* const { isLoading, isAuthenticated } = useAuth();
|
|
90
|
-
*
|
|
91
|
-
* if (!isLoading && !isAuthenticated) {
|
|
92
|
-
* return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
|
|
93
|
-
* }
|
|
69
|
+
* @param props - The props for customizing RedirectToSignIn.
|
|
70
|
+
* @returns
|
|
94
71
|
*
|
|
95
|
-
*
|
|
96
|
-
* }
|
|
97
|
-
* ```
|
|
72
|
+
* @category Components
|
|
98
73
|
*/
|
|
99
74
|
declare const RedirectToSignIn: ({
|
|
100
75
|
returnUrl,
|
|
@@ -102,46 +77,48 @@ declare const RedirectToSignIn: ({
|
|
|
102
77
|
}: RedirectToSignInProps) => null;
|
|
103
78
|
//#endregion
|
|
104
79
|
//#region src/components/client/protected.d.ts
|
|
80
|
+
/**
|
|
81
|
+
* Props for the `<Protected />` component.
|
|
82
|
+
*
|
|
83
|
+
* @category Types
|
|
84
|
+
*/
|
|
105
85
|
interface ProtectedComponentProps {
|
|
106
86
|
/**
|
|
107
|
-
*
|
|
87
|
+
* Content to render when access is allowed.
|
|
108
88
|
*/
|
|
109
89
|
children: React.ReactNode;
|
|
110
90
|
/**
|
|
111
|
-
*
|
|
91
|
+
* Groups required to view the protected content. By default, the user must belong to **any** of the specified groups.
|
|
112
92
|
*/
|
|
113
93
|
groups?: string[];
|
|
114
94
|
/**
|
|
115
|
-
* Name of the claim
|
|
95
|
+
* Name of the claim that contains groups in the user profile.
|
|
96
|
+
* @defaultValue 'groups'
|
|
116
97
|
*/
|
|
117
98
|
groupsClaim?: string;
|
|
118
99
|
/**
|
|
119
|
-
*
|
|
100
|
+
* If `true`, the user must belong to **all** specified `groups` (instead of any).
|
|
101
|
+
* @defaultValue false
|
|
120
102
|
*/
|
|
121
103
|
matchAllGroups?: boolean;
|
|
122
104
|
/**
|
|
123
|
-
*
|
|
105
|
+
* Content to render when the user is not authenticated.
|
|
124
106
|
*/
|
|
125
107
|
fallback?: React.ReactNode;
|
|
126
108
|
/**
|
|
127
|
-
*
|
|
109
|
+
* 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).
|
|
128
110
|
*/
|
|
129
|
-
|
|
111
|
+
onGroupAccessDenied?: (user: MonoCloudUser) => React.ReactNode;
|
|
130
112
|
}
|
|
131
113
|
/**
|
|
132
|
-
*
|
|
133
|
-
* status and group membership.
|
|
114
|
+
* `<Protected>` conditionally renders its children based on the user’s authentication state and (optionally) group membership.
|
|
134
115
|
*
|
|
135
|
-
*
|
|
116
|
+
* > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser.
|
|
117
|
+
* > 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()}.
|
|
136
118
|
*
|
|
137
|
-
* @
|
|
138
|
-
*
|
|
139
|
-
* @returns The children if authorized, the `fallback` or `groupFallback` content if unauthenticated or unauthorized,
|
|
140
|
-
* or `null` while loading.
|
|
119
|
+
* @example Basic Usage
|
|
141
120
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* ```tsx
|
|
121
|
+
* ```tsx title="Basic Usage"
|
|
145
122
|
* "use client";
|
|
146
123
|
*
|
|
147
124
|
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
@@ -149,17 +126,15 @@ interface ProtectedComponentProps {
|
|
|
149
126
|
* export default function Home() {
|
|
150
127
|
* return (
|
|
151
128
|
* <Protected fallback={<>Sign in to view the message.</>}>
|
|
152
|
-
* <>
|
|
129
|
+
* <>This is the protected content.</>
|
|
153
130
|
* </Protected>
|
|
154
131
|
* );
|
|
155
132
|
* }
|
|
156
133
|
* ```
|
|
157
134
|
*
|
|
158
|
-
* @example
|
|
159
|
-
*
|
|
160
|
-
* See {@link ProtectedComponentProps}.
|
|
135
|
+
* @example With Groups
|
|
161
136
|
*
|
|
162
|
-
* ```tsx
|
|
137
|
+
* ```tsx title="With Groups"
|
|
163
138
|
* "use client";
|
|
164
139
|
*
|
|
165
140
|
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
@@ -167,8 +142,8 @@ interface ProtectedComponentProps {
|
|
|
167
142
|
* export default function Home() {
|
|
168
143
|
* return (
|
|
169
144
|
* <Protected
|
|
170
|
-
* groupFallback={<>Only admins are allowed.</>}
|
|
171
145
|
* groups={["admin"]}
|
|
146
|
+
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}
|
|
172
147
|
* >
|
|
173
148
|
* <>Signed in as admin</>
|
|
174
149
|
* </Protected>
|
|
@@ -176,39 +151,30 @@ interface ProtectedComponentProps {
|
|
|
176
151
|
* }
|
|
177
152
|
* ```
|
|
178
153
|
*
|
|
179
|
-
* @example
|
|
154
|
+
* @example Requiring all groups
|
|
180
155
|
*
|
|
181
|
-
* ```tsx
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* export default function Home() {
|
|
185
|
-
* return (
|
|
186
|
-
* <Protected fallback={<>Sign in to view the message.</>}>
|
|
187
|
-
* <>You are breathtaking</>
|
|
188
|
-
* </Protected>
|
|
189
|
-
* );
|
|
190
|
-
* }
|
|
191
|
-
* ```
|
|
192
|
-
*
|
|
193
|
-
* @example Pages Router with group options
|
|
194
|
-
*
|
|
195
|
-
* See {@link ProtectedComponentProps}.
|
|
156
|
+
* ```tsx title="Requiring all groups"
|
|
157
|
+
* "use client";
|
|
196
158
|
*
|
|
197
|
-
* ```tsx
|
|
198
159
|
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
199
160
|
*
|
|
200
161
|
* export default function Home() {
|
|
201
162
|
* return (
|
|
202
163
|
* <Protected
|
|
203
|
-
*
|
|
204
|
-
*
|
|
164
|
+
* groups={["admin", "billing"]}
|
|
165
|
+
* matchAllGroups
|
|
166
|
+
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}
|
|
205
167
|
* >
|
|
206
|
-
* <>
|
|
168
|
+
* <>Sensitive settings</>
|
|
207
169
|
* </Protected>
|
|
208
170
|
* );
|
|
209
171
|
* }
|
|
210
172
|
* ```
|
|
211
173
|
*
|
|
174
|
+
* @param props - Props for customizing the Protected component.
|
|
175
|
+
* @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.
|
|
176
|
+
*
|
|
177
|
+
* @category Components
|
|
212
178
|
*/
|
|
213
179
|
declare const Protected: ({
|
|
214
180
|
children,
|
|
@@ -216,8 +182,8 @@ declare const Protected: ({
|
|
|
216
182
|
groupsClaim,
|
|
217
183
|
matchAllGroups,
|
|
218
184
|
fallback,
|
|
219
|
-
|
|
220
|
-
}: ProtectedComponentProps) =>
|
|
185
|
+
onGroupAccessDenied
|
|
186
|
+
}: ProtectedComponentProps) => React.ReactNode | null;
|
|
221
187
|
//#endregion
|
|
222
188
|
export { Protected, type ProtectedComponentProps, RedirectToSignIn, type RedirectToSignInProps };
|
|
223
189
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
import { n as redirectToSignIn, r as useAuth } from "../../client-
|
|
1
|
+
import { n as redirectToSignIn, r as useAuth } from "../../protect-client-page-BFlGkK8F.mjs";
|
|
2
|
+
import "../../client/index.mjs";
|
|
2
3
|
import { isUserInGroup } from "@monocloud/auth-node-core/utils";
|
|
3
4
|
import React, { useEffect } from "react";
|
|
4
5
|
|
|
5
6
|
//#region src/components/client/redirect-to-signin.tsx
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
+
* `<RedirectToSignIn>` is a **client-side component** that immediately redirects the user to the MonoCloud sign-in page when it is rendered.
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
+
* It does not render any UI.
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
+
* > This component must be used inside a Client Component (`"use client"`).
|
|
12
13
|
*
|
|
13
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
* @example App Router
|
|
14
|
+
* @example Basic Usage
|
|
16
15
|
*
|
|
17
|
-
* ```tsx
|
|
16
|
+
* ```tsx title="Basic Usage"
|
|
18
17
|
* "use client";
|
|
19
18
|
*
|
|
20
19
|
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
@@ -31,11 +30,11 @@ import React, { useEffect } from "react";
|
|
|
31
30
|
* }
|
|
32
31
|
* ```
|
|
33
32
|
*
|
|
34
|
-
* @example
|
|
33
|
+
* @example With Options
|
|
35
34
|
*
|
|
36
|
-
* You can customize the authorization request by passing in props.
|
|
35
|
+
* You can customize the authorization request by passing in props.
|
|
37
36
|
*
|
|
38
|
-
* ```tsx
|
|
37
|
+
* ```tsx title="With options"
|
|
39
38
|
* "use client";
|
|
40
39
|
*
|
|
41
40
|
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
@@ -45,48 +44,22 @@ import React, { useEffect } from "react";
|
|
|
45
44
|
* const { isLoading, isAuthenticated } = useAuth();
|
|
46
45
|
*
|
|
47
46
|
* if (!isLoading && !isAuthenticated) {
|
|
48
|
-
* return
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @example Pages Router
|
|
56
|
-
*
|
|
57
|
-
* ```tsx
|
|
58
|
-
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
59
|
-
* import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
|
|
60
|
-
*
|
|
61
|
-
* export default function Home() {
|
|
62
|
-
* const { isLoading, isAuthenticated } = useAuth();
|
|
63
|
-
*
|
|
64
|
-
* if (!isLoading && !isAuthenticated) {
|
|
65
|
-
* return <RedirectToSignIn />;
|
|
47
|
+
* return (
|
|
48
|
+
* <RedirectToSignIn
|
|
49
|
+
* returnUrl="/dashboard"
|
|
50
|
+
* loginHint="user@example.com"
|
|
51
|
+
* />
|
|
52
|
+
* );
|
|
66
53
|
* }
|
|
67
54
|
*
|
|
68
55
|
* return <>You are signed in</>;
|
|
69
56
|
* }
|
|
70
57
|
* ```
|
|
71
58
|
*
|
|
72
|
-
* @
|
|
73
|
-
*
|
|
74
|
-
* You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
|
|
75
|
-
*
|
|
76
|
-
* ```tsx
|
|
77
|
-
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
78
|
-
* import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
|
|
79
|
-
*
|
|
80
|
-
* export default function Home() {
|
|
81
|
-
* const { isLoading, isAuthenticated } = useAuth();
|
|
82
|
-
*
|
|
83
|
-
* if (!isLoading && !isAuthenticated) {
|
|
84
|
-
* return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
|
|
85
|
-
* }
|
|
59
|
+
* @param props - The props for customizing RedirectToSignIn.
|
|
60
|
+
* @returns
|
|
86
61
|
*
|
|
87
|
-
*
|
|
88
|
-
* }
|
|
89
|
-
* ```
|
|
62
|
+
* @category Components
|
|
90
63
|
*/
|
|
91
64
|
const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
|
|
92
65
|
useEffect(() => {
|
|
@@ -101,19 +74,14 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
|
|
|
101
74
|
//#endregion
|
|
102
75
|
//#region src/components/client/protected.tsx
|
|
103
76
|
/**
|
|
104
|
-
*
|
|
105
|
-
* status and group membership.
|
|
106
|
-
*
|
|
107
|
-
* **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.**
|
|
77
|
+
* `<Protected>` conditionally renders its children based on the user’s authentication state and (optionally) group membership.
|
|
108
78
|
*
|
|
109
|
-
*
|
|
79
|
+
* > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser.
|
|
80
|
+
* > 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()}.
|
|
110
81
|
*
|
|
111
|
-
* @
|
|
112
|
-
* or `null` while loading.
|
|
82
|
+
* @example Basic Usage
|
|
113
83
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* ```tsx
|
|
84
|
+
* ```tsx title="Basic Usage"
|
|
117
85
|
* "use client";
|
|
118
86
|
*
|
|
119
87
|
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
@@ -121,17 +89,15 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
|
|
|
121
89
|
* export default function Home() {
|
|
122
90
|
* return (
|
|
123
91
|
* <Protected fallback={<>Sign in to view the message.</>}>
|
|
124
|
-
* <>
|
|
92
|
+
* <>This is the protected content.</>
|
|
125
93
|
* </Protected>
|
|
126
94
|
* );
|
|
127
95
|
* }
|
|
128
96
|
* ```
|
|
129
97
|
*
|
|
130
|
-
* @example
|
|
131
|
-
*
|
|
132
|
-
* See {@link ProtectedComponentProps}.
|
|
98
|
+
* @example With Groups
|
|
133
99
|
*
|
|
134
|
-
* ```tsx
|
|
100
|
+
* ```tsx title="With Groups"
|
|
135
101
|
* "use client";
|
|
136
102
|
*
|
|
137
103
|
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
@@ -139,8 +105,8 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
|
|
|
139
105
|
* export default function Home() {
|
|
140
106
|
* return (
|
|
141
107
|
* <Protected
|
|
142
|
-
* groupFallback={<>Only admins are allowed.</>}
|
|
143
108
|
* groups={["admin"]}
|
|
109
|
+
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}
|
|
144
110
|
* >
|
|
145
111
|
* <>Signed in as admin</>
|
|
146
112
|
* </Protected>
|
|
@@ -148,48 +114,39 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
|
|
|
148
114
|
* }
|
|
149
115
|
* ```
|
|
150
116
|
*
|
|
151
|
-
* @example
|
|
152
|
-
*
|
|
153
|
-
* ```tsx
|
|
154
|
-
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
117
|
+
* @example Requiring all groups
|
|
155
118
|
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* <Protected fallback={<>Sign in to view the message.</>}>
|
|
159
|
-
* <>You are breathtaking</>
|
|
160
|
-
* </Protected>
|
|
161
|
-
* );
|
|
162
|
-
* }
|
|
163
|
-
* ```
|
|
164
|
-
*
|
|
165
|
-
* @example Pages Router with group options
|
|
166
|
-
*
|
|
167
|
-
* See {@link ProtectedComponentProps}.
|
|
119
|
+
* ```tsx title="Requiring all groups"
|
|
120
|
+
* "use client";
|
|
168
121
|
*
|
|
169
|
-
* ```tsx
|
|
170
122
|
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
171
123
|
*
|
|
172
124
|
* export default function Home() {
|
|
173
125
|
* return (
|
|
174
126
|
* <Protected
|
|
175
|
-
*
|
|
176
|
-
*
|
|
127
|
+
* groups={["admin", "billing"]}
|
|
128
|
+
* matchAllGroups
|
|
129
|
+
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}
|
|
177
130
|
* >
|
|
178
|
-
* <>
|
|
131
|
+
* <>Sensitive settings</>
|
|
179
132
|
* </Protected>
|
|
180
133
|
* );
|
|
181
134
|
* }
|
|
182
135
|
* ```
|
|
183
136
|
*
|
|
137
|
+
* @param props - Props for customizing the Protected component.
|
|
138
|
+
* @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.
|
|
139
|
+
*
|
|
140
|
+
* @category Components
|
|
184
141
|
*/
|
|
185
|
-
const Protected = ({ children, groups, groupsClaim, matchAllGroups = false, fallback = null,
|
|
142
|
+
const Protected = ({ children, groups, groupsClaim, matchAllGroups = false, fallback = null, onGroupAccessDenied = () => /* @__PURE__ */ React.createElement(React.Fragment, null) }) => {
|
|
186
143
|
const { isLoading, error, isAuthenticated, user } = useAuth();
|
|
187
144
|
if (isLoading) return null;
|
|
188
145
|
if (error || !isAuthenticated || !user) {
|
|
189
|
-
if (fallback) return
|
|
146
|
+
if (fallback) return fallback;
|
|
190
147
|
return null;
|
|
191
148
|
}
|
|
192
|
-
return /* @__PURE__ */ React.createElement(React.Fragment, null, !groups || isUserInGroup(user, groups, groupsClaim ?? process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_GROUPS_CLAIM, matchAllGroups) ? children :
|
|
149
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, !groups || isUserInGroup(user, groups, groupsClaim ?? process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_GROUPS_CLAIM, matchAllGroups) ? children : onGroupAccessDenied(user));
|
|
193
150
|
};
|
|
194
151
|
|
|
195
152
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"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 *
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"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 user’s 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,iBAAgB;AACd,mBAAiB;GAAE;GAAW,GAAG;GAAY,CAAC;IAC7C,CAAC,YAAY,UAAU,CAAC;AAC3B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC6BT,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,WAAW,MACX,4BAA6C,yDAAK,OACG;CACrD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAAS,SAAS;AAE7D,KAAI,UACF,QAAO;AAGT,KAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;AACtC,MAAI,SACF,QAAO;AAGT,SAAO;;AAGT,QACE,0DACG,CAAC,UACF,cACE,MACA,QACA,eAAe,QAAQ,IAAI,yCAC3B,eACD,GACG,WACA,oBAAoB,KAAK,CAC5B"}
|