@monocloud/auth-nextjs 0.1.1 → 0.1.2

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.
@@ -1,5 +1,5 @@
1
1
  const require_chunk = require('../../chunk-CbDLau6x.cjs');
2
- const require_client = require('../../client-BjnSJS59.cjs');
2
+ const require_client = require('../../client-Be6A2vEn.cjs');
3
3
  let _monocloud_auth_node_core_utils = require("@monocloud/auth-node-core/utils");
4
4
  let react = require("react");
5
5
  react = require_chunk.__toESM(react);
@@ -8,7 +8,87 @@ react = require_chunk.__toESM(react);
8
8
  /**
9
9
  * A client side component that will redirect users to the sign in page.
10
10
  *
11
- * @type RedirectToSignInProps
11
+ * **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.**
12
+ *
13
+ * @param props - The props for customizing RedirectToSignIn
14
+ *
15
+ * @returns
16
+ *
17
+ * @example App Router
18
+ *
19
+ * ```tsx
20
+ * "use client";
21
+ *
22
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
23
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
24
+ *
25
+ * export default function Home() {
26
+ * const { isLoading, isAuthenticated } = useAuth();
27
+ *
28
+ * if (!isLoading && !isAuthenticated) {
29
+ * return <RedirectToSignIn />;
30
+ * }
31
+ *
32
+ * return <>You are signed in</>;
33
+ * }
34
+ * ```
35
+ *
36
+ * @example App Router with options
37
+ *
38
+ * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
39
+ *
40
+ * ```tsx
41
+ * "use client";
42
+ *
43
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
44
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
45
+ *
46
+ * export default function Home() {
47
+ * const { isLoading, isAuthenticated } = useAuth();
48
+ *
49
+ * if (!isLoading && !isAuthenticated) {
50
+ * return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
51
+ * }
52
+ *
53
+ * return <>You are signed in</>;
54
+ * }
55
+ * ```
56
+ *
57
+ * @example Pages Router
58
+ *
59
+ * ```tsx
60
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
61
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
62
+ *
63
+ * export default function Home() {
64
+ * const { isLoading, isAuthenticated } = useAuth();
65
+ *
66
+ * if (!isLoading && !isAuthenticated) {
67
+ * return <RedirectToSignIn />;
68
+ * }
69
+ *
70
+ * return <>You are signed in</>;
71
+ * }
72
+ * ```
73
+ *
74
+ * @example Pages Router with options
75
+ *
76
+ * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
77
+ *
78
+ * ```tsx
79
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
80
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
81
+ *
82
+ * export default function Home() {
83
+ * const { isLoading, isAuthenticated } = useAuth();
84
+ *
85
+ * if (!isLoading && !isAuthenticated) {
86
+ * return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
87
+ * }
88
+ *
89
+ * return <>You are signed in</>;
90
+ * }
91
+ * ```
12
92
  */
13
93
  const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
14
94
  (0, react.useEffect)(() => {
@@ -26,10 +106,83 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
26
106
  * A wrapper component that conditionally renders its children based on the user's authentication
27
107
  * status and group membership.
28
108
  *
29
- * @param props - The properties for the Protected component.
109
+ * **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.**
110
+ *
111
+ * @param props - Props for customizing the Protected component.
30
112
  *
31
113
  * @returns The children if authorized, the `onAccessDenied` content if unauthorized,
32
114
  * or `null` while loading.
115
+ *
116
+ * @example App Router
117
+ *
118
+ * ```tsx
119
+ * "use client";
120
+ *
121
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
122
+ *
123
+ * export default function Home() {
124
+ * return (
125
+ * <Protected onAccessDenied={<>Sign in to view the message.</>}>
126
+ * <>You are breathtaking</>
127
+ * </Protected>
128
+ * );
129
+ * }
130
+ * ```
131
+ *
132
+ * @example App Router with group options
133
+ *
134
+ * See {@link ProtectedComponentProps}.
135
+ *
136
+ * ```tsx
137
+ * "use client";
138
+ *
139
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
140
+ *
141
+ * export default function Home() {
142
+ * return (
143
+ * <Protected
144
+ * onAccessDenied={<>Only admins are allowed.</>}
145
+ * groups={["admin"]}
146
+ * >
147
+ * <>Signed in as admin</>
148
+ * </Protected>
149
+ * );
150
+ * }
151
+ * ```
152
+ *
153
+ * @example Pages Router
154
+ *
155
+ * ```tsx
156
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
157
+ *
158
+ * export default function Home() {
159
+ * return (
160
+ * <Protected onAccessDenied={<>Sign in to view the message.</>}>
161
+ * <>You are breathtaking</>
162
+ * </Protected>
163
+ * );
164
+ * }
165
+ * ```
166
+ *
167
+ * @example Pages Router with group options
168
+ *
169
+ * See {@link ProtectedComponentProps}.
170
+ *
171
+ * ```tsx
172
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
173
+ *
174
+ * export default function Home() {
175
+ * return (
176
+ * <Protected
177
+ * onAccessDenied={<>Only admins are allowed.</>}
178
+ * groups={["admin"]}
179
+ * >
180
+ * <>Signed in as admin</>
181
+ * </Protected>
182
+ * );
183
+ * }
184
+ * ```
185
+ *
33
186
  */
34
187
  const Protected = ({ children, groups, groupsClaim, matchAllGroups = false, onAccessDenied = null }) => {
35
188
  const { isLoading, error, isAuthenticated, user } = require_client.useAuth();
@@ -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 * @type RedirectToSignInProps\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, { JSX } from 'react';\nimport { useAuth } from '../../client';\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 onAccessDenied?: 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 * @param props - The properties for the Protected component.\n *\n * @returns The children if authorized, the `onAccessDenied` content if unauthorized,\n * or `null` while loading.\n */\nexport const Protected = ({\n children,\n groups,\n groupsClaim,\n matchAllGroups = false,\n onAccessDenied = null,\n}: ProtectedComponentProps): JSX.Element | null => {\n const { isLoading, error, isAuthenticated, user } = useAuth();\n\n if (isLoading) {\n return null;\n }\n\n if (error || !isAuthenticated || !user) {\n if (onAccessDenied) {\n return <>{onAccessDenied}</>;\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 : onAccessDenied}\n </>\n );\n};\n"],"mappings":";;;;;;;;;;;;AAqBA,MAAa,oBAAoB,EAC/B,WACA,GAAG,iBAC8B;AACjC,4BAAgB;AACd,kCAAiB;GAAE;GAAW,GAAG;GAAY,CAAC;IAC7C,CAAC,YAAY,UAAU,CAAC;AAC3B,QAAO;;;;;;;;;;;;;;ACYT,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,iBAAiB,WACgC;CACjD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAASA,wBAAS;AAE7D,KAAI,UACF,QAAO;AAGT,KAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;AACtC,MAAI,eACF,QAAO,0EAAG,eAAkB;AAG9B,SAAO;;AAGT,QACE,0EACG,CAAC,6DAEA,MACA,QACA,eAAe,QAAQ,IAAI,yCAC3B,eACD,GACG,WACA,eACH"}
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, { JSX } from 'react';\nimport { useAuth } from '../../client';\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 onAccessDenied?: 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 `onAccessDenied` content if 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 onAccessDenied={<>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 * onAccessDenied={<>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 onAccessDenied={<>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 * onAccessDenied={<>Only admins are 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 onAccessDenied = null,\n}: ProtectedComponentProps): JSX.Element | null => {\n const { isLoading, error, isAuthenticated, user } = useAuth();\n\n if (isLoading) {\n return null;\n }\n\n if (error || !isAuthenticated || !user) {\n if (onAccessDenied) {\n return <>{onAccessDenied}</>;\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 : onAccessDenied}\n </>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqGA,MAAa,oBAAoB,EAC/B,WACA,GAAG,iBAC8B;AACjC,4BAAgB;AACd,kCAAiB;GAAE;GAAW,GAAG;GAAY,CAAC;IAC7C,CAAC,YAAY,UAAU,CAAC;AAC3B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACKT,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,iBAAiB,WACgC;CACjD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAASA,wBAAS;AAE7D,KAAI,UACF,QAAO;AAGT,KAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;AACtC,MAAI,eACF,QAAO,0EAAG,eAAkB;AAG9B,SAAO;;AAGT,QACE,0EACG,CAAC,6DAEA,MACA,QACA,eAAe,QAAQ,IAAI,yCAC3B,eACD,GACG,WACA,eACH"}
@@ -1,4 +1,4 @@
1
- import { i as ExtraAuthParams } from "../../types-BleaXQUP.mjs";
1
+ import { i as ExtraAuthParams } from "../../types-DOfZTKa6.mjs";
2
2
  import React, { JSX } from "react";
3
3
 
4
4
  //#region src/components/client/redirect-to-signin.d.ts
@@ -14,7 +14,87 @@ interface RedirectToSignInProps extends ExtraAuthParams {
14
14
  /**
15
15
  * A client side component that will redirect users to the sign in page.
16
16
  *
17
- * @type RedirectToSignInProps
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
+ *
19
+ * @param props - The props for customizing RedirectToSignIn
20
+ *
21
+ * @returns
22
+ *
23
+ * @example App Router
24
+ *
25
+ * ```tsx
26
+ * "use client";
27
+ *
28
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
29
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
30
+ *
31
+ * export default function Home() {
32
+ * const { isLoading, isAuthenticated } = useAuth();
33
+ *
34
+ * if (!isLoading && !isAuthenticated) {
35
+ * return <RedirectToSignIn />;
36
+ * }
37
+ *
38
+ * return <>You are signed in</>;
39
+ * }
40
+ * ```
41
+ *
42
+ * @example App Router with options
43
+ *
44
+ * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
45
+ *
46
+ * ```tsx
47
+ * "use client";
48
+ *
49
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
50
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
51
+ *
52
+ * export default function Home() {
53
+ * const { isLoading, isAuthenticated } = useAuth();
54
+ *
55
+ * if (!isLoading && !isAuthenticated) {
56
+ * return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
57
+ * }
58
+ *
59
+ * return <>You are signed in</>;
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 />;
74
+ * }
75
+ *
76
+ * return <>You are signed in</>;
77
+ * }
78
+ * ```
79
+ *
80
+ * @example Pages Router with options
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
+ * }
94
+ *
95
+ * return <>You are signed in</>;
96
+ * }
97
+ * ```
18
98
  */
19
99
  declare const RedirectToSignIn: ({
20
100
  returnUrl,
@@ -48,10 +128,83 @@ interface ProtectedComponentProps {
48
128
  * A wrapper component that conditionally renders its children based on the user's authentication
49
129
  * status and group membership.
50
130
  *
51
- * @param props - The properties for the Protected component.
131
+ * **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.**
132
+ *
133
+ * @param props - Props for customizing the Protected component.
52
134
  *
53
135
  * @returns The children if authorized, the `onAccessDenied` content if unauthorized,
54
136
  * or `null` while loading.
137
+ *
138
+ * @example App Router
139
+ *
140
+ * ```tsx
141
+ * "use client";
142
+ *
143
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
144
+ *
145
+ * export default function Home() {
146
+ * return (
147
+ * <Protected onAccessDenied={<>Sign in to view the message.</>}>
148
+ * <>You are breathtaking</>
149
+ * </Protected>
150
+ * );
151
+ * }
152
+ * ```
153
+ *
154
+ * @example App Router with group options
155
+ *
156
+ * See {@link ProtectedComponentProps}.
157
+ *
158
+ * ```tsx
159
+ * "use client";
160
+ *
161
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
162
+ *
163
+ * export default function Home() {
164
+ * return (
165
+ * <Protected
166
+ * onAccessDenied={<>Only admins are allowed.</>}
167
+ * groups={["admin"]}
168
+ * >
169
+ * <>Signed in as admin</>
170
+ * </Protected>
171
+ * );
172
+ * }
173
+ * ```
174
+ *
175
+ * @example Pages Router
176
+ *
177
+ * ```tsx
178
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
179
+ *
180
+ * export default function Home() {
181
+ * return (
182
+ * <Protected onAccessDenied={<>Sign in to view the message.</>}>
183
+ * <>You are breathtaking</>
184
+ * </Protected>
185
+ * );
186
+ * }
187
+ * ```
188
+ *
189
+ * @example Pages Router with group options
190
+ *
191
+ * See {@link ProtectedComponentProps}.
192
+ *
193
+ * ```tsx
194
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
195
+ *
196
+ * export default function Home() {
197
+ * return (
198
+ * <Protected
199
+ * onAccessDenied={<>Only admins are allowed.</>}
200
+ * groups={["admin"]}
201
+ * >
202
+ * <>Signed in as admin</>
203
+ * </Protected>
204
+ * );
205
+ * }
206
+ * ```
207
+ *
55
208
  */
56
209
  declare const Protected: ({
57
210
  children,
@@ -1,4 +1,4 @@
1
- import { n as redirectToSignIn, r as useAuth } from "../../client-0gaUvMR7.mjs";
1
+ import { n as redirectToSignIn, r as useAuth } from "../../client-CnvBgZM-.mjs";
2
2
  import { isUserInGroup } from "@monocloud/auth-node-core/utils";
3
3
  import React, { useEffect } from "react";
4
4
 
@@ -6,7 +6,87 @@ import React, { useEffect } from "react";
6
6
  /**
7
7
  * A client side component that will redirect users to the sign in page.
8
8
  *
9
- * @type RedirectToSignInProps
9
+ * **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.**
10
+ *
11
+ * @param props - The props for customizing RedirectToSignIn
12
+ *
13
+ * @returns
14
+ *
15
+ * @example App Router
16
+ *
17
+ * ```tsx
18
+ * "use client";
19
+ *
20
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
21
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
22
+ *
23
+ * export default function Home() {
24
+ * const { isLoading, isAuthenticated } = useAuth();
25
+ *
26
+ * if (!isLoading && !isAuthenticated) {
27
+ * return <RedirectToSignIn />;
28
+ * }
29
+ *
30
+ * return <>You are signed in</>;
31
+ * }
32
+ * ```
33
+ *
34
+ * @example App Router with options
35
+ *
36
+ * You can customize the authorization request by passing in props. See {@link RedirectToSignInProps}.
37
+ *
38
+ * ```tsx
39
+ * "use client";
40
+ *
41
+ * import { useAuth } from "@monocloud/auth-nextjs/client";
42
+ * import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
43
+ *
44
+ * export default function Home() {
45
+ * const { isLoading, isAuthenticated } = useAuth();
46
+ *
47
+ * if (!isLoading && !isAuthenticated) {
48
+ * return <RedirectToSignIn returnUrl="/dashboard" loginHint="username" />;
49
+ * }
50
+ *
51
+ * return <>You are signed in</>;
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 />;
66
+ * }
67
+ *
68
+ * return <>You are signed in</>;
69
+ * }
70
+ * ```
71
+ *
72
+ * @example Pages Router with options
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
+ * }
86
+ *
87
+ * return <>You are signed in</>;
88
+ * }
89
+ * ```
10
90
  */
11
91
  const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
12
92
  useEffect(() => {
@@ -24,10 +104,83 @@ const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
24
104
  * A wrapper component that conditionally renders its children based on the user's authentication
25
105
  * status and group membership.
26
106
  *
27
- * @param props - The properties for the Protected component.
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.**
108
+ *
109
+ * @param props - Props for customizing the Protected component.
28
110
  *
29
111
  * @returns The children if authorized, the `onAccessDenied` content if unauthorized,
30
112
  * or `null` while loading.
113
+ *
114
+ * @example App Router
115
+ *
116
+ * ```tsx
117
+ * "use client";
118
+ *
119
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
120
+ *
121
+ * export default function Home() {
122
+ * return (
123
+ * <Protected onAccessDenied={<>Sign in to view the message.</>}>
124
+ * <>You are breathtaking</>
125
+ * </Protected>
126
+ * );
127
+ * }
128
+ * ```
129
+ *
130
+ * @example App Router with group options
131
+ *
132
+ * See {@link ProtectedComponentProps}.
133
+ *
134
+ * ```tsx
135
+ * "use client";
136
+ *
137
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
138
+ *
139
+ * export default function Home() {
140
+ * return (
141
+ * <Protected
142
+ * onAccessDenied={<>Only admins are allowed.</>}
143
+ * groups={["admin"]}
144
+ * >
145
+ * <>Signed in as admin</>
146
+ * </Protected>
147
+ * );
148
+ * }
149
+ * ```
150
+ *
151
+ * @example Pages Router
152
+ *
153
+ * ```tsx
154
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
155
+ *
156
+ * export default function Home() {
157
+ * return (
158
+ * <Protected onAccessDenied={<>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}.
168
+ *
169
+ * ```tsx
170
+ * import { Protected } from "@monocloud/auth-nextjs/components/client";
171
+ *
172
+ * export default function Home() {
173
+ * return (
174
+ * <Protected
175
+ * onAccessDenied={<>Only admins are allowed.</>}
176
+ * groups={["admin"]}
177
+ * >
178
+ * <>Signed in as admin</>
179
+ * </Protected>
180
+ * );
181
+ * }
182
+ * ```
183
+ *
31
184
  */
32
185
  const Protected = ({ children, groups, groupsClaim, matchAllGroups = false, onAccessDenied = null }) => {
33
186
  const { isLoading, error, isAuthenticated, user } = useAuth();
@@ -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 * 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 * @type RedirectToSignInProps\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, { JSX } from 'react';\nimport { useAuth } from '../../client';\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 onAccessDenied?: 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 * @param props - The properties for the Protected component.\n *\n * @returns The children if authorized, the `onAccessDenied` content if unauthorized,\n * or `null` while loading.\n */\nexport const Protected = ({\n children,\n groups,\n groupsClaim,\n matchAllGroups = false,\n onAccessDenied = null,\n}: ProtectedComponentProps): JSX.Element | null => {\n const { isLoading, error, isAuthenticated, user } = useAuth();\n\n if (isLoading) {\n return null;\n }\n\n if (error || !isAuthenticated || !user) {\n if (onAccessDenied) {\n return <>{onAccessDenied}</>;\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 : onAccessDenied}\n </>\n );\n};\n"],"mappings":";;;;;;;;;;AAqBA,MAAa,oBAAoB,EAC/B,WACA,GAAG,iBAC8B;AACjC,iBAAgB;AACd,mBAAiB;GAAE;GAAW,GAAG;GAAY,CAAC;IAC7C,CAAC,YAAY,UAAU,CAAC;AAC3B,QAAO;;;;;;;;;;;;;;ACYT,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,iBAAiB,WACgC;CACjD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAAS,SAAS;AAE7D,KAAI,UACF,QAAO;AAGT,KAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;AACtC,MAAI,eACF,QAAO,0DAAG,eAAkB;AAG9B,SAAO;;AAGT,QACE,0DACG,CAAC,UACF,cACE,MACA,QACA,eAAe,QAAQ,IAAI,yCAC3B,eACD,GACG,WACA,eACH"}
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 * 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, { JSX } from 'react';\nimport { useAuth } from '../../client';\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 onAccessDenied?: 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 `onAccessDenied` content if 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 onAccessDenied={<>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 * onAccessDenied={<>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 onAccessDenied={<>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 * onAccessDenied={<>Only admins are 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 onAccessDenied = null,\n}: ProtectedComponentProps): JSX.Element | null => {\n const { isLoading, error, isAuthenticated, user } = useAuth();\n\n if (isLoading) {\n return null;\n }\n\n if (error || !isAuthenticated || !user) {\n if (onAccessDenied) {\n return <>{onAccessDenied}</>;\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 : onAccessDenied}\n </>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqGA,MAAa,oBAAoB,EAC/B,WACA,GAAG,iBAC8B;AACjC,iBAAgB;AACd,mBAAiB;GAAE;GAAW,GAAG;GAAY,CAAC;IAC7C,CAAC,YAAY,UAAU,CAAC;AAC3B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACKT,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,iBAAiB,WACgC;CACjD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAAS,SAAS;AAE7D,KAAI,UACF,QAAO;AAGT,KAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;AACtC,MAAI,eACF,QAAO,0DAAG,eAAkB;AAG9B,SAAO;;AAGT,QACE,0DACG,CAAC,UACF,cACE,MACA,QACA,eAAe,QAAQ,IAAI,yCAC3B,eACD,GACG,WACA,eACH"}