@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
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MonoCloud
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,189 @@
1
+ <div align="center">
2
+ <a href="https://www.monocloud.com?utm_source=github&utm_medium=auth_js" target="_blank" rel="noopener noreferrer">
3
+ <picture>
4
+ <img src="https://raw.githubusercontent.com/monocloud/auth-js/refs/heads/main/packages/react/banner.svg" alt="MonoCloud Banner">
5
+ </picture>
6
+ </a>
7
+ <div align="right">
8
+ <a href="https://www.npmjs.com/package/@monocloud/auth-react" target="_blank">
9
+ <img src="https://img.shields.io/npm/v/@monocloud/auth-react" alt="NPM" />
10
+ </a>
11
+ <a href="https://opensource.org/licenses/MIT">
12
+ <img src="https://img.shields.io/:license-MIT-blue.svg?style=flat" alt="License: MIT" />
13
+ </a>
14
+ <a href="https://github.com/monocloud/auth-js/actions/workflows/build.yml">
15
+ <img src="https://github.com/monocloud/auth-js/actions/workflows/build.yml/badge.svg" alt="Build Status" />
16
+ </a>
17
+ </div>
18
+ </div>
19
+
20
+ ## Introduction
21
+
22
+ **MonoCloud React Authentication SDK – secure authentication for React single-page applications.**
23
+
24
+ [MonoCloud](https://www.monocloud.com?utm_source=github&utm_medium=auth_js) is a modern, developer-friendly Identity & Access Management platform.
25
+
26
+ This SDK provides **React-friendly authentication** for single-page applications. It implements **OAuth 2.0 / OpenID Connect** with **PKCE**, and surfaces authentication state, sign-in and sign-out flows, and route protection through a React provider, hooks, and components.
27
+
28
+ > This package builds on **`@monocloud/auth-web-js`** and adds React-specific provider, hooks, and components.
29
+
30
+ ## 📘 Documentation
31
+
32
+ - **Documentation:** [https://www.monocloud.com/docs](https://www.monocloud.com/docs?utm_source=github&utm_medium=auth_js)
33
+ - **Quickstart:** [https://www.monocloud.com/docs/quickstarts/react](https://www.monocloud.com/docs/quickstarts/react?utm_source=github&utm_medium=auth_js)
34
+ - **SDK Reference:** [https://www.monocloud.com/docs/sdks/react](https://www.monocloud.com/docs/sdks/react/index?utm_source=github&utm_medium=auth_js)
35
+ - **API Reference:** [https://monocloud.github.io/auth-js](https://monocloud.github.io/auth-js?utm_source=github&utm_medium=auth_js)
36
+
37
+ ## Supported Platforms
38
+
39
+ - **React** v18+
40
+ - **Modern Browsers** (Chrome, Edge, Firefox, Safari)
41
+
42
+ ## 🚀 Getting Started
43
+
44
+ ### Requirements
45
+
46
+ - A **MonoCloud Tenant**
47
+ - A **Client** configured as a Single Page Application (SPA)
48
+ - The application's URL registered in **Callback URLs**, **Sign-out URLs** and **Cross Origin URLs**
49
+
50
+ ## 📦 Installation
51
+
52
+ ```bash
53
+ npm install @monocloud/auth-react
54
+ ```
55
+
56
+ ### Add the Provider
57
+
58
+ Wrap your application with `<MonoCloudAuthProvider>`.
59
+
60
+ ```tsx
61
+ import { MonoCloudAuthProvider } from '@monocloud/auth-react';
62
+
63
+ export default function Root() {
64
+ return (
65
+ <MonoCloudAuthProvider
66
+ tenantDomain="https://<your-tenant-domain>"
67
+ clientId="<your-client-id>"
68
+ >
69
+ <App />
70
+ </MonoCloudAuthProvider>
71
+ );
72
+ }
73
+ ```
74
+
75
+ By default the provider automatically processes the OIDC callback when it mounts.
76
+
77
+ ### Access the Authentication State
78
+
79
+ ```tsx
80
+ import { useAuth } from '@monocloud/auth-react';
81
+
82
+ export default function Home() {
83
+ const { isLoading, isAuthenticated, user } = useAuth();
84
+
85
+ if (isLoading) return <>Loading…</>;
86
+ if (!isAuthenticated) return <>Not signed in</>;
87
+
88
+ return <>Welcome, {user?.email}</>;
89
+ }
90
+ ```
91
+
92
+ `useAuth()` also exposes the actions `signIn`, `signOut`, `signInSilent`, `refreshSession`, `refetchUserInfo`, and `getTokens`. For lower-level operations use the `useMonoCloudClient()` hook to access the underlying `MonoCloudWebJSClient` client.
93
+
94
+ ### Sign-in, Sign-up and Sign-out
95
+
96
+ ```tsx
97
+ import { SignIn, SignUp, SignOut } from '@monocloud/auth-react';
98
+
99
+ <SignIn>Sign In</SignIn>
100
+ <SignUp>Sign Up</SignUp>
101
+ <SignOut>Sign Out</SignOut>
102
+ ```
103
+
104
+ ### Protect Content
105
+
106
+ ```tsx
107
+ import { Protected } from '@monocloud/auth-react';
108
+
109
+ <Protected fallback={<>Sign in to view this.</>}>
110
+ <SecretContent />
111
+ </Protected>
112
+ ```
113
+
114
+ > `<Protected>` only affects what is rendered on the client. Always enforce authorization on your APIs as well.
115
+
116
+ ### Handle the Callback on a Dedicated Route
117
+
118
+ If you use a client-side router and want the callback to land on a dedicated route (with control over navigation and no full-page reload), disable automatic processing and render `<ProcessCallback>` on that route. Navigation is controlled by the standard `postCallback` option on the provider:
119
+
120
+ ```tsx
121
+ import {
122
+ MonoCloudAuthProvider,
123
+ ProcessCallback,
124
+ } from '@monocloud/auth-react';
125
+ import { BrowserRouter, Routes, Route, useNavigate } from 'react-router-dom';
126
+
127
+ function Root() {
128
+ const navigate = useNavigate();
129
+ return (
130
+ <MonoCloudAuthProvider
131
+ tenantDomain="https://<your-tenant-domain>"
132
+ clientId="<your-client-id>"
133
+ callbackPath="/callback"
134
+ autoProcessCallback={false}
135
+ postCallback={state => navigate(state.returnUrl ?? '/')}
136
+ >
137
+ <Routes>
138
+ <Route
139
+ path="/callback"
140
+ element={
141
+ <ProcessCallback
142
+ loading={<p>Completing sign in…</p>}
143
+ error={err => <p>Sign in failed: {err.message}</p>}
144
+ />
145
+ }
146
+ />
147
+ <Route path="/" element={<Home />} />
148
+ </Routes>
149
+ </MonoCloudAuthProvider>
150
+ );
151
+ }
152
+
153
+ export default function App() {
154
+ return (
155
+ <BrowserRouter>
156
+ <Root />
157
+ </BrowserRouter>
158
+ );
159
+ }
160
+ ```
161
+
162
+ ## When should I use `@monocloud/auth-react`?
163
+
164
+ Use **`@monocloud/auth-react`** if you are building a **React single-page application** and want a secure authentication solution with minimal configuration.
165
+
166
+ This package is a good fit if you:
167
+
168
+ - Are building a **React SPA** (Vite, Create React App, and similar)
169
+ - Want **authentication state and sign-in / sign-out actions** accessible anywhere in your component tree
170
+ - Need to **conditionally render or gate UI** based on the user's authentication status and group membership
171
+ - Want **OIDC redirect callbacks handled for you**, with optional client-side-router integration on a dedicated callback route
172
+ - Prefer **framework-native ergonomics** without managing the OAuth / OpenID Connect flow yourself
173
+
174
+ `@monocloud/auth-react` builds on top of **`@monocloud/auth-web-js`**, reusing the same browser implementation (PKCE, redirects, popups, silent refresh, and cross-tab coordination) while providing React-specific ergonomics.
175
+
176
+ ## 🤝 Contributing & Support
177
+
178
+ ### Issues & Feedback
179
+
180
+ - Use **GitHub Issues** for bug reports and feature requests.
181
+ - For tenant or account-specific help, contact MonoCloud Support through your dashboard.
182
+
183
+ ### Security
184
+
185
+ Do **not** report security issues publicly. Please follow the contact instructions at: [https://www.monocloud.com/contact](https://www.monocloud.com/contact?utm_source=github&utm_medium=auth_js)
186
+
187
+ ## 📄 License
188
+
189
+ Licensed under the **MIT License**. See the included [`LICENSE`](https://github.com/monocloud/auth-js/blob/main/LICENSE) file.
@@ -0,0 +1,23 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ exports.__toESM = __toESM;
@@ -0,0 +1,62 @@
1
+ "use client";
2
+ const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
3
+ const require_use_process_callback = require("../use-process-callback.cjs");
4
+ let react = require("react");
5
+ react = require_runtime.__toESM(react);
6
+ //#region src/components/process-callback.tsx
7
+ /**
8
+ * `<ProcessCallback/>` completes a pending sign-in or sign-out callback on the
9
+ * current URL and synchronizes the authentication state.
10
+ *
11
+ * Render it on the dedicated route that MonoCloud redirects back to (for
12
+ * example, `/callback`) and disable automatic processing on the provider with
13
+ * `autoProcessCallback={false}`. It renders no UI of its own beyond the optional
14
+ * `loading`, `error`, and `children` slots. Navigation after a successful
15
+ * callback is controlled by the provider-level `postCallback` option, not by
16
+ * this component.
17
+ *
18
+ * @example Dedicated callback route
19
+ *
20
+ * ```tsx title="Dedicated callback route"
21
+ * "use client";
22
+ *
23
+ * import { ProcessCallback } from "@monocloud/auth-react";
24
+ *
25
+ * export default function Callback() {
26
+ * return (
27
+ * <ProcessCallback
28
+ * loading={<p>Completing sign in…</p>}
29
+ * error={(err) => <p>Sign in failed: {err.message}</p>}
30
+ * />
31
+ * );
32
+ * }
33
+ * ```
34
+ *
35
+ * @param props - Props for customizing the rendered states.
36
+ * @returns The `loading`, `error`, or `children` content depending on the
37
+ * processing state.
38
+ *
39
+ * @category Components
40
+ */
41
+ const ProcessCallback = ({ loading = null, error, children = null }) => {
42
+ const processCallback = require_use_process_callback.useProcessCallback();
43
+ const [state, setState] = (0, react.useState)({ status: "processing" });
44
+ const ran = (0, react.useRef)(false);
45
+ (0, react.useEffect)(() => {
46
+ /* v8 ignore start -- StrictMode double-invocation guard */
47
+ if (ran.current) return;
48
+ /* v8 ignore stop */
49
+ ran.current = true;
50
+ processCallback().then(() => setState({ status: "done" })).catch((e) => setState({
51
+ status: "error",
52
+ error: e
53
+ }));
54
+ }, [processCallback]);
55
+ if (state.status === "processing") return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, loading);
56
+ if (state.status === "error") return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, typeof error === "function" ? error(state.error) : error ?? null);
57
+ return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, children);
58
+ };
59
+ //#endregion
60
+ exports.ProcessCallback = ProcessCallback;
61
+
62
+ //# sourceMappingURL=process-callback.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-callback.cjs","names":["useProcessCallback"],"sources":["../../src/components/process-callback.tsx"],"sourcesContent":["'use client';\n\nimport React, { useEffect, useRef, useState } from 'react';\nimport { useProcessCallback } from '../use-process-callback';\nimport type { ProcessCallbackProps } from '../types';\n\ntype State =\n | { status: 'processing' }\n | { status: 'done' }\n | { status: 'error'; error: Error };\n\n/**\n * `<ProcessCallback/>` completes a pending sign-in or sign-out callback on the\n * current URL and synchronizes the authentication state.\n *\n * Render it on the dedicated route that MonoCloud redirects back to (for\n * example, `/callback`) and disable automatic processing on the provider with\n * `autoProcessCallback={false}`. It renders no UI of its own beyond the optional\n * `loading`, `error`, and `children` slots. Navigation after a successful\n * callback is controlled by the provider-level `postCallback` option, not by\n * this component.\n *\n * @example Dedicated callback route\n *\n * ```tsx title=\"Dedicated callback route\"\n * \"use client\";\n *\n * import { ProcessCallback } from \"@monocloud/auth-react\";\n *\n * export default function Callback() {\n * return (\n * <ProcessCallback\n * loading={<p>Completing sign in…</p>}\n * error={(err) => <p>Sign in failed: {err.message}</p>}\n * />\n * );\n * }\n * ```\n *\n * @param props - Props for customizing the rendered states.\n * @returns The `loading`, `error`, or `children` content depending on the\n * processing state.\n *\n * @category Components\n */\nexport const ProcessCallback = ({\n loading = null,\n error,\n children = null,\n}: ProcessCallbackProps): React.ReactNode => {\n const processCallback = useProcessCallback();\n const [state, setState] = useState<State>({ status: 'processing' });\n const ran = useRef(false);\n\n useEffect(() => {\n /* v8 ignore start -- StrictMode double-invocation guard */\n if (ran.current) {\n return;\n }\n /* v8 ignore stop */\n ran.current = true;\n\n processCallback()\n .then(() => setState({ status: 'done' }))\n .catch((e: Error) => setState({ status: 'error', error: e }));\n }, [processCallback]);\n\n if (state.status === 'processing') {\n return <>{loading}</>;\n }\n\n if (state.status === 'error') {\n return (\n <>{typeof error === 'function' ? error(state.error) : (error ?? null)}</>\n );\n }\n\n return <>{children}</>;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,MAAa,mBAAmB,EAC9B,UAAU,MACV,OACA,WAAW,WACgC;CAC3C,MAAM,kBAAkBA,6BAAAA,mBAAmB;CAC3C,MAAM,CAAC,OAAO,aAAA,GAAA,MAAA,UAA4B,EAAE,QAAQ,aAAa,CAAC;CAClE,MAAM,OAAA,GAAA,MAAA,QAAa,KAAK;CAExB,CAAA,GAAA,MAAA,iBAAgB;;EAEd,IAAI,IAAI,SACN;;EAGF,IAAI,UAAU;EAEd,gBAAgB,EACb,WAAW,SAAS,EAAE,QAAQ,OAAO,CAAC,CAAC,EACvC,OAAO,MAAa,SAAS;GAAE,QAAQ;GAAS,OAAO;EAAE,CAAC,CAAC;CAChE,GAAG,CAAC,eAAe,CAAC;CAEpB,IAAI,MAAM,WAAW,cACnB,OAAO,sBAAA,QAAA,cAAA,MAAA,QAAA,UAAA,MAAG,OAAU;CAGtB,IAAI,MAAM,WAAW,SACnB,OACE,sBAAA,QAAA,cAAA,MAAA,QAAA,UAAA,MAAG,OAAO,UAAU,aAAa,MAAM,MAAM,KAAK,IAAK,SAAS,IAAQ;CAI5E,OAAO,sBAAA,QAAA,cAAA,MAAA,QAAA,UAAA,MAAG,QAAW;AACvB"}
@@ -0,0 +1,46 @@
1
+ import { ProcessCallbackProps } from "../types.mjs";
2
+ import React from "react";
3
+
4
+ //#region src/components/process-callback.d.ts
5
+ /**
6
+ * `<ProcessCallback/>` completes a pending sign-in or sign-out callback on the
7
+ * current URL and synchronizes the authentication state.
8
+ *
9
+ * Render it on the dedicated route that MonoCloud redirects back to (for
10
+ * example, `/callback`) and disable automatic processing on the provider with
11
+ * `autoProcessCallback={false}`. It renders no UI of its own beyond the optional
12
+ * `loading`, `error`, and `children` slots. Navigation after a successful
13
+ * callback is controlled by the provider-level `postCallback` option, not by
14
+ * this component.
15
+ *
16
+ * @example Dedicated callback route
17
+ *
18
+ * ```tsx title="Dedicated callback route"
19
+ * "use client";
20
+ *
21
+ * import { ProcessCallback } from "@monocloud/auth-react";
22
+ *
23
+ * export default function Callback() {
24
+ * return (
25
+ * <ProcessCallback
26
+ * loading={<p>Completing sign in…</p>}
27
+ * error={(err) => <p>Sign in failed: {err.message}</p>}
28
+ * />
29
+ * );
30
+ * }
31
+ * ```
32
+ *
33
+ * @param props - Props for customizing the rendered states.
34
+ * @returns The `loading`, `error`, or `children` content depending on the
35
+ * processing state.
36
+ *
37
+ * @category Components
38
+ */
39
+ declare const ProcessCallback: ({
40
+ loading,
41
+ error,
42
+ children
43
+ }: ProcessCallbackProps) => React.ReactNode;
44
+ //#endregion
45
+ export { ProcessCallback };
46
+ //# sourceMappingURL=process-callback.d.mts.map
@@ -0,0 +1,60 @@
1
+ "use client";
2
+ import { useProcessCallback } from "../use-process-callback.mjs";
3
+ import React, { useEffect, useRef, useState } from "react";
4
+ //#region src/components/process-callback.tsx
5
+ /**
6
+ * `<ProcessCallback/>` completes a pending sign-in or sign-out callback on the
7
+ * current URL and synchronizes the authentication state.
8
+ *
9
+ * Render it on the dedicated route that MonoCloud redirects back to (for
10
+ * example, `/callback`) and disable automatic processing on the provider with
11
+ * `autoProcessCallback={false}`. It renders no UI of its own beyond the optional
12
+ * `loading`, `error`, and `children` slots. Navigation after a successful
13
+ * callback is controlled by the provider-level `postCallback` option, not by
14
+ * this component.
15
+ *
16
+ * @example Dedicated callback route
17
+ *
18
+ * ```tsx title="Dedicated callback route"
19
+ * "use client";
20
+ *
21
+ * import { ProcessCallback } from "@monocloud/auth-react";
22
+ *
23
+ * export default function Callback() {
24
+ * return (
25
+ * <ProcessCallback
26
+ * loading={<p>Completing sign in…</p>}
27
+ * error={(err) => <p>Sign in failed: {err.message}</p>}
28
+ * />
29
+ * );
30
+ * }
31
+ * ```
32
+ *
33
+ * @param props - Props for customizing the rendered states.
34
+ * @returns The `loading`, `error`, or `children` content depending on the
35
+ * processing state.
36
+ *
37
+ * @category Components
38
+ */
39
+ const ProcessCallback = ({ loading = null, error, children = null }) => {
40
+ const processCallback = useProcessCallback();
41
+ const [state, setState] = useState({ status: "processing" });
42
+ const ran = useRef(false);
43
+ useEffect(() => {
44
+ /* v8 ignore start -- StrictMode double-invocation guard */
45
+ if (ran.current) return;
46
+ /* v8 ignore stop */
47
+ ran.current = true;
48
+ processCallback().then(() => setState({ status: "done" })).catch((e) => setState({
49
+ status: "error",
50
+ error: e
51
+ }));
52
+ }, [processCallback]);
53
+ if (state.status === "processing") return /* @__PURE__ */ React.createElement(React.Fragment, null, loading);
54
+ if (state.status === "error") return /* @__PURE__ */ React.createElement(React.Fragment, null, typeof error === "function" ? error(state.error) : error ?? null);
55
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
56
+ };
57
+ //#endregion
58
+ export { ProcessCallback };
59
+
60
+ //# sourceMappingURL=process-callback.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-callback.mjs","names":[],"sources":["../../src/components/process-callback.tsx"],"sourcesContent":["'use client';\n\nimport React, { useEffect, useRef, useState } from 'react';\nimport { useProcessCallback } from '../use-process-callback';\nimport type { ProcessCallbackProps } from '../types';\n\ntype State =\n | { status: 'processing' }\n | { status: 'done' }\n | { status: 'error'; error: Error };\n\n/**\n * `<ProcessCallback/>` completes a pending sign-in or sign-out callback on the\n * current URL and synchronizes the authentication state.\n *\n * Render it on the dedicated route that MonoCloud redirects back to (for\n * example, `/callback`) and disable automatic processing on the provider with\n * `autoProcessCallback={false}`. It renders no UI of its own beyond the optional\n * `loading`, `error`, and `children` slots. Navigation after a successful\n * callback is controlled by the provider-level `postCallback` option, not by\n * this component.\n *\n * @example Dedicated callback route\n *\n * ```tsx title=\"Dedicated callback route\"\n * \"use client\";\n *\n * import { ProcessCallback } from \"@monocloud/auth-react\";\n *\n * export default function Callback() {\n * return (\n * <ProcessCallback\n * loading={<p>Completing sign in…</p>}\n * error={(err) => <p>Sign in failed: {err.message}</p>}\n * />\n * );\n * }\n * ```\n *\n * @param props - Props for customizing the rendered states.\n * @returns The `loading`, `error`, or `children` content depending on the\n * processing state.\n *\n * @category Components\n */\nexport const ProcessCallback = ({\n loading = null,\n error,\n children = null,\n}: ProcessCallbackProps): React.ReactNode => {\n const processCallback = useProcessCallback();\n const [state, setState] = useState<State>({ status: 'processing' });\n const ran = useRef(false);\n\n useEffect(() => {\n /* v8 ignore start -- StrictMode double-invocation guard */\n if (ran.current) {\n return;\n }\n /* v8 ignore stop */\n ran.current = true;\n\n processCallback()\n .then(() => setState({ status: 'done' }))\n .catch((e: Error) => setState({ status: 'error', error: e }));\n }, [processCallback]);\n\n if (state.status === 'processing') {\n return <>{loading}</>;\n }\n\n if (state.status === 'error') {\n return (\n <>{typeof error === 'function' ? error(state.error) : (error ?? null)}</>\n );\n }\n\n return <>{children}</>;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,MAAa,mBAAmB,EAC9B,UAAU,MACV,OACA,WAAW,WACgC;CAC3C,MAAM,kBAAkB,mBAAmB;CAC3C,MAAM,CAAC,OAAO,YAAY,SAAgB,EAAE,QAAQ,aAAa,CAAC;CAClE,MAAM,MAAM,OAAO,KAAK;CAExB,gBAAgB;;EAEd,IAAI,IAAI,SACN;;EAGF,IAAI,UAAU;EAEd,gBAAgB,EACb,WAAW,SAAS,EAAE,QAAQ,OAAO,CAAC,CAAC,EACvC,OAAO,MAAa,SAAS;GAAE,QAAQ;GAAS,OAAO;EAAE,CAAC,CAAC;CAChE,GAAG,CAAC,eAAe,CAAC;CAEpB,IAAI,MAAM,WAAW,cACnB,OAAO,sBAAA,cAAA,MAAA,UAAA,MAAG,OAAU;CAGtB,IAAI,MAAM,WAAW,SACnB,OACE,sBAAA,cAAA,MAAA,UAAA,MAAG,OAAO,UAAU,aAAa,MAAM,MAAM,KAAK,IAAK,SAAS,IAAQ;CAI5E,OAAO,sBAAA,cAAA,MAAA,UAAA,MAAG,QAAW;AACvB"}
@@ -0,0 +1,85 @@
1
+ "use client";
2
+ const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
3
+ const require_use_auth = require("../use-auth.cjs");
4
+ let react = require("react");
5
+ react = require_runtime.__toESM(react);
6
+ let _monocloud_auth_web_js_utils = require("@monocloud/auth-web-js/utils");
7
+ //#region src/components/protected.tsx
8
+ /**
9
+ * `<Protected>` conditionally renders its children based on the user’s authentication state and (optionally) group membership.
10
+ *
11
+ * > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser. Always enforce authorization on your APIs as well.
12
+ *
13
+ * @example Basic Usage
14
+ *
15
+ * ```tsx:src/Home.tsx tab="Basic Usage" tab-group="Protected"
16
+ * "use client";
17
+ *
18
+ * import { Protected } from "@monocloud/auth-react";
19
+ *
20
+ * export default function Home() {
21
+ * return (
22
+ * <Protected fallback={<>Sign in to view the message.</>}>
23
+ * <>This is the protected content.</>
24
+ * </Protected>
25
+ * );
26
+ * }
27
+ * ```
28
+ *
29
+ * @example With Groups
30
+ *
31
+ * ```tsx:src/Home.tsx tab="With Groups" tab-group="Protected"
32
+ * "use client";
33
+ *
34
+ * import { Protected } from "@monocloud/auth-react";
35
+ *
36
+ * export default function Home() {
37
+ * return (
38
+ * <Protected
39
+ * groups={["admin"]}
40
+ * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}
41
+ * >
42
+ * <>Signed in as admin</>
43
+ * </Protected>
44
+ * );
45
+ * }
46
+ * ```
47
+ *
48
+ * @example Requiring all groups
49
+ *
50
+ * ```tsx:src/Home.tsx tab="Requiring all groups" tab-group="Protected"
51
+ * "use client";
52
+ *
53
+ * import { Protected } from "@monocloud/auth-react";
54
+ *
55
+ * export default function Home() {
56
+ * return (
57
+ * <Protected
58
+ * groups={["admin", "billing"]}
59
+ * matchAllGroups
60
+ * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}
61
+ * >
62
+ * <>Sensitive settings</>
63
+ * </Protected>
64
+ * );
65
+ * }
66
+ * ```
67
+ *
68
+ * @param props - Props for customizing the Protected component.
69
+ * @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.
70
+ *
71
+ * @category Components
72
+ */
73
+ const Protected = ({ children, groups, groupsClaim, matchAllGroups = false, fallback = null, onGroupAccessDenied = () => /* @__PURE__ */ react.default.createElement(react.default.Fragment, null) }) => {
74
+ const { isLoading, error, isAuthenticated, user } = require_use_auth.useAuth();
75
+ if (isLoading) return null;
76
+ if (error || !isAuthenticated || !user) {
77
+ if (fallback) return fallback;
78
+ return null;
79
+ }
80
+ return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, !groups || (0, _monocloud_auth_web_js_utils.isUserInGroup)(user, groups, groupsClaim, matchAllGroups) ? children : onGroupAccessDenied(user));
81
+ };
82
+ //#endregion
83
+ exports.Protected = Protected;
84
+
85
+ //# sourceMappingURL=protected.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protected.cjs","names":["useAuth"],"sources":["../../src/components/protected.tsx"],"sourcesContent":["'use client';\n\nimport { isUserInGroup } from '@monocloud/auth-web-js/utils';\nimport React from 'react';\nimport type { MonoCloudUser } from '@monocloud/auth-web-js';\nimport { useAuth } from '../use-auth';\n\n/**\n * Props for the `<Protected />` component.\n *\n * @category Types\n */\nexport interface ProtectedComponentProps {\n /**\n * Content to render when access is allowed.\n */\n children: React.ReactNode;\n\n /**\n * Groups required to view the protected content. By default, the user must belong to **any** of the specified groups.\n */\n groups?: string[];\n\n /**\n * Name of the claim that contains groups in the user profile.\n * @defaultValue 'groups'\n */\n groupsClaim?: string;\n\n /**\n * If `true`, the user must belong to **all** specified `groups` (instead of any).\n * @defaultValue false\n */\n matchAllGroups?: boolean;\n\n /**\n * Content to render when the user is not authenticated.\n */\n fallback?: React.ReactNode;\n\n /**\n * Rendered when the user is authenticated but does not meet the `groups` requirement. If omitted, nothing is rendered (or `fallback` is used only for unauthenticated users).\n */\n onGroupAccessDenied?: (user: MonoCloudUser) => React.ReactNode;\n}\n\n/**\n * `<Protected>` conditionally renders its children based on the user’s authentication state and (optionally) group membership.\n *\n * > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser. Always enforce authorization on your APIs as well.\n *\n * @example Basic Usage\n *\n * ```tsx:src/Home.tsx tab=\"Basic Usage\" tab-group=\"Protected\"\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-react\";\n *\n * export default function Home() {\n * return (\n * <Protected fallback={<>Sign in to view the message.</>}>\n * <>This is the protected content.</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @example With Groups\n *\n * ```tsx:src/Home.tsx tab=\"With Groups\" tab-group=\"Protected\"\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-react\";\n *\n * export default function Home() {\n * return (\n * <Protected\n * groups={[\"admin\"]}\n * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}\n * >\n * <>Signed in as admin</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @example Requiring all groups\n *\n * ```tsx:src/Home.tsx tab=\"Requiring all groups\" tab-group=\"Protected\"\n * \"use client\";\n *\n * import { Protected } from \"@monocloud/auth-react\";\n *\n * export default function Home() {\n * return (\n * <Protected\n * groups={[\"admin\", \"billing\"]}\n * matchAllGroups\n * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}\n * >\n * <>Sensitive settings</>\n * </Protected>\n * );\n * }\n * ```\n *\n * @param props - Props for customizing the Protected component.\n * @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.\n *\n * @category Components\n */\nexport const Protected = ({\n children,\n groups,\n groupsClaim,\n matchAllGroups = false,\n fallback = null,\n onGroupAccessDenied = (): React.ReactNode => <></>,\n}: ProtectedComponentProps): React.ReactNode | null => {\n const { isLoading, error, isAuthenticated, user } = useAuth();\n\n if (isLoading) {\n return null;\n }\n\n if (error || !isAuthenticated || !user) {\n if (fallback) {\n return fallback;\n }\n\n return null;\n }\n\n return (\n <>\n {!groups || isUserInGroup(user, groups, groupsClaim, matchAllGroups)\n ? children\n : onGroupAccessDenied(user)}\n </>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+GA,MAAa,aAAa,EACxB,UACA,QACA,aACA,iBAAiB,OACjB,WAAW,MACX,4BAA6C,sBAAA,QAAA,cAAA,MAAA,QAAA,UAAA,IAAI,QACI;CACrD,MAAM,EAAE,WAAW,OAAO,iBAAiB,SAASA,iBAAAA,QAAQ;CAE5D,IAAI,WACF,OAAO;CAGT,IAAI,SAAS,CAAC,mBAAmB,CAAC,MAAM;EACtC,IAAI,UACF,OAAO;EAGT,OAAO;CACT;CAEA,OACE,sBAAA,QAAA,cAAA,MAAA,QAAA,UAAA,MACG,CAAC,WAAA,GAAA,6BAAA,eAAwB,MAAM,QAAQ,aAAa,cAAc,IAC/D,WACA,oBAAoB,IAAI,CAC5B;AAEN"}
@@ -0,0 +1,113 @@
1
+ import { MonoCloudUser } from "@monocloud/auth-web-js";
2
+ import React from "react";
3
+
4
+ //#region src/components/protected.d.ts
5
+ /**
6
+ * Props for the `<Protected />` component.
7
+ *
8
+ * @category Types
9
+ */
10
+ interface ProtectedComponentProps {
11
+ /**
12
+ * Content to render when access is allowed.
13
+ */
14
+ children: React.ReactNode;
15
+ /**
16
+ * Groups required to view the protected content. By default, the user must belong to **any** of the specified groups.
17
+ */
18
+ groups?: string[];
19
+ /**
20
+ * Name of the claim that contains groups in the user profile.
21
+ * @defaultValue 'groups'
22
+ */
23
+ groupsClaim?: string;
24
+ /**
25
+ * If `true`, the user must belong to **all** specified `groups` (instead of any).
26
+ * @defaultValue false
27
+ */
28
+ matchAllGroups?: boolean;
29
+ /**
30
+ * Content to render when the user is not authenticated.
31
+ */
32
+ fallback?: React.ReactNode;
33
+ /**
34
+ * 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).
35
+ */
36
+ onGroupAccessDenied?: (user: MonoCloudUser) => React.ReactNode;
37
+ }
38
+ /**
39
+ * `<Protected>` conditionally renders its children based on the user’s authentication state and (optionally) group membership.
40
+ *
41
+ * > `<Protected>` runs on the client and only affects what is rendered. It does **not** prevent data from being sent to the browser. Always enforce authorization on your APIs as well.
42
+ *
43
+ * @example Basic Usage
44
+ *
45
+ * ```tsx:src/Home.tsx tab="Basic Usage" tab-group="Protected"
46
+ * "use client";
47
+ *
48
+ * import { Protected } from "@monocloud/auth-react";
49
+ *
50
+ * export default function Home() {
51
+ * return (
52
+ * <Protected fallback={<>Sign in to view the message.</>}>
53
+ * <>This is the protected content.</>
54
+ * </Protected>
55
+ * );
56
+ * }
57
+ * ```
58
+ *
59
+ * @example With Groups
60
+ *
61
+ * ```tsx:src/Home.tsx tab="With Groups" tab-group="Protected"
62
+ * "use client";
63
+ *
64
+ * import { Protected } from "@monocloud/auth-react";
65
+ *
66
+ * export default function Home() {
67
+ * return (
68
+ * <Protected
69
+ * groups={["admin"]}
70
+ * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access admin content.</>}
71
+ * >
72
+ * <>Signed in as admin</>
73
+ * </Protected>
74
+ * );
75
+ * }
76
+ * ```
77
+ *
78
+ * @example Requiring all groups
79
+ *
80
+ * ```tsx:src/Home.tsx tab="Requiring all groups" tab-group="Protected"
81
+ * "use client";
82
+ *
83
+ * import { Protected } from "@monocloud/auth-react";
84
+ *
85
+ * export default function Home() {
86
+ * return (
87
+ * <Protected
88
+ * groups={["admin", "billing"]}
89
+ * matchAllGroups
90
+ * onGroupAccessDenied={(user) => <>User {user.email} is not allowed to access billing content.</>}
91
+ * >
92
+ * <>Sensitive settings</>
93
+ * </Protected>
94
+ * );
95
+ * }
96
+ * ```
97
+ *
98
+ * @param props - Props for customizing the Protected component.
99
+ * @returns The children if authorized, the `fallback` or `onGroupAccessDenied` content if unauthenticated or unauthorized, or `null` while loading.
100
+ *
101
+ * @category Components
102
+ */
103
+ declare const Protected: ({
104
+ children,
105
+ groups,
106
+ groupsClaim,
107
+ matchAllGroups,
108
+ fallback,
109
+ onGroupAccessDenied
110
+ }: ProtectedComponentProps) => React.ReactNode | null;
111
+ //#endregion
112
+ export { Protected, ProtectedComponentProps };
113
+ //# sourceMappingURL=protected.d.mts.map