@monocloud/auth-nextjs 0.1.1 → 0.1.3
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 +3 -0
- package/dist/client/index.cjs +1 -1
- package/dist/client/index.d.mts +136 -3
- package/dist/client/index.mjs +1 -1
- package/dist/{client-BjnSJS59.cjs → client-Be6A2vEn.cjs} +149 -10
- package/dist/client-Be6A2vEn.cjs.map +1 -0
- package/dist/client-CnvBgZM-.mjs +244 -0
- package/dist/client-CnvBgZM-.mjs.map +1 -0
- package/dist/components/client/index.cjs +156 -3
- package/dist/components/client/index.cjs.map +1 -1
- package/dist/components/client/index.d.mts +156 -3
- package/dist/components/client/index.mjs +156 -3
- package/dist/components/client/index.mjs.map +1 -1
- package/dist/components/index.cjs +84 -1
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.mts +86 -1
- package/dist/components/index.mjs +84 -1
- package/dist/components/index.mjs.map +1 -1
- package/dist/index.cjs +716 -333
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +1890 -56
- package/dist/index.mjs +691 -308
- package/dist/index.mjs.map +1 -1
- package/dist/{types-BleaXQUP.d.mts → types-DOfZTKa6.d.mts} +90 -141
- package/package.json +2 -2
- package/dist/client-0gaUvMR7.mjs +0 -105
- package/dist/client-0gaUvMR7.mjs.map +0 -1
- package/dist/client-BjnSJS59.cjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-CnvBgZM-.mjs","names":[],"sources":["../src/client/use-auth.tsx","../src/client/protect.tsx"],"sourcesContent":["'use client';\n\nimport type { MonoCloudUser } from '@monocloud/auth-node-core';\nimport useSWR from 'swr';\n\n/**\n * Authentication State returned by `useAuth` hook.\n */\nexport interface AuthState {\n /**\n * Flag indicating if the authentication state is still loading.\n */\n isLoading: boolean;\n /**\n * Flag indicating if the user is authenticated.\n */\n isAuthenticated: boolean;\n /**\n * Error encountered during authentication, if any.\n */\n error?: Error;\n /**\n * The authenticated user's information, if available.\n */\n user?: MonoCloudUser;\n /**\n * Function to refetch the authentication state.\n *\n */\n refetch: (refresh?: boolean) => void;\n}\n\nconst fetchUser = async (url: string): Promise<MonoCloudUser | undefined> => {\n const res = await fetch(url, { credentials: 'include' });\n\n if (res.status === 204) {\n return undefined;\n }\n\n if (res.ok) {\n return res.json();\n }\n\n throw new Error('Failed to fetch user');\n};\n\n/**\n *\n * Hook for getting the user's profile on client components\n *\n * @returns Authentication State\n *\n * @example App Router\n *\n * ```tsx\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n *\n * export default function Home() {\n * const { user } = useAuth();\n *\n * return <>User Id: {user?.sub}</>;\n * }\n * ```\n *\n * @example App Router - Refetch user from Userinfo endpoint\n *\n * Calling `refetch(true)` will force refresh the user's profile from the userinfo endpoint.\n * If you do not intent to refersh from your tenants userinfo endpoint, use just `refetch()`\n *\n * **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**\n *\n * ```tsx\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n *\n * export default function Home() {\n * const { user, refetch } = useAuth();\n *\n * return (\n * <>\n * <pre>{JSON.stringify(user)}</pre>\n * <button onClick={() => refetch(true)}>Refresh</button>\n * </>\n * );\n * }\n * ```\n *\n * @example Pages Router\n *\n * ```tsx\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n *\n * export default function Home() {\n * const { user } = useAuth();\n *\n * return <>User Id: {user?.sub}</>;\n * }\n * ```\n *\n * @example Pages Router - Refetch user from Userinfo endpoint\n *\n * Calling `refetch(true)` will force refresh the user's profile from the userinfo endpoint.\n * If you do not intent to refersh from your tenants userinfo endpoint, use just `refetch()`\n *\n * **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**\n *\n * ```tsx\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n *\n * export default function Home() {\n * const { user, refetch } = useAuth();\n *\n * return (\n * <>\n * <pre>{JSON.stringify(user)}</pre>\n * <button onClick={() => refetch(true)}>Refresh</button>\n * </>\n * );\n * }\n * ```\n *\n */\nexport const useAuth = (): AuthState => {\n const key =\n process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_USER_INFO_URL ??\n // eslint-disable-next-line no-underscore-dangle\n `${process.env.__NEXT_ROUTER_BASEPATH ?? ''}/api/auth/userinfo`;\n\n const { data, error, isLoading, mutate } = useSWR<MonoCloudUser | undefined>(\n key,\n fetchUser\n );\n\n const refetch = (refresh?: boolean): void => {\n const url = new URL(key, 'https://dummy');\n if (refresh) {\n url.searchParams.set('refresh', 'true');\n }\n\n void mutate(async () => await fetchUser(url.pathname + url.search), {\n revalidate: false,\n });\n };\n\n if (error) {\n return {\n user: undefined,\n isLoading: false,\n isAuthenticated: false,\n error: error as Error,\n refetch,\n };\n }\n\n if (data) {\n return {\n user: data,\n isLoading,\n isAuthenticated: !!data && Object.keys(data).length > 0,\n error: undefined,\n refetch,\n };\n }\n\n return {\n user: undefined,\n isLoading,\n isAuthenticated: false,\n error: undefined,\n /* v8 ignore next -- @preserve */\n refetch: (): void => {},\n };\n};\n","/* eslint-disable react/display-name */\n'use client';\n\nimport React, { ComponentType, JSX, useEffect } from 'react';\nimport type { MonoCloudUser } from '@monocloud/auth-node-core';\nimport { isUserInGroup } from '@monocloud/auth-node-core/utils';\nimport { useAuth } from './use-auth';\nimport { ExtraAuthParams, GroupOptions } from '../types';\n\n/**\n * Options for configuring page protection.\n */\nexport type ProtectPageOptions = {\n /**\n *The url where the user will be redirected to after sign in\n */\n returnUrl?: string;\n\n /**\n * A custom react element to render when the user is not authenticated or is not a member of the specified groups.\n */\n onAccessDenied?: (user?: MonoCloudUser) => JSX.Element;\n\n /**\n * Authorization parameters to be used during authentication.\n */\n authParams?: ExtraAuthParams;\n\n /**\n * Callback function to handle errors.\n * If not provided, errors will be thrown.\n *\n * @param error - The error object.\n * @returns JSX element to handle the error.\n */\n onError?: (error: Error) => JSX.Element;\n} & GroupOptions;\n\nexport const redirectToSignIn = (\n options: { returnUrl?: string } & ExtraAuthParams\n): void => {\n const searchParams = new URLSearchParams(window.location.search);\n searchParams.set(\n 'return_url',\n options.returnUrl ?? window.location.toString()\n );\n\n if (options?.scopes) {\n searchParams.set('scope', options.scopes);\n }\n if (options?.resource) {\n searchParams.set('resource', options.resource);\n }\n\n if (options?.acrValues) {\n searchParams.set('acr_values', options.acrValues.join(' '));\n }\n\n if (options?.display) {\n searchParams.set('display', options.display);\n }\n\n if (options?.prompt) {\n searchParams.set('prompt', options.prompt);\n }\n\n if (options?.authenticatorHint) {\n searchParams.set('authenticator_hint', options.authenticatorHint);\n }\n\n if (options?.uiLocales) {\n searchParams.set('ui_locales', options.uiLocales);\n }\n\n if (options?.maxAge) {\n searchParams.set('max_age', options.maxAge.toString());\n }\n\n if (options?.loginHint) {\n searchParams.set('login_hint', options.loginHint);\n }\n\n window.location.assign(\n // eslint-disable-next-line no-underscore-dangle\n `${process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_SIGNIN_URL ?? `${process.env.__NEXT_ROUTER_BASEPATH ?? ''}/api/auth/signin`}?${searchParams.toString()}`\n );\n};\n\nconst handlePageError = (\n error: Error,\n options?: ProtectPageOptions\n): JSX.Element => {\n /* v8 ignore else -- @preserve */\n if (options?.onError) {\n return options.onError(error);\n }\n\n /* v8 ignore next -- @preserve */\n throw error;\n};\n\n/**\n * Function to protect a client rendered page component.\n * Ensures that only authenticated users can access the component.\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 Component - The component to protect.\n * @param options - The options.\n *\n * @returns Protected client rendered page component.\n *\n * @example App Router\n *\n * ```tsx\n * \"use client\";\n *\n * import { protectPage } from \"@monocloud/auth-nextjs/client\";\n *\n * export default protectPage(function Home() {\n * return <>You are signed in</>;\n * });\n * ```\n *\n * @example App Router with options\n * \n * See {@link ProtectPageOptions} for more options.\n *\n * ```tsx\n * \"use client\";\n *\n * import { protectPage } from \"@monocloud/auth-nextjs/client\";\n *\n * export default protectPage(\n * function Home() {\n * return <>You are signed in</>;\n * },\n * { returnUrl: \"/dashboard\", authParams: { loginHint: \"username\" } }\n * );\n * ```\n\n* @example Pages Router\n *\n * ```tsx\n * import { protectPage } from \"@monocloud/auth-nextjs/client\";\n *\n * export default protectPage(function Home() {\n * return <>You are signed in</>;\n * });\n * ```\n *\n * @example Pages Router with options\n * \n * See {@link ProtectPageOptions} for more options.\n *\n * ```tsx\n * import { protectPage } from \"@monocloud/auth-nextjs/client\";\n *\n * export default protectPage(\n * function Home() {\n * return <>You are signed in</>;\n * },\n * { returnUrl: \"/dashboard\", authParams: { loginHint: \"username\" } }\n * );\n * ```\n */\nexport const protectPage = <P extends object>(\n Component: ComponentType<P & { user: MonoCloudUser }>,\n options?: ProtectPageOptions\n): React.FC<P> => {\n return props => {\n const { user, error, isLoading } = useAuth();\n\n useEffect(() => {\n if (!user && !isLoading && !error) {\n if (options?.onAccessDenied) {\n return;\n }\n\n const authParams = options?.authParams ?? {};\n redirectToSignIn({\n returnUrl: options?.returnUrl,\n ...authParams,\n });\n }\n }, [user, isLoading, error]);\n\n if (error) {\n return handlePageError(error, options);\n }\n\n if (!user && !isLoading && options?.onAccessDenied) {\n return options.onAccessDenied();\n }\n\n if (user) {\n if (\n options?.groups &&\n !isUserInGroup(\n user,\n options.groups,\n options.groupsClaim ??\n process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_GROUPS_CLAIM,\n options.matchAll\n )\n ) {\n const { onAccessDenied = (): JSX.Element => <div>Access Denied</div> } =\n options;\n return onAccessDenied(user);\n }\n\n return <Component user={user} {...props} />;\n }\n\n return null;\n };\n};\n"],"mappings":"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"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_chunk = require('../../chunk-CbDLau6x.cjs');
|
|
2
|
-
const require_client = require('../../client-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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 * @
|
|
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-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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 * @
|
|
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"}
|