@monocloud/auth-nextjs 0.1.9 → 0.1.11
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 +1 -1
- package/dist/{chunk-C0xms8kb.cjs → _virtual/_rolldown/runtime.cjs} +1 -6
- package/dist/client/index.cjs +3 -2
- package/dist/client/index.d.mts +3 -203
- package/dist/client/index.mjs +2 -1
- package/dist/{protect-client-page-BdsnH8gs.cjs → client/protect-client-page.cjs} +9 -112
- package/dist/client/protect-client-page.cjs.map +1 -0
- package/dist/client/protect-client-page.d.mts +123 -0
- package/dist/{protect-client-page-BFVskb3X.mjs → client/protect-client-page.mjs} +5 -91
- package/dist/client/protect-client-page.mjs.map +1 -0
- package/dist/client/use-auth.cjs +96 -0
- package/dist/client/use-auth.cjs.map +1 -0
- package/dist/client/use-auth.d.mts +84 -0
- package/dist/client/use-auth.mjs +94 -0
- package/dist/client/use-auth.mjs.map +1 -0
- package/dist/components/client/index.cjs +4 -156
- package/dist/components/client/index.d.mts +3 -189
- package/dist/components/client/index.mjs +3 -153
- package/dist/components/client/protected.cjs +89 -0
- package/dist/components/client/protected.cjs.map +1 -0
- package/dist/components/client/protected.d.mts +114 -0
- package/dist/components/client/protected.mjs +87 -0
- package/dist/components/client/protected.mjs.map +1 -0
- package/dist/components/client/redirect-to-signin.cjs +77 -0
- package/dist/components/client/redirect-to-signin.cjs.map +1 -0
- package/dist/components/client/redirect-to-signin.d.mts +78 -0
- package/dist/components/client/redirect-to-signin.mjs +76 -0
- package/dist/components/client/redirect-to-signin.mjs.map +1 -0
- package/dist/components/index.cjs +6 -168
- package/dist/components/index.d.mts +4 -188
- package/dist/components/index.mjs +4 -164
- package/dist/components/signin.cjs +63 -0
- package/dist/components/signin.cjs.map +1 -0
- package/dist/components/signin.d.mts +72 -0
- package/dist/components/signin.mjs +61 -0
- package/dist/components/signin.mjs.map +1 -0
- package/dist/components/signout.cjs +49 -0
- package/dist/components/signout.cjs.map +1 -0
- package/dist/components/signout.d.mts +55 -0
- package/dist/components/signout.mjs +47 -0
- package/dist/components/signout.mjs.map +1 -0
- package/dist/components/signup.cjs +67 -0
- package/dist/components/signup.cjs.map +1 -0
- package/dist/components/signup.d.mts +70 -0
- package/dist/components/signup.mjs +65 -0
- package/dist/components/signup.mjs.map +1 -0
- package/dist/index.cjs +15 -1136
- package/dist/index.d.mts +5 -1681
- package/dist/index.mjs +4 -1125
- package/dist/initialize.cjs +284 -0
- package/dist/initialize.cjs.map +1 -0
- package/dist/initialize.d.mts +1383 -0
- package/dist/initialize.mjs +274 -0
- package/dist/initialize.mjs.map +1 -0
- package/dist/monocloud-next-client.cjs +600 -0
- package/dist/monocloud-next-client.cjs.map +1 -0
- package/dist/monocloud-next-client.d.mts +330 -0
- package/dist/monocloud-next-client.mjs +599 -0
- package/dist/monocloud-next-client.mjs.map +1 -0
- package/dist/requests/monocloud-app-router-request.cjs +32 -0
- package/dist/requests/monocloud-app-router-request.cjs.map +1 -0
- package/dist/requests/monocloud-app-router-request.mjs +31 -0
- package/dist/requests/monocloud-app-router-request.mjs.map +1 -0
- package/dist/requests/monocloud-cookie-request.cjs +22 -0
- package/dist/requests/monocloud-cookie-request.cjs.map +1 -0
- package/dist/requests/monocloud-cookie-request.mjs +21 -0
- package/dist/requests/monocloud-cookie-request.mjs.map +1 -0
- package/dist/requests/monocloud-page-router-request.cjs +37 -0
- package/dist/requests/monocloud-page-router-request.cjs.map +1 -0
- package/dist/requests/monocloud-page-router-request.mjs +36 -0
- package/dist/requests/monocloud-page-router-request.mjs.map +1 -0
- package/dist/responses/monocloud-app-router-response.cjs +67 -0
- package/dist/responses/monocloud-app-router-response.cjs.map +1 -0
- package/dist/responses/monocloud-app-router-response.mjs +66 -0
- package/dist/responses/monocloud-app-router-response.mjs.map +1 -0
- package/dist/responses/monocloud-cookie-response.cjs +20 -0
- package/dist/responses/monocloud-cookie-response.cjs.map +1 -0
- package/dist/responses/monocloud-cookie-response.mjs +19 -0
- package/dist/responses/monocloud-cookie-response.mjs.map +1 -0
- package/dist/responses/monocloud-page-router-response.cjs +54 -0
- package/dist/responses/monocloud-page-router-response.cjs.map +1 -0
- package/dist/responses/monocloud-page-router-response.mjs +53 -0
- package/dist/responses/monocloud-page-router-response.mjs.map +1 -0
- package/dist/{types-ClljFIvK.d.mts → types.d.mts} +2 -2
- package/dist/utils.cjs +89 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.mjs +80 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/components/client/index.cjs.map +0 -1
- package/dist/components/client/index.mjs.map +0 -1
- package/dist/components/index.cjs.map +0 -1
- package/dist/components/index.mjs.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/protect-client-page-BFVskb3X.mjs.map +0 -1
- package/dist/protect-client-page-BdsnH8gs.cjs.map +0 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
4
|
+
let swr = require("swr");
|
|
5
|
+
swr = require_runtime.__toESM(swr);
|
|
6
|
+
|
|
7
|
+
//#region src/client/use-auth.tsx
|
|
8
|
+
const fetchUser = async (url) => {
|
|
9
|
+
const res = await fetch(url, { credentials: "include" });
|
|
10
|
+
if (res.status === 204) return;
|
|
11
|
+
if (res.ok) return res.json();
|
|
12
|
+
throw new Error("Failed to fetch user");
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* `useAuth()` is a client-side hook that provides access to the current authentication state.
|
|
17
|
+
*
|
|
18
|
+
* It can only be used inside **Client Components**.
|
|
19
|
+
*
|
|
20
|
+
* @example Basic Usage
|
|
21
|
+
* ```tsx title="Basic Usage"
|
|
22
|
+
* "use client";
|
|
23
|
+
*
|
|
24
|
+
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
25
|
+
*
|
|
26
|
+
* export default function Home() {
|
|
27
|
+
* const { user, isAuthenticated } = useAuth();
|
|
28
|
+
*
|
|
29
|
+
* if (!isAuthenticated) {
|
|
30
|
+
* return <>Not signed in</>;
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* return <>User Id: {user?.sub}</>;
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example Refetch user
|
|
38
|
+
*
|
|
39
|
+
* Calling `refetch(true)` forces a refresh of the user profile from the `UserInfo` endpoint.
|
|
40
|
+
* Calling `refetch()` refreshes authentication state without forcing a `UserInfo` request.
|
|
41
|
+
*
|
|
42
|
+
* ```tsx title="Refetch User"
|
|
43
|
+
* "use client";
|
|
44
|
+
*
|
|
45
|
+
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
46
|
+
*
|
|
47
|
+
* export default function Home() {
|
|
48
|
+
* const { user, refetch } = useAuth();
|
|
49
|
+
*
|
|
50
|
+
* return (
|
|
51
|
+
* <>
|
|
52
|
+
* <pre>{JSON.stringify(user, null, 2)}</pre>
|
|
53
|
+
* <button onClick={() => refetch(true)}>Refresh Profile</button>
|
|
54
|
+
* </>
|
|
55
|
+
* );
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @returns
|
|
60
|
+
*
|
|
61
|
+
* @category Hooks
|
|
62
|
+
*/
|
|
63
|
+
const useAuth = () => {
|
|
64
|
+
const key = process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_USER_INFO_URL ?? `${process.env.__NEXT_ROUTER_BASEPATH ?? ""}/api/auth/userinfo`;
|
|
65
|
+
const { data, error, isLoading, mutate } = (0, swr.default)(key, fetchUser);
|
|
66
|
+
const refetch = (refresh) => {
|
|
67
|
+
const url = new URL(key, "https://dummy");
|
|
68
|
+
if (refresh) url.searchParams.set("refresh", "true");
|
|
69
|
+
mutate(async () => await fetchUser(url.pathname + url.search), { revalidate: false });
|
|
70
|
+
};
|
|
71
|
+
if (error) return {
|
|
72
|
+
user: void 0,
|
|
73
|
+
isLoading: false,
|
|
74
|
+
isAuthenticated: false,
|
|
75
|
+
error,
|
|
76
|
+
refetch
|
|
77
|
+
};
|
|
78
|
+
if (data) return {
|
|
79
|
+
user: data,
|
|
80
|
+
isLoading,
|
|
81
|
+
isAuthenticated: !!data && Object.keys(data).length > 0,
|
|
82
|
+
error: void 0,
|
|
83
|
+
refetch
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
user: void 0,
|
|
87
|
+
isLoading,
|
|
88
|
+
isAuthenticated: false,
|
|
89
|
+
error: void 0,
|
|
90
|
+
refetch: () => {}
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
exports.useAuth = useAuth;
|
|
96
|
+
//# sourceMappingURL=use-auth.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-auth.cjs","names":[],"sources":["../../src/client/use-auth.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 *\n * @category Types\n */\nexport interface AuthenticationState {\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 * `useAuth()` is a client-side hook that provides access to the current authentication state.\n *\n * It can only be used inside **Client Components**.\n *\n * @example Basic Usage\n * ```tsx title=\"Basic Usage\"\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n *\n * export default function Home() {\n * const { user, isAuthenticated } = useAuth();\n *\n * if (!isAuthenticated) {\n * return <>Not signed in</>;\n * }\n *\n * return <>User Id: {user?.sub}</>;\n * }\n * ```\n *\n * @example Refetch user\n *\n * Calling `refetch(true)` forces a refresh of the user profile from the `UserInfo` endpoint.\n * Calling `refetch()` refreshes authentication state without forcing a `UserInfo` request.\n *\n * ```tsx title=\"Refetch User\"\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, null, 2)}</pre>\n * <button onClick={() => refetch(true)}>Refresh Profile</button>\n * </>\n * );\n * }\n * ```\n *\n * @returns\n *\n * @category Hooks\n */\nexport const useAuth = (): AuthenticationState => {\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"],"mappings":";;;;;;;AAkCA,MAAM,YAAY,OAAO,QAAoD;CAC3E,MAAM,MAAM,MAAM,MAAM,KAAK,EAAE,aAAa,WAAW,CAAC;AAExD,KAAI,IAAI,WAAW,IACjB;AAGF,KAAI,IAAI,GACN,QAAO,IAAI,MAAM;AAGnB,OAAM,IAAI,MAAM,uBAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDzC,MAAa,gBAAqC;CAChD,MAAM,MACJ,QAAQ,IAAI,4CAEZ,GAAG,QAAQ,IAAI,0BAA0B,GAAG;CAE9C,MAAM,EAAE,MAAM,OAAO,WAAW,4BAC9B,KACA,UACD;CAED,MAAM,WAAW,YAA4B;EAC3C,MAAM,MAAM,IAAI,IAAI,KAAK,gBAAgB;AACzC,MAAI,QACF,KAAI,aAAa,IAAI,WAAW,OAAO;AAGzC,EAAK,OAAO,YAAY,MAAM,UAAU,IAAI,WAAW,IAAI,OAAO,EAAE,EAClE,YAAY,OACb,CAAC;;AAGJ,KAAI,MACF,QAAO;EACL,MAAM;EACN,WAAW;EACX,iBAAiB;EACV;EACP;EACD;AAGH,KAAI,KACF,QAAO;EACL,MAAM;EACN;EACA,iBAAiB,CAAC,CAAC,QAAQ,OAAO,KAAK,KAAK,CAAC,SAAS;EACtD,OAAO;EACP;EACD;AAGH,QAAO;EACL,MAAM;EACN;EACA,iBAAiB;EACjB,OAAO;EAEP,eAAqB;EACtB"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { MonoCloudUser } from "@monocloud/auth-node-core";
|
|
2
|
+
|
|
3
|
+
//#region src/client/use-auth.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Authentication State returned by `useAuth` hook.
|
|
6
|
+
*
|
|
7
|
+
* @category Types
|
|
8
|
+
*/
|
|
9
|
+
interface AuthenticationState {
|
|
10
|
+
/**
|
|
11
|
+
* Flag indicating if the authentication state is still loading.
|
|
12
|
+
*/
|
|
13
|
+
isLoading: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Flag indicating if the user is authenticated.
|
|
16
|
+
*/
|
|
17
|
+
isAuthenticated: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Error encountered during authentication, if any.
|
|
20
|
+
*/
|
|
21
|
+
error?: Error;
|
|
22
|
+
/**
|
|
23
|
+
* The authenticated user's information, if available.
|
|
24
|
+
*/
|
|
25
|
+
user?: MonoCloudUser;
|
|
26
|
+
/**
|
|
27
|
+
* Function to refetch the authentication state.
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
refetch: (refresh?: boolean) => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* `useAuth()` is a client-side hook that provides access to the current authentication state.
|
|
35
|
+
*
|
|
36
|
+
* It can only be used inside **Client Components**.
|
|
37
|
+
*
|
|
38
|
+
* @example Basic Usage
|
|
39
|
+
* ```tsx title="Basic Usage"
|
|
40
|
+
* "use client";
|
|
41
|
+
*
|
|
42
|
+
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
43
|
+
*
|
|
44
|
+
* export default function Home() {
|
|
45
|
+
* const { user, isAuthenticated } = useAuth();
|
|
46
|
+
*
|
|
47
|
+
* if (!isAuthenticated) {
|
|
48
|
+
* return <>Not signed in</>;
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* return <>User Id: {user?.sub}</>;
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example Refetch user
|
|
56
|
+
*
|
|
57
|
+
* Calling `refetch(true)` forces a refresh of the user profile from the `UserInfo` endpoint.
|
|
58
|
+
* Calling `refetch()` refreshes authentication state without forcing a `UserInfo` request.
|
|
59
|
+
*
|
|
60
|
+
* ```tsx title="Refetch User"
|
|
61
|
+
* "use client";
|
|
62
|
+
*
|
|
63
|
+
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
64
|
+
*
|
|
65
|
+
* export default function Home() {
|
|
66
|
+
* const { user, refetch } = useAuth();
|
|
67
|
+
*
|
|
68
|
+
* return (
|
|
69
|
+
* <>
|
|
70
|
+
* <pre>{JSON.stringify(user, null, 2)}</pre>
|
|
71
|
+
* <button onClick={() => refetch(true)}>Refresh Profile</button>
|
|
72
|
+
* </>
|
|
73
|
+
* );
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @returns
|
|
78
|
+
*
|
|
79
|
+
* @category Hooks
|
|
80
|
+
*/
|
|
81
|
+
declare const useAuth: () => AuthenticationState;
|
|
82
|
+
//#endregion
|
|
83
|
+
export { AuthenticationState, useAuth };
|
|
84
|
+
//# sourceMappingURL=use-auth.d.mts.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import useSWR from "swr";
|
|
4
|
+
|
|
5
|
+
//#region src/client/use-auth.tsx
|
|
6
|
+
const fetchUser = async (url) => {
|
|
7
|
+
const res = await fetch(url, { credentials: "include" });
|
|
8
|
+
if (res.status === 204) return;
|
|
9
|
+
if (res.ok) return res.json();
|
|
10
|
+
throw new Error("Failed to fetch user");
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* `useAuth()` is a client-side hook that provides access to the current authentication state.
|
|
15
|
+
*
|
|
16
|
+
* It can only be used inside **Client Components**.
|
|
17
|
+
*
|
|
18
|
+
* @example Basic Usage
|
|
19
|
+
* ```tsx title="Basic Usage"
|
|
20
|
+
* "use client";
|
|
21
|
+
*
|
|
22
|
+
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
23
|
+
*
|
|
24
|
+
* export default function Home() {
|
|
25
|
+
* const { user, isAuthenticated } = useAuth();
|
|
26
|
+
*
|
|
27
|
+
* if (!isAuthenticated) {
|
|
28
|
+
* return <>Not signed in</>;
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* return <>User Id: {user?.sub}</>;
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example Refetch user
|
|
36
|
+
*
|
|
37
|
+
* Calling `refetch(true)` forces a refresh of the user profile from the `UserInfo` endpoint.
|
|
38
|
+
* Calling `refetch()` refreshes authentication state without forcing a `UserInfo` request.
|
|
39
|
+
*
|
|
40
|
+
* ```tsx title="Refetch User"
|
|
41
|
+
* "use client";
|
|
42
|
+
*
|
|
43
|
+
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
44
|
+
*
|
|
45
|
+
* export default function Home() {
|
|
46
|
+
* const { user, refetch } = useAuth();
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <>
|
|
50
|
+
* <pre>{JSON.stringify(user, null, 2)}</pre>
|
|
51
|
+
* <button onClick={() => refetch(true)}>Refresh Profile</button>
|
|
52
|
+
* </>
|
|
53
|
+
* );
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @returns
|
|
58
|
+
*
|
|
59
|
+
* @category Hooks
|
|
60
|
+
*/
|
|
61
|
+
const useAuth = () => {
|
|
62
|
+
const key = process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_USER_INFO_URL ?? `${process.env.__NEXT_ROUTER_BASEPATH ?? ""}/api/auth/userinfo`;
|
|
63
|
+
const { data, error, isLoading, mutate } = useSWR(key, fetchUser);
|
|
64
|
+
const refetch = (refresh) => {
|
|
65
|
+
const url = new URL(key, "https://dummy");
|
|
66
|
+
if (refresh) url.searchParams.set("refresh", "true");
|
|
67
|
+
mutate(async () => await fetchUser(url.pathname + url.search), { revalidate: false });
|
|
68
|
+
};
|
|
69
|
+
if (error) return {
|
|
70
|
+
user: void 0,
|
|
71
|
+
isLoading: false,
|
|
72
|
+
isAuthenticated: false,
|
|
73
|
+
error,
|
|
74
|
+
refetch
|
|
75
|
+
};
|
|
76
|
+
if (data) return {
|
|
77
|
+
user: data,
|
|
78
|
+
isLoading,
|
|
79
|
+
isAuthenticated: !!data && Object.keys(data).length > 0,
|
|
80
|
+
error: void 0,
|
|
81
|
+
refetch
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
user: void 0,
|
|
85
|
+
isLoading,
|
|
86
|
+
isAuthenticated: false,
|
|
87
|
+
error: void 0,
|
|
88
|
+
refetch: () => {}
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { useAuth };
|
|
94
|
+
//# sourceMappingURL=use-auth.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-auth.mjs","names":[],"sources":["../../src/client/use-auth.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 *\n * @category Types\n */\nexport interface AuthenticationState {\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 * `useAuth()` is a client-side hook that provides access to the current authentication state.\n *\n * It can only be used inside **Client Components**.\n *\n * @example Basic Usage\n * ```tsx title=\"Basic Usage\"\n * \"use client\";\n *\n * import { useAuth } from \"@monocloud/auth-nextjs/client\";\n *\n * export default function Home() {\n * const { user, isAuthenticated } = useAuth();\n *\n * if (!isAuthenticated) {\n * return <>Not signed in</>;\n * }\n *\n * return <>User Id: {user?.sub}</>;\n * }\n * ```\n *\n * @example Refetch user\n *\n * Calling `refetch(true)` forces a refresh of the user profile from the `UserInfo` endpoint.\n * Calling `refetch()` refreshes authentication state without forcing a `UserInfo` request.\n *\n * ```tsx title=\"Refetch User\"\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, null, 2)}</pre>\n * <button onClick={() => refetch(true)}>Refresh Profile</button>\n * </>\n * );\n * }\n * ```\n *\n * @returns\n *\n * @category Hooks\n */\nexport const useAuth = (): AuthenticationState => {\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"],"mappings":";;;;;AAkCA,MAAM,YAAY,OAAO,QAAoD;CAC3E,MAAM,MAAM,MAAM,MAAM,KAAK,EAAE,aAAa,WAAW,CAAC;AAExD,KAAI,IAAI,WAAW,IACjB;AAGF,KAAI,IAAI,GACN,QAAO,IAAI,MAAM;AAGnB,OAAM,IAAI,MAAM,uBAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDzC,MAAa,gBAAqC;CAChD,MAAM,MACJ,QAAQ,IAAI,4CAEZ,GAAG,QAAQ,IAAI,0BAA0B,GAAG;CAE9C,MAAM,EAAE,MAAM,OAAO,WAAW,WAAW,OACzC,KACA,UACD;CAED,MAAM,WAAW,YAA4B;EAC3C,MAAM,MAAM,IAAI,IAAI,KAAK,gBAAgB;AACzC,MAAI,QACF,KAAI,aAAa,IAAI,WAAW,OAAO;AAGzC,EAAK,OAAO,YAAY,MAAM,UAAU,IAAI,WAAW,IAAI,OAAO,EAAE,EAClE,YAAY,OACb,CAAC;;AAGJ,KAAI,MACF,QAAO;EACL,MAAM;EACN,WAAW;EACX,iBAAiB;EACV;EACP;EACD;AAGH,KAAI,KACF,QAAO;EACL,MAAM;EACN;EACA,iBAAiB,CAAC,CAAC,QAAQ,OAAO,KAAK,KAAK,CAAC,SAAS;EACtD,OAAO;EACP;EACD;AAGH,QAAO;EACL,MAAM;EACN;EACA,iBAAiB;EACjB,OAAO;EAEP,eAAqB;EACtB"}
|
|
@@ -1,158 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
require('../../client/index.cjs');
|
|
5
|
-
let _monocloud_auth_node_core_utils = require("@monocloud/auth-node-core/utils");
|
|
6
|
-
let react = require("react");
|
|
7
|
-
react = require_chunk.__toESM(react);
|
|
2
|
+
const require_redirect_to_signin = require('./redirect-to-signin.cjs');
|
|
3
|
+
const require_protected = require('./protected.cjs');
|
|
8
4
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* `<RedirectToSignIn>` is a **client-side component** that immediately redirects the user to the MonoCloud sign-in page when it is rendered.
|
|
12
|
-
*
|
|
13
|
-
* It does not render any UI.
|
|
14
|
-
*
|
|
15
|
-
* > This component must be used inside a Client Component (`"use client"`).
|
|
16
|
-
*
|
|
17
|
-
* @example Basic Usage
|
|
18
|
-
*
|
|
19
|
-
* ```tsx title="Basic Usage"
|
|
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 With Options
|
|
37
|
-
*
|
|
38
|
-
* You can customize the authorization request by passing in props.
|
|
39
|
-
*
|
|
40
|
-
* ```tsx title="With options"
|
|
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 (
|
|
51
|
-
* <RedirectToSignIn
|
|
52
|
-
* returnUrl="/dashboard"
|
|
53
|
-
* loginHint="user@example.com"
|
|
54
|
-
* />
|
|
55
|
-
* );
|
|
56
|
-
* }
|
|
57
|
-
*
|
|
58
|
-
* return <>You are signed in</>;
|
|
59
|
-
* }
|
|
60
|
-
* ```
|
|
61
|
-
*
|
|
62
|
-
* @param props - The props for customizing RedirectToSignIn.
|
|
63
|
-
* @returns
|
|
64
|
-
*
|
|
65
|
-
* @category Components
|
|
66
|
-
*/
|
|
67
|
-
const RedirectToSignIn = ({ returnUrl, ...authParams }) => {
|
|
68
|
-
(0, react.useEffect)(() => {
|
|
69
|
-
require_protect_client_page.redirectToSignIn({
|
|
70
|
-
returnUrl,
|
|
71
|
-
...authParams
|
|
72
|
-
});
|
|
73
|
-
}, [authParams, returnUrl]);
|
|
74
|
-
return null;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/components/client/protected.tsx
|
|
79
|
-
/**
|
|
80
|
-
* `<Protected>` conditionally renders its children based on the user’s authentication state and (optionally) group membership.
|
|
81
|
-
*
|
|
82
|
-
* > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser.
|
|
83
|
-
* > To enforce access before content is rendered or sent to the client, use server-side protection such as {@link MonoCloudNextClient.protectPage | protectPage()}, or {@link MonoCloudNextClient.protect | protect()}.
|
|
84
|
-
*
|
|
85
|
-
* @example Basic Usage
|
|
86
|
-
*
|
|
87
|
-
* ```tsx title="Basic Usage"
|
|
88
|
-
* "use client";
|
|
89
|
-
*
|
|
90
|
-
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
91
|
-
*
|
|
92
|
-
* export default function Home() {
|
|
93
|
-
* return (
|
|
94
|
-
* <Protected fallback={<>Sign in to view the message.</>}>
|
|
95
|
-
* <>This is the protected content.</>
|
|
96
|
-
* </Protected>
|
|
97
|
-
* );
|
|
98
|
-
* }
|
|
99
|
-
* ```
|
|
100
|
-
*
|
|
101
|
-
* @example With Groups
|
|
102
|
-
*
|
|
103
|
-
* ```tsx title="With Groups"
|
|
104
|
-
* "use client";
|
|
105
|
-
*
|
|
106
|
-
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
107
|
-
*
|
|
108
|
-
* export default function Home() {
|
|
109
|
-
* return (
|
|
110
|
-
* <Protected
|
|
111
|
-
* groups={["admin"]}
|
|
112
|
-
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}
|
|
113
|
-
* >
|
|
114
|
-
* <>Signed in as admin</>
|
|
115
|
-
* </Protected>
|
|
116
|
-
* );
|
|
117
|
-
* }
|
|
118
|
-
* ```
|
|
119
|
-
*
|
|
120
|
-
* @example Requiring all groups
|
|
121
|
-
*
|
|
122
|
-
* ```tsx title="Requiring all groups"
|
|
123
|
-
* "use client";
|
|
124
|
-
*
|
|
125
|
-
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
126
|
-
*
|
|
127
|
-
* export default function Home() {
|
|
128
|
-
* return (
|
|
129
|
-
* <Protected
|
|
130
|
-
* groups={["admin", "billing"]}
|
|
131
|
-
* matchAllGroups
|
|
132
|
-
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}
|
|
133
|
-
* >
|
|
134
|
-
* <>Sensitive settings</>
|
|
135
|
-
* </Protected>
|
|
136
|
-
* );
|
|
137
|
-
* }
|
|
138
|
-
* ```
|
|
139
|
-
*
|
|
140
|
-
* @param props - Props for customizing the Protected component.
|
|
141
|
-
* @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.
|
|
142
|
-
*
|
|
143
|
-
* @category Components
|
|
144
|
-
*/
|
|
145
|
-
const Protected = ({ children, groups, groupsClaim, matchAllGroups = false, fallback = null, onGroupAccessDenied = () => /* @__PURE__ */ react.default.createElement(react.default.Fragment, null) }) => {
|
|
146
|
-
const { isLoading, error, isAuthenticated, user } = require_protect_client_page.useAuth();
|
|
147
|
-
if (isLoading) return null;
|
|
148
|
-
if (error || !isAuthenticated || !user) {
|
|
149
|
-
if (fallback) return fallback;
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, !groups || (0, _monocloud_auth_node_core_utils.isUserInGroup)(user, groups, groupsClaim ?? process.env.NEXT_PUBLIC_MONOCLOUD_AUTH_GROUPS_CLAIM, matchAllGroups) ? children : onGroupAccessDenied(user));
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
//#endregion
|
|
156
|
-
exports.Protected = Protected;
|
|
157
|
-
exports.RedirectToSignIn = RedirectToSignIn;
|
|
158
|
-
//# sourceMappingURL=index.cjs.map
|
|
5
|
+
exports.Protected = require_protected.Protected;
|
|
6
|
+
exports.RedirectToSignIn = require_redirect_to_signin.RedirectToSignIn;
|
|
@@ -1,189 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//#region src/components/client/redirect-to-signin.d.ts
|
|
6
|
-
/**
|
|
7
|
-
* Props for the `<RedirectToSignIn />` Component
|
|
8
|
-
*
|
|
9
|
-
* @category Types
|
|
10
|
-
*/
|
|
11
|
-
interface RedirectToSignInProps extends ExtraAuthParams {
|
|
12
|
-
/**
|
|
13
|
-
* The URL to return to after successful authentication. If not provided, the current URL is used.
|
|
14
|
-
*/
|
|
15
|
-
returnUrl?: string;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* `<RedirectToSignIn>` is a **client-side component** that immediately redirects the user to the MonoCloud sign-in page when it is rendered.
|
|
19
|
-
*
|
|
20
|
-
* It does not render any UI.
|
|
21
|
-
*
|
|
22
|
-
* > This component must be used inside a Client Component (`"use client"`).
|
|
23
|
-
*
|
|
24
|
-
* @example Basic Usage
|
|
25
|
-
*
|
|
26
|
-
* ```tsx title="Basic Usage"
|
|
27
|
-
* "use client";
|
|
28
|
-
*
|
|
29
|
-
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
30
|
-
* import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
|
|
31
|
-
*
|
|
32
|
-
* export default function Home() {
|
|
33
|
-
* const { isLoading, isAuthenticated } = useAuth();
|
|
34
|
-
*
|
|
35
|
-
* if (!isLoading && !isAuthenticated) {
|
|
36
|
-
* return <RedirectToSignIn />;
|
|
37
|
-
* }
|
|
38
|
-
*
|
|
39
|
-
* return <>You are signed in</>;
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @example With Options
|
|
44
|
-
*
|
|
45
|
-
* You can customize the authorization request by passing in props.
|
|
46
|
-
*
|
|
47
|
-
* ```tsx title="With options"
|
|
48
|
-
* "use client";
|
|
49
|
-
*
|
|
50
|
-
* import { useAuth } from "@monocloud/auth-nextjs/client";
|
|
51
|
-
* import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";
|
|
52
|
-
*
|
|
53
|
-
* export default function Home() {
|
|
54
|
-
* const { isLoading, isAuthenticated } = useAuth();
|
|
55
|
-
*
|
|
56
|
-
* if (!isLoading && !isAuthenticated) {
|
|
57
|
-
* return (
|
|
58
|
-
* <RedirectToSignIn
|
|
59
|
-
* returnUrl="/dashboard"
|
|
60
|
-
* loginHint="user@example.com"
|
|
61
|
-
* />
|
|
62
|
-
* );
|
|
63
|
-
* }
|
|
64
|
-
*
|
|
65
|
-
* return <>You are signed in</>;
|
|
66
|
-
* }
|
|
67
|
-
* ```
|
|
68
|
-
*
|
|
69
|
-
* @param props - The props for customizing RedirectToSignIn.
|
|
70
|
-
* @returns
|
|
71
|
-
*
|
|
72
|
-
* @category Components
|
|
73
|
-
*/
|
|
74
|
-
declare const RedirectToSignIn: ({
|
|
75
|
-
returnUrl,
|
|
76
|
-
...authParams
|
|
77
|
-
}: RedirectToSignInProps) => null;
|
|
78
|
-
//#endregion
|
|
79
|
-
//#region src/components/client/protected.d.ts
|
|
80
|
-
/**
|
|
81
|
-
* Props for the `<Protected />` component.
|
|
82
|
-
*
|
|
83
|
-
* @category Types
|
|
84
|
-
*/
|
|
85
|
-
interface ProtectedComponentProps {
|
|
86
|
-
/**
|
|
87
|
-
* Content to render when access is allowed.
|
|
88
|
-
*/
|
|
89
|
-
children: React.ReactNode;
|
|
90
|
-
/**
|
|
91
|
-
* Groups required to view the protected content. By default, the user must belong to **any** of the specified groups.
|
|
92
|
-
*/
|
|
93
|
-
groups?: string[];
|
|
94
|
-
/**
|
|
95
|
-
* Name of the claim that contains groups in the user profile.
|
|
96
|
-
* @defaultValue 'groups'
|
|
97
|
-
*/
|
|
98
|
-
groupsClaim?: string;
|
|
99
|
-
/**
|
|
100
|
-
* If `true`, the user must belong to **all** specified `groups` (instead of any).
|
|
101
|
-
* @defaultValue false
|
|
102
|
-
*/
|
|
103
|
-
matchAllGroups?: boolean;
|
|
104
|
-
/**
|
|
105
|
-
* Content to render when the user is not authenticated.
|
|
106
|
-
*/
|
|
107
|
-
fallback?: React.ReactNode;
|
|
108
|
-
/**
|
|
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).
|
|
110
|
-
*/
|
|
111
|
-
onGroupAccessDenied?: (user: MonoCloudUser) => React.ReactNode;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* `<Protected>` conditionally renders its children based on the user’s authentication state and (optionally) group membership.
|
|
115
|
-
*
|
|
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()}.
|
|
118
|
-
*
|
|
119
|
-
* @example Basic Usage
|
|
120
|
-
*
|
|
121
|
-
* ```tsx title="Basic Usage"
|
|
122
|
-
* "use client";
|
|
123
|
-
*
|
|
124
|
-
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
125
|
-
*
|
|
126
|
-
* export default function Home() {
|
|
127
|
-
* return (
|
|
128
|
-
* <Protected fallback={<>Sign in to view the message.</>}>
|
|
129
|
-
* <>This is the protected content.</>
|
|
130
|
-
* </Protected>
|
|
131
|
-
* );
|
|
132
|
-
* }
|
|
133
|
-
* ```
|
|
134
|
-
*
|
|
135
|
-
* @example With Groups
|
|
136
|
-
*
|
|
137
|
-
* ```tsx title="With Groups"
|
|
138
|
-
* "use client";
|
|
139
|
-
*
|
|
140
|
-
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
141
|
-
*
|
|
142
|
-
* export default function Home() {
|
|
143
|
-
* return (
|
|
144
|
-
* <Protected
|
|
145
|
-
* groups={["admin"]}
|
|
146
|
-
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}
|
|
147
|
-
* >
|
|
148
|
-
* <>Signed in as admin</>
|
|
149
|
-
* </Protected>
|
|
150
|
-
* );
|
|
151
|
-
* }
|
|
152
|
-
* ```
|
|
153
|
-
*
|
|
154
|
-
* @example Requiring all groups
|
|
155
|
-
*
|
|
156
|
-
* ```tsx title="Requiring all groups"
|
|
157
|
-
* "use client";
|
|
158
|
-
*
|
|
159
|
-
* import { Protected } from "@monocloud/auth-nextjs/components/client";
|
|
160
|
-
*
|
|
161
|
-
* export default function Home() {
|
|
162
|
-
* return (
|
|
163
|
-
* <Protected
|
|
164
|
-
* groups={["admin", "billing"]}
|
|
165
|
-
* matchAllGroups
|
|
166
|
-
* onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}
|
|
167
|
-
* >
|
|
168
|
-
* <>Sensitive settings</>
|
|
169
|
-
* </Protected>
|
|
170
|
-
* );
|
|
171
|
-
* }
|
|
172
|
-
* ```
|
|
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
|
|
178
|
-
*/
|
|
179
|
-
declare const Protected: ({
|
|
180
|
-
children,
|
|
181
|
-
groups,
|
|
182
|
-
groupsClaim,
|
|
183
|
-
matchAllGroups,
|
|
184
|
-
fallback,
|
|
185
|
-
onGroupAccessDenied
|
|
186
|
-
}: ProtectedComponentProps) => React.ReactNode | null;
|
|
187
|
-
//#endregion
|
|
188
|
-
export { Protected, type ProtectedComponentProps, RedirectToSignIn, type RedirectToSignInProps };
|
|
189
|
-
//# sourceMappingURL=index.d.mts.map
|
|
1
|
+
import { RedirectToSignIn, RedirectToSignInProps } from "./redirect-to-signin.mjs";
|
|
2
|
+
import { Protected, ProtectedComponentProps } from "./protected.mjs";
|
|
3
|
+
export { Protected, type ProtectedComponentProps, RedirectToSignIn, type RedirectToSignInProps };
|