@shane_donnelly/dsi-internal-react-utils 0.2.0 → 0.2.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.
|
@@ -4,70 +4,80 @@ import { jsx as c } from "react/jsx-runtime";
|
|
|
4
4
|
//#region lib/keycloak/react/KeycloakProvider/index.tsx
|
|
5
5
|
var l = r(null);
|
|
6
6
|
function u({ config: r, children: u, idpHint: d, refreshInterval: f = 300, minTokenValidity: p = 30, onAuthError: m }) {
|
|
7
|
-
let [h, g] = s("loading"), [_, v] = s(null), [y, b] = s(null), x = o(null), S = o(!1), C =
|
|
7
|
+
let [h, g] = s("loading"), [_, v] = s(null), [y, b] = s(null), x = o(null), S = o(!1), C = "__dsi_kc_idp_hint__", w = () => {
|
|
8
|
+
try {
|
|
9
|
+
return sessionStorage.getItem(C) ?? void 0;
|
|
10
|
+
} catch {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
}, T = (e) => {
|
|
14
|
+
try {
|
|
15
|
+
e ? sessionStorage.setItem(C, e) : sessionStorage.removeItem(C);
|
|
16
|
+
} catch {}
|
|
17
|
+
}, E = i((e) => {
|
|
8
18
|
e.authenticated && e.token ? (v(e.token), b(n(e)), g("authenticated")) : (v(null), b(null), g("unauthenticated"));
|
|
9
|
-
}, []),
|
|
10
|
-
m === "logout" ? await x.current?.logout({ redirectUri: window.location.origin }) : typeof m == "function" ? m(e) : await x.current?.login({ idpHint:
|
|
11
|
-
}, [m, d]),
|
|
19
|
+
}, []), D = i(async (e) => {
|
|
20
|
+
m === "logout" ? await x.current?.logout({ redirectUri: window.location.origin }) : typeof m == "function" ? m(e) : await x.current?.login({ idpHint: w() ?? d });
|
|
21
|
+
}, [m, d]), O = i(async () => {
|
|
12
22
|
let e = x.current;
|
|
13
23
|
if (!e) return !1;
|
|
14
24
|
try {
|
|
15
|
-
return await e.updateToken(p) &&
|
|
25
|
+
return await e.updateToken(p) && E(e), !0;
|
|
16
26
|
} catch (e) {
|
|
17
|
-
return console.warn("[dsi-keycloak] Token refresh failed:", e), await
|
|
27
|
+
return console.warn("[dsi-keycloak] Token refresh failed:", e), await D(e), !1;
|
|
18
28
|
}
|
|
19
29
|
}, [
|
|
20
30
|
p,
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
E,
|
|
32
|
+
D
|
|
23
33
|
]);
|
|
24
34
|
a(() => {
|
|
25
35
|
if (S.current) return;
|
|
26
36
|
S.current = !0;
|
|
27
37
|
let n = e(r);
|
|
28
38
|
x.current = n, t(n).then((e) => {
|
|
29
|
-
|
|
39
|
+
E(n), e || g("unauthenticated");
|
|
30
40
|
}).catch((e) => {
|
|
31
41
|
console.error("[dsi-keycloak] Init error:", e), g("error");
|
|
32
42
|
});
|
|
33
43
|
}, []), a(() => {
|
|
34
44
|
if (h !== "authenticated") return;
|
|
35
45
|
let e = window.setInterval(() => {
|
|
36
|
-
|
|
46
|
+
O();
|
|
37
47
|
}, f * 1e3), t = () => {
|
|
38
|
-
document.visibilityState === "visible" &&
|
|
39
|
-
}, n = () =>
|
|
48
|
+
document.visibilityState === "visible" && O();
|
|
49
|
+
}, n = () => O(), r = () => O();
|
|
40
50
|
return document.addEventListener("visibilitychange", t), window.addEventListener("focus", n), window.addEventListener("online", r), () => {
|
|
41
51
|
clearInterval(e), document.removeEventListener("visibilitychange", t), window.removeEventListener("focus", n), window.removeEventListener("online", r);
|
|
42
52
|
};
|
|
43
53
|
}, [
|
|
44
54
|
h,
|
|
45
55
|
f,
|
|
46
|
-
|
|
56
|
+
O
|
|
47
57
|
]);
|
|
48
|
-
let
|
|
58
|
+
let k = i(async (e) => {
|
|
49
59
|
let t = x.current;
|
|
50
60
|
if (!t) return;
|
|
51
61
|
let n = e?.idpHint ?? d;
|
|
52
|
-
|
|
62
|
+
T(n), await t.login({
|
|
53
63
|
idpHint: n,
|
|
54
64
|
redirectUri: e?.redirectUri
|
|
55
65
|
});
|
|
56
|
-
}, [d]),
|
|
66
|
+
}, [d]), A = i(async (e) => {
|
|
57
67
|
let t = x.current;
|
|
58
68
|
t && await t.logout({ redirectUri: e?.redirectUri ?? window.location.origin });
|
|
59
|
-
}, []),
|
|
69
|
+
}, []), j = {
|
|
60
70
|
status: h,
|
|
61
71
|
token: _,
|
|
62
72
|
user: y,
|
|
63
73
|
isAuthenticated: h === "authenticated",
|
|
64
74
|
keycloak: x.current,
|
|
65
|
-
login:
|
|
66
|
-
logout:
|
|
67
|
-
refreshToken:
|
|
75
|
+
login: k,
|
|
76
|
+
logout: A,
|
|
77
|
+
refreshToken: O
|
|
68
78
|
};
|
|
69
79
|
return /* @__PURE__ */ c(l.Provider, {
|
|
70
|
-
value:
|
|
80
|
+
value: j,
|
|
71
81
|
children: u
|
|
72
82
|
});
|
|
73
83
|
}
|