antaeus.keycloak.react 2.1.2 → 2.2.0
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 +210 -2
- package/dist/index.d.mts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +14 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useState, useRef, useCallback, useEffect, useMemo } from 'react';
|
|
2
2
|
import { useAuth as useAuth$1, AuthProvider } from 'react-oidc-context';
|
|
3
|
+
import { WebStorageStateStore, User } from 'oidc-client-ts';
|
|
3
4
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
4
5
|
import { useLocation } from 'react-router-dom';
|
|
5
6
|
import { QRCodeSVG } from 'qrcode.react';
|
|
6
|
-
import { User } from 'oidc-client-ts';
|
|
7
7
|
|
|
8
8
|
// src/components/KeycloakProvider.tsx
|
|
9
9
|
|
|
@@ -33,7 +33,8 @@ var defaultConfig = {
|
|
|
33
33
|
responseType: "code",
|
|
34
34
|
codeChallengeMethod: "S256",
|
|
35
35
|
loadUserInfo: true,
|
|
36
|
-
automaticSilentSignIn: false
|
|
36
|
+
automaticSilentSignIn: false,
|
|
37
|
+
storageType: "session"
|
|
37
38
|
},
|
|
38
39
|
events: {}
|
|
39
40
|
};
|
|
@@ -50,6 +51,8 @@ var KeycloakConfigBuilder = class {
|
|
|
50
51
|
static build(userConfig) {
|
|
51
52
|
this.validate(userConfig);
|
|
52
53
|
const config = this.mergeWithDefaults(userConfig);
|
|
54
|
+
const storageType = config.advanced.storageType || "session";
|
|
55
|
+
const storage = storageType === "local" ? window.localStorage : window.sessionStorage;
|
|
53
56
|
const oidcSettings = {
|
|
54
57
|
authority: config.authority,
|
|
55
58
|
client_id: config.clientId,
|
|
@@ -60,6 +63,8 @@ var KeycloakConfigBuilder = class {
|
|
|
60
63
|
automaticSilentRenew: config.silentRenew.enabled,
|
|
61
64
|
loadUserInfo: config.advanced.loadUserInfo,
|
|
62
65
|
silent_redirect_uri: config.silentRenew.silentRedirectUri,
|
|
66
|
+
// Configure storage for user data and tokens
|
|
67
|
+
userStore: new WebStorageStateStore({ store: storage }),
|
|
63
68
|
// Add PKCE support (required for public clients)
|
|
64
69
|
...config.advanced.codeChallengeMethod && {
|
|
65
70
|
response_mode: "query"
|
|
@@ -1482,6 +1487,7 @@ var DeviceLogin = ({
|
|
|
1482
1487
|
function useProtectedFetch(options = {}) {
|
|
1483
1488
|
const auth = useAuth();
|
|
1484
1489
|
const refreshInProgressRef = useRef(false);
|
|
1490
|
+
const { user, isLoading, signinSilent } = auth;
|
|
1485
1491
|
const {
|
|
1486
1492
|
baseUrl = "",
|
|
1487
1493
|
onUnauthorized,
|
|
@@ -1500,11 +1506,11 @@ function useProtectedFetch(options = {}) {
|
|
|
1500
1506
|
[baseUrl]
|
|
1501
1507
|
);
|
|
1502
1508
|
const getAuthHeader = () => {
|
|
1503
|
-
if (!
|
|
1509
|
+
if (!user?.access_token) {
|
|
1504
1510
|
return {};
|
|
1505
1511
|
}
|
|
1506
1512
|
return {
|
|
1507
|
-
Authorization: `Bearer ${
|
|
1513
|
+
Authorization: `Bearer ${user.access_token}`
|
|
1508
1514
|
};
|
|
1509
1515
|
};
|
|
1510
1516
|
const buildHeaders = useCallback(
|
|
@@ -1531,10 +1537,10 @@ function useProtectedFetch(options = {}) {
|
|
|
1531
1537
|
});
|
|
1532
1538
|
if (response.status === 401) {
|
|
1533
1539
|
onUnauthorized?.();
|
|
1534
|
-
if (retryOn401 &&
|
|
1540
|
+
if (retryOn401 && user && !refreshInProgressRef.current) {
|
|
1535
1541
|
refreshInProgressRef.current = true;
|
|
1536
1542
|
try {
|
|
1537
|
-
await
|
|
1543
|
+
await signinSilent();
|
|
1538
1544
|
const newHeaders = buildHeaders(init?.headers);
|
|
1539
1545
|
const retryResponse = await fetch(fullUrl, {
|
|
1540
1546
|
...init,
|
|
@@ -1552,7 +1558,7 @@ function useProtectedFetch(options = {}) {
|
|
|
1552
1558
|
}
|
|
1553
1559
|
return response;
|
|
1554
1560
|
},
|
|
1555
|
-
[buildUrl, buildHeaders, onUnauthorized, retryOn401,
|
|
1561
|
+
[buildUrl, buildHeaders, onUnauthorized, retryOn401, user, signinSilent]
|
|
1556
1562
|
);
|
|
1557
1563
|
const fetchJson = useCallback(
|
|
1558
1564
|
async (url, init) => {
|
|
@@ -1571,7 +1577,7 @@ function useProtectedFetch(options = {}) {
|
|
|
1571
1577
|
return {
|
|
1572
1578
|
fetchJson,
|
|
1573
1579
|
fetch: protectedFetch,
|
|
1574
|
-
isLoading
|
|
1580
|
+
isLoading
|
|
1575
1581
|
};
|
|
1576
1582
|
}
|
|
1577
1583
|
|