@monocloud/auth-react 0.1.1-canary-20260605042109

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.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +189 -0
  3. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  4. package/dist/components/process-callback.cjs +62 -0
  5. package/dist/components/process-callback.cjs.map +1 -0
  6. package/dist/components/process-callback.d.mts +46 -0
  7. package/dist/components/process-callback.mjs +60 -0
  8. package/dist/components/process-callback.mjs.map +1 -0
  9. package/dist/components/protected.cjs +85 -0
  10. package/dist/components/protected.cjs.map +1 -0
  11. package/dist/components/protected.d.mts +113 -0
  12. package/dist/components/protected.mjs +83 -0
  13. package/dist/components/protected.mjs.map +1 -0
  14. package/dist/components/signin.cjs +69 -0
  15. package/dist/components/signin.cjs.map +1 -0
  16. package/dist/components/signin.d.mts +70 -0
  17. package/dist/components/signin.mjs +67 -0
  18. package/dist/components/signin.mjs.map +1 -0
  19. package/dist/components/signout.cjs +61 -0
  20. package/dist/components/signout.cjs.map +1 -0
  21. package/dist/components/signout.d.mts +62 -0
  22. package/dist/components/signout.mjs +59 -0
  23. package/dist/components/signout.mjs.map +1 -0
  24. package/dist/components/signup.cjs +68 -0
  25. package/dist/components/signup.cjs.map +1 -0
  26. package/dist/components/signup.d.mts +68 -0
  27. package/dist/components/signup.mjs +66 -0
  28. package/dist/components/signup.mjs.map +1 -0
  29. package/dist/context.cjs +12 -0
  30. package/dist/context.cjs.map +1 -0
  31. package/dist/context.mjs +10 -0
  32. package/dist/context.mjs.map +1 -0
  33. package/dist/index.cjs +79 -0
  34. package/dist/index.d.mts +11 -0
  35. package/dist/index.mjs +11 -0
  36. package/dist/monocloud-auth-provider.cjs +181 -0
  37. package/dist/monocloud-auth-provider.cjs.map +1 -0
  38. package/dist/monocloud-auth-provider.d.mts +64 -0
  39. package/dist/monocloud-auth-provider.mjs +179 -0
  40. package/dist/monocloud-auth-provider.mjs.map +1 -0
  41. package/dist/types.d.mts +107 -0
  42. package/dist/use-auth.cjs +63 -0
  43. package/dist/use-auth.cjs.map +1 -0
  44. package/dist/use-auth.d.mts +56 -0
  45. package/dist/use-auth.mjs +63 -0
  46. package/dist/use-auth.mjs.map +1 -0
  47. package/dist/use-monocloud-client.cjs +47 -0
  48. package/dist/use-monocloud-client.cjs.map +1 -0
  49. package/dist/use-monocloud-client.d.mts +40 -0
  50. package/dist/use-monocloud-client.mjs +47 -0
  51. package/dist/use-monocloud-client.mjs.map +1 -0
  52. package/dist/use-process-callback.cjs +14 -0
  53. package/dist/use-process-callback.cjs.map +1 -0
  54. package/dist/use-process-callback.mjs +14 -0
  55. package/dist/use-process-callback.mjs.map +1 -0
  56. package/package.json +67 -0
@@ -0,0 +1,66 @@
1
+ "use client";
2
+ import { useAuth } from "../use-auth.mjs";
3
+ import React from "react";
4
+ //#region src/components/signup.tsx
5
+ /**
6
+ * `<SignUp>` renders a button that starts the sign-up (registration) flow when
7
+ * clicked.
8
+ *
9
+ * @example Basic Usage
10
+ *
11
+ * ```tsx:src/SignUpButton.tsx tab="Basic Usage" tab-group="SignUp"
12
+ * "use client";
13
+ *
14
+ * import { SignUp } from "@monocloud/auth-react";
15
+ *
16
+ * export default function Home() {
17
+ * return <SignUp>Create account</SignUp>;
18
+ * }
19
+ * ```
20
+ *
21
+ * @example Customized
22
+ *
23
+ * ```tsx:src/SignUpButton.tsx tab="Customized" tab-group="SignUp"
24
+ * "use client";
25
+ *
26
+ * import { SignUp } from "@monocloud/auth-react";
27
+ *
28
+ * export default function Home() {
29
+ * return (
30
+ * <SignUp returnUrl="/welcome">
31
+ * Sign-up now
32
+ * </SignUp>
33
+ * );
34
+ * }
35
+ * ```
36
+ *
37
+ * @param props - Sign-up options.
38
+ * @returns A button that triggers sign-up on click.
39
+ *
40
+ * @category Components
41
+ */
42
+ const SignUp = ({ children, maxAge, uiLocales, mode, acrValues, display, resource, returnUrl, scopes, appState, ...props }) => {
43
+ const { signIn } = useAuth();
44
+ return /* @__PURE__ */ React.createElement("button", {
45
+ ...props,
46
+ type: "button",
47
+ onClick: () => {
48
+ signIn({
49
+ signUp: true,
50
+ maxAge,
51
+ uiLocales,
52
+ mode,
53
+ acrValues,
54
+ display,
55
+ resource,
56
+ returnUrl,
57
+ scopes,
58
+ appState
59
+ });
60
+ }
61
+ }, children);
62
+ };
63
+ //#endregion
64
+ export { SignUp };
65
+
66
+ //# sourceMappingURL=signup.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signup.mjs","names":[],"sources":["../../src/components/signup.tsx"],"sourcesContent":["'use client';\n\nimport type { SignInOptions } from '@monocloud/auth-web-js';\nimport React from 'react';\nimport { useAuth } from '../use-auth';\n\n/**\n * Props for the `<SignUp />` component.\n *\n * @category Types\n */\nexport interface SignUpProps\n extends\n Omit<\n SignInOptions,\n 'signUp' | 'authenticatorHint' | 'loginHint' | 'prompt'\n >,\n React.ButtonHTMLAttributes<HTMLButtonElement> {\n /**\n * Content rendered inside the button.\n */\n children: React.ReactNode;\n}\n\n/**\n * `<SignUp>` renders a button that starts the sign-up (registration) flow when\n * clicked.\n *\n * @example Basic Usage\n *\n * ```tsx:src/SignUpButton.tsx tab=\"Basic Usage\" tab-group=\"SignUp\"\n * \"use client\";\n *\n * import { SignUp } from \"@monocloud/auth-react\";\n *\n * export default function Home() {\n * return <SignUp>Create account</SignUp>;\n * }\n * ```\n *\n * @example Customized\n *\n * ```tsx:src/SignUpButton.tsx tab=\"Customized\" tab-group=\"SignUp\"\n * \"use client\";\n *\n * import { SignUp } from \"@monocloud/auth-react\";\n *\n * export default function Home() {\n * return (\n * <SignUp returnUrl=\"/welcome\">\n * Sign-up now\n * </SignUp>\n * );\n * }\n * ```\n *\n * @param props - Sign-up options.\n * @returns A button that triggers sign-up on click.\n *\n * @category Components\n */\nexport const SignUp = ({\n children,\n maxAge,\n uiLocales,\n mode,\n acrValues,\n display,\n resource,\n returnUrl,\n scopes,\n appState,\n ...props\n}: SignUpProps): React.JSX.Element => {\n const { signIn } = useAuth();\n\n return (\n <button\n {...props}\n type=\"button\"\n onClick={() => {\n void signIn({\n signUp: true,\n maxAge,\n uiLocales,\n mode,\n acrValues,\n display,\n resource,\n returnUrl,\n scopes,\n appState,\n });\n }}\n >\n {children}\n </button>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6DA,MAAa,UAAU,EACrB,UACA,QACA,WACA,MACA,WACA,SACA,UACA,WACA,QACA,UACA,GAAG,YACiC;CACpC,MAAM,EAAE,WAAW,QAAQ;CAE3B,OACE,sBAAA,cAAC,UAAD;EACE,GAAI;EACJ,MAAK;EACL,eAAe;GACb,OAAY;IACV,QAAQ;IACR;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;GACF,CAAC;EACH;CAGM,GADL,QACK;AAEZ"}
@@ -0,0 +1,12 @@
1
+ "use client";
2
+ let react = require("react");
3
+ //#region src/context.ts
4
+ const MonoCloudAuthContext = (0, react.createContext)(void 0);
5
+ const MonoCloudClientContext = (0, react.createContext)(void 0);
6
+ const MonoCloudProcessCallbackContext = (0, react.createContext)(void 0);
7
+ //#endregion
8
+ exports.MonoCloudAuthContext = MonoCloudAuthContext;
9
+ exports.MonoCloudClientContext = MonoCloudClientContext;
10
+ exports.MonoCloudProcessCallbackContext = MonoCloudProcessCallbackContext;
11
+
12
+ //# sourceMappingURL=context.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.cjs","names":[],"sources":["../src/context.ts"],"sourcesContent":["'use client';\n\nimport type { MonoCloudWebJSClient } from '@monocloud/auth-web-js';\nimport { createContext } from 'react';\nimport type { MonoCloudAuth } from './types';\n\nexport const MonoCloudAuthContext = createContext<MonoCloudAuth | undefined>(\n undefined\n);\n\nexport const MonoCloudClientContext = createContext<\n MonoCloudWebJSClient | undefined\n>(undefined);\n\nexport const MonoCloudProcessCallbackContext = createContext<\n (() => Promise<void>) | undefined\n>(undefined);\n"],"mappings":";;;AAMA,MAAa,wBAAA,GAAA,MAAA,eACX,KAAA,CACF;AAEA,MAAa,0BAAA,GAAA,MAAA,eAEX,KAAA,CAAS;AAEX,MAAa,mCAAA,GAAA,MAAA,eAEX,KAAA,CAAS"}
@@ -0,0 +1,10 @@
1
+ "use client";
2
+ import { createContext } from "react";
3
+ //#region src/context.ts
4
+ const MonoCloudAuthContext = createContext(void 0);
5
+ const MonoCloudClientContext = createContext(void 0);
6
+ const MonoCloudProcessCallbackContext = createContext(void 0);
7
+ //#endregion
8
+ export { MonoCloudAuthContext, MonoCloudClientContext, MonoCloudProcessCallbackContext };
9
+
10
+ //# sourceMappingURL=context.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.mjs","names":[],"sources":["../src/context.ts"],"sourcesContent":["'use client';\n\nimport type { MonoCloudWebJSClient } from '@monocloud/auth-web-js';\nimport { createContext } from 'react';\nimport type { MonoCloudAuth } from './types';\n\nexport const MonoCloudAuthContext = createContext<MonoCloudAuth | undefined>(\n undefined\n);\n\nexport const MonoCloudClientContext = createContext<\n MonoCloudWebJSClient | undefined\n>(undefined);\n\nexport const MonoCloudProcessCallbackContext = createContext<\n (() => Promise<void>) | undefined\n>(undefined);\n"],"mappings":";;;AAMA,MAAa,uBAAuB,cAClC,KAAA,CACF;AAEA,MAAa,yBAAyB,cAEpC,KAAA,CAAS;AAEX,MAAa,kCAAkC,cAE7C,KAAA,CAAS"}
package/dist/index.cjs ADDED
@@ -0,0 +1,79 @@
1
+ "use client";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const require_monocloud_auth_provider = require("./monocloud-auth-provider.cjs");
4
+ const require_use_auth = require("./use-auth.cjs");
5
+ const require_use_monocloud_client = require("./use-monocloud-client.cjs");
6
+ const require_protected = require("./components/protected.cjs");
7
+ const require_process_callback = require("./components/process-callback.cjs");
8
+ const require_signin = require("./components/signin.cjs");
9
+ const require_signup = require("./components/signup.cjs");
10
+ const require_signout = require("./components/signout.cjs");
11
+ let _monocloud_auth_web_js = require("@monocloud/auth-web-js");
12
+ Object.defineProperty(exports, "LocalStorage", {
13
+ enumerable: true,
14
+ get: function() {
15
+ return _monocloud_auth_web_js.LocalStorage;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "MemoryStorage", {
19
+ enumerable: true,
20
+ get: function() {
21
+ return _monocloud_auth_web_js.MemoryStorage;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "MonoCloudAuthBaseError", {
25
+ enumerable: true,
26
+ get: function() {
27
+ return _monocloud_auth_web_js.MonoCloudAuthBaseError;
28
+ }
29
+ });
30
+ exports.MonoCloudAuthProvider = require_monocloud_auth_provider.MonoCloudAuthProvider;
31
+ Object.defineProperty(exports, "MonoCloudHttpError", {
32
+ enumerable: true,
33
+ get: function() {
34
+ return _monocloud_auth_web_js.MonoCloudHttpError;
35
+ }
36
+ });
37
+ Object.defineProperty(exports, "MonoCloudJsError", {
38
+ enumerable: true,
39
+ get: function() {
40
+ return _monocloud_auth_web_js.MonoCloudJsError;
41
+ }
42
+ });
43
+ Object.defineProperty(exports, "MonoCloudOPError", {
44
+ enumerable: true,
45
+ get: function() {
46
+ return _monocloud_auth_web_js.MonoCloudOPError;
47
+ }
48
+ });
49
+ Object.defineProperty(exports, "MonoCloudTokenError", {
50
+ enumerable: true,
51
+ get: function() {
52
+ return _monocloud_auth_web_js.MonoCloudTokenError;
53
+ }
54
+ });
55
+ Object.defineProperty(exports, "MonoCloudValidationError", {
56
+ enumerable: true,
57
+ get: function() {
58
+ return _monocloud_auth_web_js.MonoCloudValidationError;
59
+ }
60
+ });
61
+ Object.defineProperty(exports, "MonoCloudWebJSClient", {
62
+ enumerable: true,
63
+ get: function() {
64
+ return _monocloud_auth_web_js.MonoCloudWebJSClient;
65
+ }
66
+ });
67
+ exports.ProcessCallback = require_process_callback.ProcessCallback;
68
+ exports.Protected = require_protected.Protected;
69
+ Object.defineProperty(exports, "SessionStorage", {
70
+ enumerable: true,
71
+ get: function() {
72
+ return _monocloud_auth_web_js.SessionStorage;
73
+ }
74
+ });
75
+ exports.SignIn = require_signin.SignIn;
76
+ exports.SignOut = require_signout.SignOut;
77
+ exports.SignUp = require_signup.SignUp;
78
+ exports.useAuth = require_use_auth.useAuth;
79
+ exports.useMonoCloudClient = require_use_monocloud_client.useMonoCloudClient;
@@ -0,0 +1,11 @@
1
+ import { AuthState, MonoCloudAuth, MonoCloudAuthProviderProps, ProcessCallbackProps } from "./types.mjs";
2
+ import { MonoCloudAuthProvider } from "./monocloud-auth-provider.mjs";
3
+ import { useAuth } from "./use-auth.mjs";
4
+ import { useMonoCloudClient } from "./use-monocloud-client.mjs";
5
+ import { Protected, ProtectedComponentProps } from "./components/protected.mjs";
6
+ import { ProcessCallback } from "./components/process-callback.mjs";
7
+ import { SignIn, SignInProps } from "./components/signin.mjs";
8
+ import { SignUp, SignUpProps } from "./components/signup.mjs";
9
+ import { SignOut, SignOutProps } from "./components/signout.mjs";
10
+ import { AccessToken, Address, ApplicationState, Authenticators, AuthorizationParams, CallbackState, ClientAuthMethod, CodeChallengeMethod, DefaultAuthParams, DisplayOptions, GetTokensOptions, Group, IStorage, IdTokenClaims, Indicator, InteractionMode, Jwk, LocalStorage, MemoryStorage, MonoCloudAuthBaseError, MonoCloudHttpError, MonoCloudJsError, MonoCloudOPError, MonoCloudSession, MonoCloudTokenError, MonoCloudTokens, MonoCloudUser, MonoCloudValidationError, MonoCloudWebJSClient, MonoCloudWebJSClientOptions, OnSessionCreating, PostCallback, Prompt, RefreshGrantOptions, RefreshOptions, ResponseModes, ResponseTypes, SecurityAlgorithms, SessionStorage, SignInOptions, SignInSilentOptions, SignOutOptions, UserinfoResponse } from "@monocloud/auth-web-js";
11
+ export { type AccessToken, type Address, type ApplicationState, type AuthState, type Authenticators, type AuthorizationParams, type CallbackState, type ClientAuthMethod, type CodeChallengeMethod, type DefaultAuthParams, type DisplayOptions, type GetTokensOptions, type Group, type IStorage, type IdTokenClaims, type Indicator, type InteractionMode, type Jwk, LocalStorage, MemoryStorage, type MonoCloudAuth, MonoCloudAuthBaseError, MonoCloudAuthProvider, type MonoCloudAuthProviderProps, MonoCloudHttpError, MonoCloudJsError, MonoCloudOPError, type MonoCloudSession, MonoCloudTokenError, type MonoCloudTokens, type MonoCloudUser, MonoCloudValidationError, MonoCloudWebJSClient, type MonoCloudWebJSClientOptions, type OnSessionCreating, type PostCallback, ProcessCallback, type ProcessCallbackProps, type Prompt, Protected, type ProtectedComponentProps, type RefreshGrantOptions, type RefreshOptions, type ResponseModes, type ResponseTypes, type SecurityAlgorithms, SessionStorage, SignIn, type SignInOptions, type SignInProps, type SignInSilentOptions, SignOut, type SignOutOptions, type SignOutProps, SignUp, type SignUpProps, type UserinfoResponse, useAuth, useMonoCloudClient };
package/dist/index.mjs ADDED
@@ -0,0 +1,11 @@
1
+ "use client";
2
+ import { MonoCloudAuthProvider } from "./monocloud-auth-provider.mjs";
3
+ import { useAuth } from "./use-auth.mjs";
4
+ import { useMonoCloudClient } from "./use-monocloud-client.mjs";
5
+ import { Protected } from "./components/protected.mjs";
6
+ import { ProcessCallback } from "./components/process-callback.mjs";
7
+ import { SignIn } from "./components/signin.mjs";
8
+ import { SignUp } from "./components/signup.mjs";
9
+ import { SignOut } from "./components/signout.mjs";
10
+ import { LocalStorage, MemoryStorage, MonoCloudAuthBaseError, MonoCloudHttpError, MonoCloudJsError, MonoCloudOPError, MonoCloudTokenError, MonoCloudValidationError, MonoCloudWebJSClient, SessionStorage } from "@monocloud/auth-web-js";
11
+ export { LocalStorage, MemoryStorage, MonoCloudAuthBaseError, MonoCloudAuthProvider, MonoCloudHttpError, MonoCloudJsError, MonoCloudOPError, MonoCloudTokenError, MonoCloudValidationError, MonoCloudWebJSClient, ProcessCallback, Protected, SessionStorage, SignIn, SignOut, SignUp, useAuth, useMonoCloudClient };
@@ -0,0 +1,181 @@
1
+ "use client";
2
+ const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
3
+ const require_context = require("./context.cjs");
4
+ let _monocloud_auth_web_js = require("@monocloud/auth-web-js");
5
+ let react = require("react");
6
+ react = require_runtime.__toESM(react);
7
+ //#region src/monocloud-auth-provider.tsx
8
+ const initialState = {
9
+ isLoading: true,
10
+ isAuthenticated: false
11
+ };
12
+ /**
13
+ * `<MonoCloudAuthProvider>` initializes the MonoCloud JavaScript client and makes
14
+ * the authentication state and actions available through {@link useAuth}.
15
+ *
16
+ * @example Basic setup
17
+ *
18
+ * ```tsx:src/main.tsx tab="Basic setup" tab-group="MonoCloudAuthProvider"
19
+ * "use client";
20
+ *
21
+ * import { MonoCloudAuthProvider } from "@monocloud/auth-react";
22
+ *
23
+ * export default function Root() {
24
+ * return (
25
+ * <MonoCloudAuthProvider
26
+ * tenantDomain="https://your-tenant-domain"
27
+ * clientId="your-client-id"
28
+ * >
29
+ * <App />
30
+ * </MonoCloudAuthProvider>
31
+ * );
32
+ * }
33
+ * ```
34
+ *
35
+ * @example Client-side router navigation
36
+ *
37
+ * ```tsx:src/main.tsx tab="Client-side router navigation" tab-group="MonoCloudAuthProvider"
38
+ * "use client";
39
+ *
40
+ * import { MonoCloudAuthProvider } from "@monocloud/auth-react";
41
+ * import { useNavigate } from "react-router-dom";
42
+ *
43
+ * export default function Root() {
44
+ * const navigate = useNavigate();
45
+ *
46
+ * return (
47
+ * <MonoCloudAuthProvider
48
+ * tenantDomain="https://your-tenant-domain"
49
+ * clientId="your-client-id"
50
+ * autoProcessCallback={false}
51
+ * postCallback={state => navigate(state.returnUrl ?? "/")}
52
+ * >
53
+ * <App />
54
+ * </MonoCloudAuthProvider>
55
+ * );
56
+ * }
57
+ * ```
58
+ *
59
+ * @param props - Props for configuring the provider and the underlying client.
60
+ * @returns The provider element wrapping `children`.
61
+ *
62
+ * @category Components
63
+ */
64
+ const MonoCloudAuthProvider = ({ children, autoProcessCallback = true, ...clientOptions }) => {
65
+ const [state, setState] = (0, react.useState)(initialState);
66
+ const [client] = (0, react.useState)(() => new _monocloud_auth_web_js.MonoCloudWebJSClient(clientOptions));
67
+ const syncSession = (0, react.useCallback)(async () => {
68
+ const session = await client.getSession();
69
+ setState({
70
+ isLoading: false,
71
+ isAuthenticated: !!session,
72
+ user: session === null || session === void 0 ? void 0 : session.user,
73
+ session,
74
+ error: void 0
75
+ });
76
+ }, [client]);
77
+ const processCallback = (0, react.useCallback)(async () => {
78
+ setState((prev) => ({
79
+ ...prev,
80
+ isLoading: true
81
+ }));
82
+ try {
83
+ await client.processCallback();
84
+ await syncSession();
85
+ } catch (e) {
86
+ setState({
87
+ isLoading: false,
88
+ isAuthenticated: false,
89
+ user: void 0,
90
+ session: void 0,
91
+ error: e
92
+ });
93
+ throw e;
94
+ }
95
+ }, [client, syncSession]);
96
+ const initialized = (0, react.useRef)(false);
97
+ (0, react.useEffect)(() => {
98
+ /* v8 ignore start -- StrictMode double-invocation guard */
99
+ if (initialized.current) return;
100
+ /* v8 ignore stop */
101
+ initialized.current = true;
102
+ if (autoProcessCallback) processCallback().catch(() => {});
103
+ else syncSession();
104
+ }, [
105
+ autoProcessCallback,
106
+ processCallback,
107
+ syncSession
108
+ ]);
109
+ const signIn = (0, react.useCallback)(async (signInOptions) => {
110
+ setState((prev) => ({
111
+ ...prev,
112
+ isLoading: true
113
+ }));
114
+ try {
115
+ await client.signIn(signInOptions);
116
+ await syncSession();
117
+ } catch (e) {
118
+ setState((prev) => ({
119
+ ...prev,
120
+ isLoading: false,
121
+ error: e
122
+ }));
123
+ }
124
+ }, [client, syncSession]);
125
+ const signOut = (0, react.useCallback)(async (signOutOptions) => {
126
+ setState((prev) => ({
127
+ ...prev,
128
+ isLoading: true
129
+ }));
130
+ try {
131
+ await client.signOut(signOutOptions);
132
+ await syncSession();
133
+ } catch (e) {
134
+ setState((prev) => ({
135
+ ...prev,
136
+ isLoading: false,
137
+ error: e
138
+ }));
139
+ }
140
+ }, [client, syncSession]);
141
+ const signInSilent = (0, react.useCallback)(async (signInSilentOptions) => {
142
+ const session = await client.signInSilent(signInSilentOptions);
143
+ await syncSession();
144
+ return session;
145
+ }, [client, syncSession]);
146
+ const refreshSession = (0, react.useCallback)(async (refreshOptions) => {
147
+ await client.refreshSession(refreshOptions);
148
+ await syncSession();
149
+ }, [client, syncSession]);
150
+ const refetchUserInfo = (0, react.useCallback)(async () => {
151
+ await client.refetchUserInfo();
152
+ await syncSession();
153
+ }, [client, syncSession]);
154
+ const getTokens = (0, react.useCallback)(async (options) => {
155
+ const tokens = await client.getTokens(options);
156
+ await syncSession();
157
+ return tokens;
158
+ }, [client, syncSession]);
159
+ const value = (0, react.useMemo)(() => ({
160
+ ...state,
161
+ signIn,
162
+ signOut,
163
+ signInSilent,
164
+ refreshSession,
165
+ refetchUserInfo,
166
+ getTokens
167
+ }), [
168
+ state,
169
+ signIn,
170
+ signOut,
171
+ signInSilent,
172
+ refreshSession,
173
+ refetchUserInfo,
174
+ getTokens
175
+ ]);
176
+ return /* @__PURE__ */ react.default.createElement(require_context.MonoCloudClientContext.Provider, { value: client }, /* @__PURE__ */ react.default.createElement(require_context.MonoCloudProcessCallbackContext.Provider, { value: processCallback }, /* @__PURE__ */ react.default.createElement(require_context.MonoCloudAuthContext.Provider, { value }, children)));
177
+ };
178
+ //#endregion
179
+ exports.MonoCloudAuthProvider = MonoCloudAuthProvider;
180
+
181
+ //# sourceMappingURL=monocloud-auth-provider.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"monocloud-auth-provider.cjs","names":["MonoCloudWebJSClient","MonoCloudClientContext","MonoCloudProcessCallbackContext","MonoCloudAuthContext"],"sources":["../src/monocloud-auth-provider.tsx"],"sourcesContent":["'use client';\n\nimport {\n MonoCloudWebJSClient,\n type GetTokensOptions,\n type MonoCloudSession,\n type MonoCloudTokens,\n type RefreshOptions,\n type SignInOptions,\n type SignInSilentOptions,\n type SignOutOptions,\n} from '@monocloud/auth-web-js';\nimport React, {\n useCallback,\n useEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport {\n MonoCloudAuthContext,\n MonoCloudClientContext,\n MonoCloudProcessCallbackContext,\n} from './context';\nimport type {\n AuthState,\n MonoCloudAuth,\n MonoCloudAuthProviderProps,\n} from './types';\n\nconst initialState: AuthState = {\n isLoading: true,\n isAuthenticated: false,\n};\n\n/**\n * `<MonoCloudAuthProvider>` initializes the MonoCloud JavaScript client and makes\n * the authentication state and actions available through {@link useAuth}.\n *\n * @example Basic setup\n *\n * ```tsx:src/main.tsx tab=\"Basic setup\" tab-group=\"MonoCloudAuthProvider\"\n * \"use client\";\n *\n * import { MonoCloudAuthProvider } from \"@monocloud/auth-react\";\n *\n * export default function Root() {\n * return (\n * <MonoCloudAuthProvider\n * tenantDomain=\"https://your-tenant-domain\"\n * clientId=\"your-client-id\"\n * >\n * <App />\n * </MonoCloudAuthProvider>\n * );\n * }\n * ```\n *\n * @example Client-side router navigation\n *\n * ```tsx:src/main.tsx tab=\"Client-side router navigation\" tab-group=\"MonoCloudAuthProvider\"\n * \"use client\";\n *\n * import { MonoCloudAuthProvider } from \"@monocloud/auth-react\";\n * import { useNavigate } from \"react-router-dom\";\n *\n * export default function Root() {\n * const navigate = useNavigate();\n *\n * return (\n * <MonoCloudAuthProvider\n * tenantDomain=\"https://your-tenant-domain\"\n * clientId=\"your-client-id\"\n * autoProcessCallback={false}\n * postCallback={state => navigate(state.returnUrl ?? \"/\")}\n * >\n * <App />\n * </MonoCloudAuthProvider>\n * );\n * }\n * ```\n *\n * @param props - Props for configuring the provider and the underlying client.\n * @returns The provider element wrapping `children`.\n *\n * @category Components\n */\nexport const MonoCloudAuthProvider = ({\n children,\n autoProcessCallback = true,\n ...clientOptions\n}: MonoCloudAuthProviderProps): React.JSX.Element => {\n const [state, setState] = useState<AuthState>(initialState);\n\n const [client] = useState<MonoCloudWebJSClient>(\n () => new MonoCloudWebJSClient(clientOptions)\n );\n\n const syncSession = useCallback(async (): Promise<void> => {\n const session = await client.getSession();\n setState({\n isLoading: false,\n isAuthenticated: !!session,\n user: session?.user,\n session,\n error: undefined,\n });\n }, [client]);\n\n const processCallback = useCallback(async (): Promise<void> => {\n setState(prev => ({ ...prev, isLoading: true }));\n\n try {\n await client.processCallback();\n await syncSession();\n } catch (e) {\n setState({\n isLoading: false,\n isAuthenticated: false,\n user: undefined,\n session: undefined,\n error: e as Error,\n });\n throw e;\n }\n }, [client, syncSession]);\n\n const initialized = useRef(false);\n useEffect(() => {\n /* v8 ignore start -- StrictMode double-invocation guard */\n if (initialized.current) {\n return;\n }\n /* v8 ignore stop */\n initialized.current = true;\n\n if (autoProcessCallback) {\n processCallback().catch(() => {});\n } else {\n syncSession();\n }\n }, [autoProcessCallback, processCallback, syncSession]);\n\n const signIn = useCallback(\n async (signInOptions?: SignInOptions): Promise<void> => {\n setState(prev => ({ ...prev, isLoading: true }));\n try {\n await client.signIn(signInOptions);\n await syncSession();\n } catch (e) {\n setState(prev => ({ ...prev, isLoading: false, error: e as Error }));\n }\n },\n [client, syncSession]\n );\n\n const signOut = useCallback(\n async (signOutOptions?: SignOutOptions): Promise<void> => {\n setState(prev => ({ ...prev, isLoading: true }));\n try {\n await client.signOut(signOutOptions);\n await syncSession();\n } catch (e) {\n setState(prev => ({ ...prev, isLoading: false, error: e as Error }));\n }\n },\n [client, syncSession]\n );\n\n const signInSilent = useCallback(\n async (\n signInSilentOptions?: SignInSilentOptions\n ): Promise<MonoCloudSession> => {\n const session = await client.signInSilent(signInSilentOptions);\n await syncSession();\n return session;\n },\n [client, syncSession]\n );\n\n const refreshSession = useCallback(\n async (refreshOptions?: RefreshOptions): Promise<void> => {\n await client.refreshSession(refreshOptions);\n await syncSession();\n },\n [client, syncSession]\n );\n\n const refetchUserInfo = useCallback(async (): Promise<void> => {\n await client.refetchUserInfo();\n await syncSession();\n }, [client, syncSession]);\n\n const getTokens = useCallback(\n async (options?: GetTokensOptions): Promise<MonoCloudTokens> => {\n const tokens = await client.getTokens(options);\n await syncSession();\n return tokens;\n },\n [client, syncSession]\n );\n\n const value = useMemo<MonoCloudAuth>(\n () => ({\n ...state,\n signIn,\n signOut,\n signInSilent,\n refreshSession,\n refetchUserInfo,\n getTokens,\n }),\n [\n state,\n signIn,\n signOut,\n signInSilent,\n refreshSession,\n refetchUserInfo,\n getTokens,\n ]\n );\n\n return (\n <MonoCloudClientContext.Provider value={client}>\n <MonoCloudProcessCallbackContext.Provider value={processCallback}>\n <MonoCloudAuthContext.Provider value={value}>\n {children}\n </MonoCloudAuthContext.Provider>\n </MonoCloudProcessCallbackContext.Provider>\n </MonoCloudClientContext.Provider>\n );\n};\n"],"mappings":"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"}
@@ -0,0 +1,64 @@
1
+ import { MonoCloudAuthProviderProps } from "./types.mjs";
2
+ import React from "react";
3
+
4
+ //#region src/monocloud-auth-provider.d.ts
5
+ /**
6
+ * `<MonoCloudAuthProvider>` initializes the MonoCloud JavaScript client and makes
7
+ * the authentication state and actions available through {@link useAuth}.
8
+ *
9
+ * @example Basic setup
10
+ *
11
+ * ```tsx:src/main.tsx tab="Basic setup" tab-group="MonoCloudAuthProvider"
12
+ * "use client";
13
+ *
14
+ * import { MonoCloudAuthProvider } from "@monocloud/auth-react";
15
+ *
16
+ * export default function Root() {
17
+ * return (
18
+ * <MonoCloudAuthProvider
19
+ * tenantDomain="https://your-tenant-domain"
20
+ * clientId="your-client-id"
21
+ * >
22
+ * <App />
23
+ * </MonoCloudAuthProvider>
24
+ * );
25
+ * }
26
+ * ```
27
+ *
28
+ * @example Client-side router navigation
29
+ *
30
+ * ```tsx:src/main.tsx tab="Client-side router navigation" tab-group="MonoCloudAuthProvider"
31
+ * "use client";
32
+ *
33
+ * import { MonoCloudAuthProvider } from "@monocloud/auth-react";
34
+ * import { useNavigate } from "react-router-dom";
35
+ *
36
+ * export default function Root() {
37
+ * const navigate = useNavigate();
38
+ *
39
+ * return (
40
+ * <MonoCloudAuthProvider
41
+ * tenantDomain="https://your-tenant-domain"
42
+ * clientId="your-client-id"
43
+ * autoProcessCallback={false}
44
+ * postCallback={state => navigate(state.returnUrl ?? "/")}
45
+ * >
46
+ * <App />
47
+ * </MonoCloudAuthProvider>
48
+ * );
49
+ * }
50
+ * ```
51
+ *
52
+ * @param props - Props for configuring the provider and the underlying client.
53
+ * @returns The provider element wrapping `children`.
54
+ *
55
+ * @category Components
56
+ */
57
+ declare const MonoCloudAuthProvider: ({
58
+ children,
59
+ autoProcessCallback,
60
+ ...clientOptions
61
+ }: MonoCloudAuthProviderProps) => React.JSX.Element;
62
+ //#endregion
63
+ export { MonoCloudAuthProvider };
64
+ //# sourceMappingURL=monocloud-auth-provider.d.mts.map
@@ -0,0 +1,179 @@
1
+ "use client";
2
+ import { MonoCloudAuthContext, MonoCloudClientContext, MonoCloudProcessCallbackContext } from "./context.mjs";
3
+ import { MonoCloudWebJSClient } from "@monocloud/auth-web-js";
4
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
5
+ //#region src/monocloud-auth-provider.tsx
6
+ const initialState = {
7
+ isLoading: true,
8
+ isAuthenticated: false
9
+ };
10
+ /**
11
+ * `<MonoCloudAuthProvider>` initializes the MonoCloud JavaScript client and makes
12
+ * the authentication state and actions available through {@link useAuth}.
13
+ *
14
+ * @example Basic setup
15
+ *
16
+ * ```tsx:src/main.tsx tab="Basic setup" tab-group="MonoCloudAuthProvider"
17
+ * "use client";
18
+ *
19
+ * import { MonoCloudAuthProvider } from "@monocloud/auth-react";
20
+ *
21
+ * export default function Root() {
22
+ * return (
23
+ * <MonoCloudAuthProvider
24
+ * tenantDomain="https://your-tenant-domain"
25
+ * clientId="your-client-id"
26
+ * >
27
+ * <App />
28
+ * </MonoCloudAuthProvider>
29
+ * );
30
+ * }
31
+ * ```
32
+ *
33
+ * @example Client-side router navigation
34
+ *
35
+ * ```tsx:src/main.tsx tab="Client-side router navigation" tab-group="MonoCloudAuthProvider"
36
+ * "use client";
37
+ *
38
+ * import { MonoCloudAuthProvider } from "@monocloud/auth-react";
39
+ * import { useNavigate } from "react-router-dom";
40
+ *
41
+ * export default function Root() {
42
+ * const navigate = useNavigate();
43
+ *
44
+ * return (
45
+ * <MonoCloudAuthProvider
46
+ * tenantDomain="https://your-tenant-domain"
47
+ * clientId="your-client-id"
48
+ * autoProcessCallback={false}
49
+ * postCallback={state => navigate(state.returnUrl ?? "/")}
50
+ * >
51
+ * <App />
52
+ * </MonoCloudAuthProvider>
53
+ * );
54
+ * }
55
+ * ```
56
+ *
57
+ * @param props - Props for configuring the provider and the underlying client.
58
+ * @returns The provider element wrapping `children`.
59
+ *
60
+ * @category Components
61
+ */
62
+ const MonoCloudAuthProvider = ({ children, autoProcessCallback = true, ...clientOptions }) => {
63
+ const [state, setState] = useState(initialState);
64
+ const [client] = useState(() => new MonoCloudWebJSClient(clientOptions));
65
+ const syncSession = useCallback(async () => {
66
+ const session = await client.getSession();
67
+ setState({
68
+ isLoading: false,
69
+ isAuthenticated: !!session,
70
+ user: session === null || session === void 0 ? void 0 : session.user,
71
+ session,
72
+ error: void 0
73
+ });
74
+ }, [client]);
75
+ const processCallback = useCallback(async () => {
76
+ setState((prev) => ({
77
+ ...prev,
78
+ isLoading: true
79
+ }));
80
+ try {
81
+ await client.processCallback();
82
+ await syncSession();
83
+ } catch (e) {
84
+ setState({
85
+ isLoading: false,
86
+ isAuthenticated: false,
87
+ user: void 0,
88
+ session: void 0,
89
+ error: e
90
+ });
91
+ throw e;
92
+ }
93
+ }, [client, syncSession]);
94
+ const initialized = useRef(false);
95
+ useEffect(() => {
96
+ /* v8 ignore start -- StrictMode double-invocation guard */
97
+ if (initialized.current) return;
98
+ /* v8 ignore stop */
99
+ initialized.current = true;
100
+ if (autoProcessCallback) processCallback().catch(() => {});
101
+ else syncSession();
102
+ }, [
103
+ autoProcessCallback,
104
+ processCallback,
105
+ syncSession
106
+ ]);
107
+ const signIn = useCallback(async (signInOptions) => {
108
+ setState((prev) => ({
109
+ ...prev,
110
+ isLoading: true
111
+ }));
112
+ try {
113
+ await client.signIn(signInOptions);
114
+ await syncSession();
115
+ } catch (e) {
116
+ setState((prev) => ({
117
+ ...prev,
118
+ isLoading: false,
119
+ error: e
120
+ }));
121
+ }
122
+ }, [client, syncSession]);
123
+ const signOut = useCallback(async (signOutOptions) => {
124
+ setState((prev) => ({
125
+ ...prev,
126
+ isLoading: true
127
+ }));
128
+ try {
129
+ await client.signOut(signOutOptions);
130
+ await syncSession();
131
+ } catch (e) {
132
+ setState((prev) => ({
133
+ ...prev,
134
+ isLoading: false,
135
+ error: e
136
+ }));
137
+ }
138
+ }, [client, syncSession]);
139
+ const signInSilent = useCallback(async (signInSilentOptions) => {
140
+ const session = await client.signInSilent(signInSilentOptions);
141
+ await syncSession();
142
+ return session;
143
+ }, [client, syncSession]);
144
+ const refreshSession = useCallback(async (refreshOptions) => {
145
+ await client.refreshSession(refreshOptions);
146
+ await syncSession();
147
+ }, [client, syncSession]);
148
+ const refetchUserInfo = useCallback(async () => {
149
+ await client.refetchUserInfo();
150
+ await syncSession();
151
+ }, [client, syncSession]);
152
+ const getTokens = useCallback(async (options) => {
153
+ const tokens = await client.getTokens(options);
154
+ await syncSession();
155
+ return tokens;
156
+ }, [client, syncSession]);
157
+ const value = useMemo(() => ({
158
+ ...state,
159
+ signIn,
160
+ signOut,
161
+ signInSilent,
162
+ refreshSession,
163
+ refetchUserInfo,
164
+ getTokens
165
+ }), [
166
+ state,
167
+ signIn,
168
+ signOut,
169
+ signInSilent,
170
+ refreshSession,
171
+ refetchUserInfo,
172
+ getTokens
173
+ ]);
174
+ return /* @__PURE__ */ React.createElement(MonoCloudClientContext.Provider, { value: client }, /* @__PURE__ */ React.createElement(MonoCloudProcessCallbackContext.Provider, { value: processCallback }, /* @__PURE__ */ React.createElement(MonoCloudAuthContext.Provider, { value }, children)));
175
+ };
176
+ //#endregion
177
+ export { MonoCloudAuthProvider };
178
+
179
+ //# sourceMappingURL=monocloud-auth-provider.mjs.map