@shane_donnelly/dsi-internal-react-utils 0.1.0 → 0.1.1

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 CHANGED
@@ -10,8 +10,6 @@ npm install @shane_donnelly/dsi-internal-react-utils keycloak-js
10
10
 
11
11
  ### Prérequis
12
12
 
13
- - React >= 19
14
- - keycloak-js >= 25
15
13
  - Un serveur Keycloak configuré avec un realm, un client, et au moins un Identity Provider
16
14
 
17
15
  > **Recommandé** : utiliser [react-router](https://reactrouter.com/) pour éviter de perdre l'état de la page lors des redirections d'authentification.
@@ -159,3 +157,47 @@ function AdvancedComponent() {
159
157
  console.log(keycloak?.tokenParsed);
160
158
  }
161
159
  ```
160
+
161
+ ### Fonctions utilitaires standalone
162
+
163
+ Deux fonctions sont disponibles pour manipuler l'instance Keycloak en dehors de React (intercepteurs HTTP, services, etc.) :
164
+
165
+ ```tsx
166
+ import { useAuth, logoutKeycloak, refreshTokenKeycloak } from '@shane_donnelly/dsi-internal-react-utils';
167
+
168
+ // Récupérer l'instance keycloak via useAuth()
169
+ const { keycloak } = useAuth();
170
+
171
+ // Rafraîchir le token (minValidity en secondes, défaut: 30)
172
+ const refreshed = await refreshTokenKeycloak(keycloak!, 60);
173
+ const freshToken = keycloak!.token;
174
+
175
+ // Déconnecter l'utilisateur (redirectUri optionnel, défaut: window.location.origin)
176
+ await logoutKeycloak(keycloak!);
177
+ ```
178
+
179
+ Exemple dans un intercepteur Axios :
180
+
181
+ ```ts
182
+ import axios from 'axios';
183
+ import { logoutKeycloak, refreshTokenKeycloak } from '@shane_donnelly/dsi-internal-react-utils';
184
+ import type Keycloak from 'keycloak-js';
185
+
186
+ function setupInterceptors(keycloak: Keycloak) {
187
+ axios.interceptors.request.use(async (config) => {
188
+ await refreshTokenKeycloak(keycloak, 60);
189
+ config.headers.Authorization = `Bearer ${keycloak.token}`;
190
+ return config;
191
+ });
192
+
193
+ axios.interceptors.response.use(
194
+ (response) => response,
195
+ async (error) => {
196
+ if (error.response?.status === 401) {
197
+ await logoutKeycloak(keycloak);
198
+ }
199
+ return Promise.reject(error);
200
+ },
201
+ );
202
+ }
203
+ ```
@@ -20,3 +20,41 @@ export declare function initKeycloak(keycloak: Keycloak): Promise<boolean>;
20
20
  * @returns Informations utilisateur ou `null`
21
21
  */
22
22
  export declare function parseUser(keycloak: Keycloak): AuthUser | null;
23
+ /**
24
+ * Déconnecte l'utilisateur via l'instance Keycloak.
25
+ *
26
+ * Utilisable en dehors de React (ex: intercepteur HTTP, service, guard).
27
+ *
28
+ * @param keycloak - Instance Keycloak (récupérable via `useAuth().keycloak`)
29
+ * @param redirectUri - URL de redirection après déconnexion (défaut: `window.location.origin`)
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * import { logoutKeycloak } from '@shane_donnelly/dsi-internal-react-utils';
34
+ *
35
+ * // dans un intercepteur axios
36
+ * if (response.status === 401) {
37
+ * await logoutKeycloak(keycloakInstance);
38
+ * }
39
+ * ```
40
+ */
41
+ export declare function logoutKeycloak(keycloak: Keycloak, redirectUri?: string): Promise<void>;
42
+ /**
43
+ * Rafraîchit le token d'accès via l'instance Keycloak.
44
+ *
45
+ * Utilisable en dehors de React (ex: intercepteur HTTP, service).
46
+ *
47
+ * @param keycloak - Instance Keycloak (récupérable via `useAuth().keycloak`)
48
+ * @param minValidity - Durée minimale de validité restante en secondes (défaut: 30)
49
+ * @returns `true` si le token a été rafraîchi, `false` sinon
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * import { refreshTokenKeycloak } from '@shane_donnelly/dsi-internal-react-utils';
54
+ *
55
+ * // avant un appel API
56
+ * await refreshTokenKeycloak(keycloakInstance, 60);
57
+ * const token = keycloakInstance.token;
58
+ * ```
59
+ */
60
+ export declare function refreshTokenKeycloak(keycloak: Keycloak, minValidity?: number): Promise<boolean>;
@@ -21,5 +21,15 @@ async function n(e) {
21
21
  function r(e) {
22
22
  return e.tokenParsed ? e.tokenParsed : null;
23
23
  }
24
+ async function i(e, t) {
25
+ await e.logout({ redirectUri: t ?? window.location.origin });
26
+ }
27
+ async function a(e, t = 30) {
28
+ try {
29
+ return await e.updateToken(t);
30
+ } catch (e) {
31
+ return console.warn("[dsi-keycloak] Token refresh failed:", e), !1;
32
+ }
33
+ }
24
34
  //#endregion
25
- export { t as createKeycloakInstance, n as initKeycloak, r as parseUser };
35
+ export { t as createKeycloakInstance, n as initKeycloak, i as logoutKeycloak, r as parseUser, a as refreshTokenKeycloak };
@@ -3,4 +3,5 @@ export type { KeycloakProviderProps } from './react/KeycloakProvider';
3
3
  export { ProtectedRoute } from './react/ProtectedRoute';
4
4
  export type { ProtectedRouteProps } from './react/ProtectedRoute';
5
5
  export { useAuth } from './react/hooks/useAuth';
6
+ export { logoutKeycloak, refreshTokenKeycloak } from './core/client';
6
7
  export type { KeycloakConfig, KeycloakAuthOptions, AuthStatus, AuthUser, AuthContextValue, } from './core/types';
@@ -1,4 +1,5 @@
1
- import { KeycloakProvider as e } from "./react/KeycloakProvider/index.js";
2
- import { useAuth as t } from "./react/hooks/useAuth.js";
3
- import { t as n } from "../ProtectedRoute-Bl0cCdQ2.js";
4
- export { e as KeycloakProvider, n as ProtectedRoute, t as useAuth };
1
+ import { logoutKeycloak as e, refreshTokenKeycloak as t } from "./core/client.js";
2
+ import { KeycloakProvider as n } from "./react/KeycloakProvider/index.js";
3
+ import { useAuth as r } from "./react/hooks/useAuth.js";
4
+ import { t as i } from "../ProtectedRoute-Bl0cCdQ2.js";
5
+ export { n as KeycloakProvider, i as ProtectedRoute, e as logoutKeycloak, t as refreshTokenKeycloak, r as useAuth };
package/dist/main.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { KeycloakProvider, ProtectedRoute, useAuth, } from './keycloak';
1
+ export { KeycloakProvider, ProtectedRoute, useAuth, logoutKeycloak, refreshTokenKeycloak, } from './keycloak';
2
2
  export type { KeycloakProviderProps, ProtectedRouteProps, KeycloakConfig, KeycloakAuthOptions, AuthStatus, AuthUser, AuthContextValue, } from './keycloak';
package/dist/main.js CHANGED
@@ -1,5 +1,6 @@
1
- import { KeycloakProvider as e } from "./keycloak/react/KeycloakProvider/index.js";
2
- import { useAuth as t } from "./keycloak/react/hooks/useAuth.js";
3
- import { t as n } from "./ProtectedRoute-Bl0cCdQ2.js";
1
+ import { logoutKeycloak as e, refreshTokenKeycloak as t } from "./keycloak/core/client.js";
2
+ import { KeycloakProvider as n } from "./keycloak/react/KeycloakProvider/index.js";
3
+ import { useAuth as r } from "./keycloak/react/hooks/useAuth.js";
4
+ import { t as i } from "./ProtectedRoute-Bl0cCdQ2.js";
4
5
  import "./keycloak/index.js";
5
- export { e as KeycloakProvider, n as ProtectedRoute, t as useAuth };
6
+ export { n as KeycloakProvider, i as ProtectedRoute, e as logoutKeycloak, t as refreshTokenKeycloak, r as useAuth };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shane_donnelly/dsi-internal-react-utils",
3
3
  "private": false,
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",