@ssoeasy-dev/react 1.0.0-beta.0abbf06

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MoreWiktor
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.
22
+
package/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # @ssoeasy-dev/react
2
+
3
+ React-адаптер для SSO Easy. Оборачивает `@ssoeasy-dev/core` в React Context и предоставляет хук `useAuth` и компонент `ProtectedRoute`.
4
+
5
+ ## Установка
6
+
7
+ ```bash
8
+ npm install @ssoeasy-dev/react @ssoeasy-dev/core
9
+ ```
10
+
11
+ ## Быстрый старт
12
+
13
+ ### 1. Оборачиваем приложение в `AuthProvider`
14
+
15
+ ```tsx
16
+ import { AuthProvider } from "@ssoeasy-dev/react";
17
+ import { AuthManager } from "@ssoeasy-dev/core";
18
+
19
+ const auth = new AuthManager({
20
+ serviceId: "your-service-id",
21
+ redirectUri: "https://yourapp.com",
22
+ loginPath: "/callback",
23
+ authPageUrl: "https://sso.example.com/login",
24
+ authServerConfig: {
25
+ baseURL: "https://api.example.com",
26
+ },
27
+ });
28
+
29
+ function App() {
30
+ return (
31
+ <AuthProvider auth={auth}>
32
+ <AppRouter />
33
+ </AuthProvider>
34
+ );
35
+ }
36
+ ```
37
+
38
+ ### 2. Защищаем роуты через `ProtectedRoute`
39
+
40
+ ```tsx
41
+ import { BrowserRouter, Route, Routes } from "react-router-dom";
42
+ import { ProtectedRoute } from "@ssoeasy-dev/react";
43
+
44
+ function AppRouter() {
45
+ return (
46
+ <BrowserRouter>
47
+ <Routes>
48
+ <Route path="/callback" element={<CallbackPage />} />
49
+ <Route path="/" element={<PublicPage />} />
50
+ <Route
51
+ path="/dashboard"
52
+ element={
53
+ <ProtectedRoute redirectTo="/dashboard">
54
+ <Dashboard />
55
+ </ProtectedRoute>
56
+ }
57
+ />
58
+ </Routes>
59
+ </BrowserRouter>
60
+ );
61
+ }
62
+ ```
63
+
64
+ ### 3. Используем `useAuth` в компонентах
65
+
66
+ ```tsx
67
+ import { useAuth } from "@ssoeasy-dev/react";
68
+
69
+ function Header() {
70
+ const auth = useAuth();
71
+
72
+ return (
73
+ <header>
74
+ {auth.getState().isAuthenticated ? (
75
+ <button onClick={() => auth.logout()}>Выйти</button>
76
+ ) : (
77
+ <button onClick={() => auth.login()}>Войти</button>
78
+ )}
79
+ </header>
80
+ );
81
+ }
82
+ ```
83
+
84
+ ### 4. Страница callback
85
+
86
+ ```tsx
87
+ import { useEffect } from "react";
88
+ import { useAuth } from "@ssoeasy-dev/react";
89
+ import { useNavigate } from "react-router-dom";
90
+
91
+ function CallbackPage() {
92
+ const auth = useAuth();
93
+ const navigate = useNavigate();
94
+
95
+ useEffect(() => {
96
+ auth.handleRedirectCallback().then(({ redirectTo }) => {
97
+ navigate(redirectTo, { replace: true });
98
+ });
99
+ }, []);
100
+
101
+ return <div>Авторизация...</div>;
102
+ }
103
+ ```
104
+
105
+ ## API
106
+
107
+ ### `AuthProvider`
108
+
109
+ ```tsx
110
+ <AuthProvider auth={AuthManager}>{children}</AuthProvider>
111
+ ```
112
+
113
+ Предоставляет `AuthManager` через React Context. Должен оборачивать всё приложение.
114
+
115
+ ### `useAuth`
116
+
117
+ ```tsx
118
+ const auth = useAuth(); // возвращает AuthManager
119
+ ```
120
+
121
+ Хук для доступа к `AuthManager` внутри компонентов. Выбрасывает ошибку если вызван вне `AuthProvider`.
122
+
123
+ ### `ProtectedRoute`
124
+
125
+ ```tsx
126
+ <ProtectedRoute
127
+ redirectTo="/target-path" // путь для redirectTo при логине (опционально)
128
+ fallback={<Spinner />} // отображается во время checkAuth (по умолчанию <div>Loading...</div>)
129
+ unauthenticatedElement={<Node />} // рендерится вместо редиректа на SSO, если пользователь не авторизован (опционально)
130
+ >
131
+ <ProtectedContent />
132
+ </ProtectedRoute>
133
+ ```
134
+
135
+ При монтировании вызывает `auth.checkAuth()`. Пока идёт проверка — рендерит `fallback`.
136
+
137
+ Если пользователь не авторизован:
138
+
139
+ - **`unauthenticatedElement` не передан** — вызывает `auth.login({ redirectTo })`, редирект на SSO (поведение по умолчанию)
140
+ - **`unauthenticatedElement` передан** — рендерит переданный элемент без редиректа
141
+
142
+ #### Пример: внутренняя страница выбора входа/регистрации
143
+
144
+ Если в приложении есть собственная страница `/auth` с кнопками «Войти» и «Зарегистрироваться», можно передать `<Navigate>` вместо того, чтобы сразу уходить на SSO:
145
+
146
+ ```tsx
147
+ import { Navigate } from "react-router-dom";
148
+ import { ProtectedRoute } from "@ssoeasy-dev/react";
149
+
150
+ const LayoutWrapper = () => {
151
+ return (
152
+ <ProtectedRoute unauthenticatedElement={<Navigate to="/auth" replace />}>
153
+ <Layout>
154
+ <Outlet />
155
+ </Layout>
156
+ </ProtectedRoute>
157
+ );
158
+ };
159
+ ```
160
+
161
+ Страница `/auth` при этом остаётся публичной — она не обёрнута в `ProtectedRoute`.
162
+
163
+ ## Экспорты
164
+
165
+ ```typescript
166
+ export { AuthProvider } from "./AuthProvider";
167
+ export { ProtectedRoute } from "./ProtectedRoute";
168
+ export { useAuth } from "./useAuth";
169
+ ```
170
+
171
+ ## Лицензия
172
+
173
+ MIT — см. [LICENSE](../LICENSE).
174
+
175
+ ## Контакты
176
+
177
+ - Email: morewiktor@yandex.ru
178
+ - Telegram: [@MoreWiktor](https://t.me/MoreWiktor)
179
+ - GitHub: [@MoreWiktor](https://github.com/MoreWiktor)
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ import { AuthConfig } from "@ssoeasy-dev/core";
3
+ export declare const AuthProvider: React.FC<{
4
+ config: AuthConfig;
5
+ children: React.ReactNode;
6
+ }>;
7
+ //# sourceMappingURL=AuthProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../src/AuthProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkB,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,UAAU,EAA0B,MAAM,mBAAmB,CAAC;AAIvE,eAAO,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC;IAClC,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B,CAaA,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useMemo } from "react";
3
+ import { AuthManager } from "@ssoeasy-dev/core";
4
+ import { useEffect, useState } from "react";
5
+ import { AuthContext } from "./context";
6
+ export const AuthProvider = ({ config, children }) => {
7
+ const auth = useMemo(() => new AuthManager(config), [config.serviceId, config.redirectUri]);
8
+ const [_, setState] = useState(auth.getState());
9
+ useEffect(() => {
10
+ const unsubscribe = auth.onStateChange(setState);
11
+ return unsubscribe;
12
+ }, [auth]);
13
+ return _jsx(AuthContext.Provider, { value: auth, children: children });
14
+ };
15
+ //# sourceMappingURL=AuthProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthProvider.js","sourceRoot":"","sources":["../src/AuthProvider.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,EAAc,WAAW,EAAa,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,CAAC,MAAM,YAAY,GAGpB,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC5B,MAAM,IAAI,GAAG,OAAO,CAClB,GAAG,EAAE,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,EAC7B,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,CAAC,CACvC,CAAC;IACF,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAY,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;IAE3D,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,WAAW,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QACjD,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,OAAO,KAAC,WAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,IAAI,YAAG,QAAQ,GAAwB,CAAC;AAC9E,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import React from "react";
2
+ export declare const ProtectedRoute: React.FC<{
3
+ children: React.ReactNode;
4
+ redirectTo?: string;
5
+ fallback?: React.ReactNode;
6
+ unauthenticatedElement?: React.ReactNode;
7
+ }>;
8
+ //# sourceMappingURL=ProtectedRoute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProtectedRoute.d.ts","sourceRoot":"","sources":["../src/ProtectedRoute.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA8B,MAAM,OAAO,CAAC;AAGnD,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC;IACpC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,sBAAsB,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC1C,CA0BA,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { useEffect, useState } from "react";
3
+ import { useAuth } from "./useAuth";
4
+ export const ProtectedRoute = ({ children, redirectTo, fallback = _jsx("div", { children: "Loading..." }), unauthenticatedElement, }) => {
5
+ const auth = useAuth();
6
+ const [isAuthorized, setIsAuthorized] = useState(null);
7
+ useEffect(() => {
8
+ auth.checkAuth().then((val) => {
9
+ setIsAuthorized(val);
10
+ });
11
+ }, [auth]);
12
+ if (isAuthorized === null)
13
+ return fallback;
14
+ if (!isAuthorized) {
15
+ if (unauthenticatedElement !== undefined) {
16
+ return _jsx(_Fragment, { children: unauthenticatedElement });
17
+ }
18
+ auth.login({ redirectTo });
19
+ return null;
20
+ }
21
+ return _jsx(_Fragment, { children: children });
22
+ };
23
+ //# sourceMappingURL=ProtectedRoute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProtectedRoute.js","sourceRoot":"","sources":["../src/ProtectedRoute.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,MAAM,CAAC,MAAM,cAAc,GAKtB,CAAC,EACJ,QAAQ,EACR,UAAU,EACV,QAAQ,GAAG,uCAAqB,EAChC,sBAAsB,GACvB,EAAE,EAAE;IACH,MAAM,IAAI,GAAG,OAAO,EAAE,CAAC;IACvB,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,QAAQ,CAAiB,IAAI,CAAC,CAAC;IAEvE,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;YAC5B,eAAe,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,IAAI,YAAY,KAAK,IAAI;QAAE,OAAO,QAAQ,CAAC;IAE3C,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,IAAI,sBAAsB,KAAK,SAAS,EAAE,CAAC;YACzC,OAAO,4BAAG,sBAAsB,GAAI,CAAC;QACvC,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,4BAAG,QAAQ,GAAI,CAAC;AACzB,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { AuthManager } from "@ssoeasy-dev/core";
2
+ export declare const AuthContext: import("react").Context<AuthManager | null>;
3
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,eAAO,MAAM,WAAW,6CAA0C,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { createContext } from "react";
2
+ export const AuthContext = createContext(null);
3
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAEtC,MAAM,CAAC,MAAM,WAAW,GAAG,aAAa,CAAqB,IAAI,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./AuthProvider";
2
+ export * from "./ProtectedRoute";
3
+ export * from "./useAuth";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./AuthProvider";
2
+ export * from "./ProtectedRoute";
3
+ export * from "./useAuth";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,WAAW,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const useAuth: () => import("@ssoeasy-dev/core").AuthManager;
2
+ //# sourceMappingURL=useAuth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,OAAO,+CAInB,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { useContext } from "react";
2
+ import { AuthContext } from "./context";
3
+ export const useAuth = () => {
4
+ const auth = useContext(AuthContext);
5
+ if (!auth)
6
+ throw new Error("useAuth must be used within AuthProvider");
7
+ return auth;
8
+ };
9
+ //# sourceMappingURL=useAuth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,EAAE;IAC1B,MAAM,IAAI,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IACrC,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IACvE,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@ssoeasy-dev/react",
3
+ "version": "1.0.0-beta.0abbf06",
4
+ "description": "",
5
+ "main": "index.ts",
6
+ "types": "index.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "dependencies": {
19
+ "@ssoeasy-dev/core": "1.0.0-beta.806f697"
20
+ },
21
+ "peerDependencies": {
22
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/react": "^19.2.14",
26
+ "typescript": "^5.0.0"
27
+ },
28
+ "author": "MoreWiktor <MoreWiktor@yandex.ru>",
29
+ "license": "MIT",
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "scripts": {
34
+ "build": "tsc"
35
+ }
36
+ }