@ssoeasy-dev/react 1.0.0-beta.64cfe3a → 1.0.0-beta.f91a3ba

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 (2) hide show
  1. package/README.md +152 -0
  2. package/package.json +4 -4
package/README.md ADDED
@@ -0,0 +1,152 @@
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
+ >
130
+ <ProtectedContent />
131
+ </ProtectedRoute>
132
+ ```
133
+
134
+ При монтировании вызывает `auth.checkAuth()`. Если не авторизован — вызывает `auth.login({ redirectTo })`. Пока идёт проверка — рендерит `fallback`.
135
+
136
+ ## Экспорты
137
+
138
+ ```typescript
139
+ export { AuthProvider } from "./AuthProvider";
140
+ export { ProtectedRoute } from "./ProtectedRoute";
141
+ export { useAuth } from "./useAuth";
142
+ ```
143
+
144
+ ## Лицензия
145
+
146
+ MIT — см. [LICENSE](../LICENSE).
147
+
148
+ ## Контакты
149
+
150
+ - Email: morewiktor@yandex.ru
151
+ - Telegram: [@MoreWiktor](https://t.me/MoreWiktor)
152
+ - GitHub: [@MoreWiktor](https://github.com/MoreWiktor)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssoeasy-dev/react",
3
- "version": "1.0.0-beta.64cfe3a",
3
+ "version": "1.0.0-beta.f91a3ba",
4
4
  "description": "",
5
5
  "main": "index.ts",
6
6
  "types": "index.ts",
@@ -15,9 +15,9 @@
15
15
  "README.md",
16
16
  "LICENSE"
17
17
  ],
18
- "dependencies": {
19
- "@ssoeasy-dev/core": "1.0.0-beta.2bdd988",
20
- "react": "^19.2.4"
18
+ "peerDependencies": {
19
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
20
+ "@ssoeasy-dev/core": "1.0.0-beta.2bdd988"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/react": "^19.2.14",