@axa-fr/react-oidc 7.26.5 → 7.26.7

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
@@ -120,6 +120,13 @@ trustedDomains.config_with_dpop = {
120
120
 
121
121
  // Setting allowMultiTabLogin to true will enable storing login-specific parameters (state, nonce, code verifier)
122
122
  // separately for each tab. This will prevent errors when logins are initiated from multiple tabs.
123
+ // IMPORTANT: When allowMultiTabLogin is true, you MUST use the OIDC fetch provided by useOidcFetch()
124
+ // or withOidcFetch() for API requests. The service worker embeds a tab-specific token placeholder
125
+ // in the Authorization header, which it then replaces with the real access token.
126
+ // Using a plain fetch or axios without the OIDC fetch wrapper will result in requests being sent
127
+ // without an Authorization header (401 errors), because the service worker cannot determine which
128
+ // tab's token to inject without the placeholder.
129
+ // Example with axios: configure it to use the OIDC fetch as its adapter or use the OIDC fetch directly.
123
130
  trustedDomains.config_multi_tab_login = {
124
131
  domains: ['https://demo.duendesoftware.com'],
125
132
  allowMultiTabLogin: true,
package/dist/index.js CHANGED
@@ -1,122 +1,122 @@
1
- import { jsx as i, jsxs as S, Fragment as m } from "react/jsx-runtime";
2
- import { OidcClient as c, getParseQueryStringFromLocation as Q, getPath as O, OidcLocation as F, getFetchDefault as J } from "@axa-fr/oidc-client";
3
- import { OidcClient as Ue, OidcLocation as Me, TokenAutomaticRenewMode as Fe, TokenRenewMode as qe } from "@axa-fr/oidc-client";
4
- import V, { useCallback as X, useState as p, useEffect as v } from "react";
5
- const x = "default", Z = (t, s, e = !1) => async (...n) => await s().fetchWithTokens(t, e)(...n), Ce = (t = null, s = x, e = !1) => (n) => (o) => {
6
- const { fetch: r } = ee(
1
+ import { jsx as i, jsxs as E, Fragment as m } from "react/jsx-runtime";
2
+ import { OidcClient as c, getParseQueryStringFromLocation as Q, getPath as A, OidcLocation as F, getFetchDefault as J } from "@axa-fr/oidc-client";
3
+ import { OidcClient as Me, OidcLocation as Fe, TokenAutomaticRenewMode as qe, TokenRenewMode as De } from "@axa-fr/oidc-client";
4
+ import V, { useCallback as X, useState as f, useEffect as v, useRef as Z } from "react";
5
+ const R = "default", ee = (t, n, e = !1) => async (...s) => await n().fetchWithTokens(t, e)(...s), Ae = (t = null, n = R, e = !1) => (s) => (o) => {
6
+ const { fetch: r } = te(
7
7
  t || o.fetch,
8
- s,
8
+ n,
9
9
  e
10
10
  );
11
- return /* @__PURE__ */ i(n, { ...o, fetch: r });
12
- }, ee = (t = null, s = x, e = !1) => {
13
- const n = t || window.fetch, o = c.get;
11
+ return /* @__PURE__ */ i(s, { ...o, fetch: r });
12
+ }, te = (t = null, n = R, e = !1) => {
13
+ const s = t || window.fetch, o = c.get;
14
14
  return { fetch: X(
15
- (d, l) => Z(
16
- n,
17
- () => o(s),
15
+ (d, l) => ee(
16
+ s,
17
+ () => o(n),
18
18
  e
19
19
  )(d, l),
20
- [n, s, e]
20
+ [s, n, e]
21
21
  ) };
22
- }, W = () => /* @__PURE__ */ i("div", { className: "oidc-authenticating", children: /* @__PURE__ */ S("div", { className: "oidc-authenticating__container", children: [
22
+ }, x = () => /* @__PURE__ */ i("div", { className: "oidc-authenticating", children: /* @__PURE__ */ E("div", { className: "oidc-authenticating__container", children: [
23
23
  /* @__PURE__ */ i("h1", { className: "oidc-authenticating__title", children: "Error authentication" }),
24
24
  /* @__PURE__ */ i("p", { className: "oidc-authenticating__content", children: "An error occurred during authentication." })
25
- ] }) }), te = () => /* @__PURE__ */ i("div", { className: "oidc-authenticating", children: /* @__PURE__ */ S("div", { className: "oidc-authenticating__container", children: [
25
+ ] }) }), ne = () => /* @__PURE__ */ i("div", { className: "oidc-authenticating", children: /* @__PURE__ */ E("div", { className: "oidc-authenticating__container", children: [
26
26
  /* @__PURE__ */ i("h1", { className: "oidc-authenticating__title", children: "Authentication in progress" }),
27
27
  /* @__PURE__ */ i("p", { className: "oidc-authenticating__content", children: "You will be redirected to the login page." })
28
- ] }) }), se = () => Math.random().toString(36).slice(2, 8), ne = (t, s) => (e, n) => {
28
+ ] }) }), se = () => Math.random().toString(36).slice(2, 8), oe = (t, n) => (e, s) => {
29
29
  if (typeof t.CustomEvent == "function")
30
- return new t.CustomEvent(e, n);
31
- const o = n || { bubbles: !1, cancelable: !1, detail: void 0 }, r = s.createEvent("CustomEvent");
30
+ return new t.CustomEvent(e, s);
31
+ const o = s || { bubbles: !1, cancelable: !1, detail: void 0 }, r = n.createEvent("CustomEvent");
32
32
  return r.initCustomEvent(
33
33
  e,
34
34
  o.bubbles,
35
35
  o.cancelable,
36
36
  o.detail
37
37
  ), r.prototype = t.Event.prototype, r;
38
- }, oe = (t, s, e) => ({
39
- replaceState: (n, o) => {
38
+ }, ce = (t, n, e) => ({
39
+ replaceState: (s, o) => {
40
40
  const r = e(), d = o || t.history.state;
41
- t.history.replaceState({ key: r, state: d }, null, n), t.dispatchEvent(s("popstate"));
41
+ t.history.replaceState({ key: r, state: d }, null, s), t.dispatchEvent(n("popstate"));
42
42
  }
43
- }), ce = () => oe(window, ne(window, document), se), R = () => /* @__PURE__ */ i("div", { className: "oidc-callback", children: /* @__PURE__ */ S("div", { className: "oidc-callback__container", children: [
43
+ }), re = () => ce(window, oe(window, document), se), W = () => /* @__PURE__ */ i("div", { className: "oidc-callback", children: /* @__PURE__ */ E("div", { className: "oidc-callback__container", children: [
44
44
  /* @__PURE__ */ i("h1", { className: "oidc-callback__title", children: "Authentication complete" }),
45
45
  /* @__PURE__ */ i("p", { className: "oidc-callback__content", children: "You will be redirected to your application." })
46
- ] }) }), re = ({
46
+ ] }) }), ie = ({
47
47
  callBackError: t,
48
- callBackSuccess: s,
48
+ callBackSuccess: n,
49
49
  configurationName: e,
50
- withCustomHistory: n
50
+ withCustomHistory: s
51
51
  }) => {
52
- const [o, r] = p(!1);
52
+ const [o, r] = f(!1);
53
53
  v(() => {
54
54
  let a = !0;
55
55
  return (async () => {
56
- const _ = c.get;
56
+ const h = c.get;
57
57
  try {
58
- const { callbackPath: k } = await _(e).loginCallbackAsync();
59
- (n ? n() : ce()).replaceState(k || "/");
60
- } catch (k) {
61
- a && (console.warn(k), r(!0));
58
+ const { callbackPath: _ } = await h(e).loginCallbackAsync();
59
+ (s ? s() : re()).replaceState(_ || "/");
60
+ } catch (_) {
61
+ a && (console.warn(_), r(!0));
62
62
  }
63
63
  })(), () => {
64
64
  a = !1;
65
65
  };
66
66
  }, []);
67
- const d = t || W, l = s || R;
67
+ const d = t || x, l = n || W;
68
68
  return o ? /* @__PURE__ */ i(d, { configurationName: e }) : /* @__PURE__ */ i(l, { configurationName: e });
69
- }, ie = () => /* @__PURE__ */ i("span", { className: "oidc-loading", children: "Loading" }), le = () => /* @__PURE__ */ i("div", { className: "oidc-serviceworker", children: /* @__PURE__ */ S("div", { className: "oidc-serviceworker__container", children: [
69
+ }, le = () => /* @__PURE__ */ i("span", { className: "oidc-loading", children: "Loading" }), ae = () => /* @__PURE__ */ i("div", { className: "oidc-serviceworker", children: /* @__PURE__ */ E("div", { className: "oidc-serviceworker__container", children: [
70
70
  /* @__PURE__ */ i("h1", { className: "oidc-serviceworker__title", children: "Unable to authenticate on this browser" }),
71
71
  /* @__PURE__ */ i("p", { className: "oidc-serviceworker__content", children: "Your browser is not secure enough to make authentication work. Try updating your browser or use a newer browser." })
72
- ] }) }), ae = () => /* @__PURE__ */ i("div", { className: "oidc-session-lost", children: /* @__PURE__ */ S("div", { className: "oidc-session-lost__container", children: [
72
+ ] }) }), de = () => /* @__PURE__ */ i("div", { className: "oidc-session-lost", children: /* @__PURE__ */ E("div", { className: "oidc-session-lost__container", children: [
73
73
  /* @__PURE__ */ i("h1", { className: "oidc-session-lost__title", children: "Session timed out" }),
74
74
  /* @__PURE__ */ i("p", { className: "oidc-session-lost__content", children: "Your session has expired. Please re-authenticate." })
75
- ] }) }), de = ({ configurationName: t }) => (v(() => {
75
+ ] }) }), ue = ({ configurationName: t }) => (v(() => {
76
76
  (async () => {
77
77
  c.get(t).silentLoginCallbackAsync();
78
78
  })().catch((e) => {
79
79
  console.error("Error during silent login callback:", e);
80
80
  });
81
- }, [t]), null), ue = ({ configurationName: t }) => {
82
- const s = Q(window.location.href), e = c.get, n = e(t);
81
+ }, [t]), null), he = ({ configurationName: t }) => {
82
+ const n = Q(window.location.href), e = c.get, s = e(t);
83
83
  let o = null;
84
- for (const [r, d] of Object.entries(s))
84
+ for (const [r, d] of Object.entries(n))
85
85
  r === "state" || r === "scope" || (o === null && (o = {}), o[r] = d);
86
86
  return v(() => {
87
- n.tokens || n.loginAsync(null, o, !0, s.scope);
87
+ s.tokens || s.loginAsync(null, o, !0, n.scope);
88
88
  }, []), /* @__PURE__ */ i(m, {});
89
- }, he = ({
89
+ }, ke = ({
90
90
  callbackErrorComponent: t,
91
- callbackSuccessComponent: s,
91
+ callbackSuccessComponent: n,
92
92
  redirect_uri: e,
93
- silent_redirect_uri: n,
93
+ silent_redirect_uri: s,
94
94
  silent_login_uri: o,
95
95
  children: r,
96
96
  configurationName: d,
97
97
  withCustomHistory: l = null
98
98
  }) => {
99
- const a = window ? O(window.location.href) : "", [u, _] = p(a);
99
+ const a = window ? A(window.location.href) : "", [u, h] = f(a);
100
100
  v(() => {
101
- const f = () => _(O(window.location.href));
102
- return f(), window.addEventListener("popstate", f, !1), () => window.removeEventListener("popstate", f, !1);
101
+ const y = () => h(A(window.location.href));
102
+ return y(), window.addEventListener("popstate", y, !1), () => window.removeEventListener("popstate", y, !1);
103
103
  }, []);
104
- const k = O(e);
105
- return n && u === O(n) ? /* @__PURE__ */ i(de, { configurationName: d }) : o && u === O(o) ? /* @__PURE__ */ i(ue, { configurationName: d }) : u === k ? /* @__PURE__ */ i(
106
- re,
104
+ const _ = A(e);
105
+ return s && u === A(s) ? /* @__PURE__ */ i(ue, { configurationName: d }) : o && u === A(o) ? /* @__PURE__ */ i(he, { configurationName: d }) : u === _ ? /* @__PURE__ */ i(
106
+ ie,
107
107
  {
108
108
  callBackError: t,
109
- callBackSuccess: s,
109
+ callBackSuccess: n,
110
110
  configurationName: d,
111
111
  withCustomHistory: l
112
112
  }
113
113
  ) : /* @__PURE__ */ i(m, { children: r });
114
- }, ke = V.memo(he), q = { name: "", data: null }, _e = ({
114
+ }, _e = V.memo(ke), q = { name: "", data: null }, ve = ({
115
115
  loadingComponent: t,
116
- children: s,
116
+ children: n,
117
117
  configurationName: e
118
118
  }) => {
119
- const [n, o] = p(!0), r = c.get, d = r(e);
119
+ const [s, o] = f(!0), r = c.get, d = r(e);
120
120
  return v(() => {
121
121
  let a = !0;
122
122
  return d && d.tryKeepExistingSessionAsync().then(() => {
@@ -124,83 +124,83 @@ const x = "default", Z = (t, s, e = !1) => async (...n) => await s().fetchWithTo
124
124
  }), () => {
125
125
  a = !1;
126
126
  };
127
- }, [e]), /* @__PURE__ */ i(m, { children: n ? /* @__PURE__ */ i(t, { configurationName: e }) : /* @__PURE__ */ i(m, { children: s }) });
128
- }, E = ({ isLoading: t, loadingComponent: s, children: e, configurationName: n }) => {
129
- const o = s;
130
- return t ? /* @__PURE__ */ i(o, { configurationName: n, children: e }) : /* @__PURE__ */ i(m, { children: e });
131
- }, Ae = ({
127
+ }, [e]), /* @__PURE__ */ i(m, { children: s ? /* @__PURE__ */ i(t, { configurationName: e }) : /* @__PURE__ */ i(m, { children: n }) });
128
+ }, O = ({ isLoading: t, loadingComponent: n, children: e, configurationName: s }) => {
129
+ const o = n;
130
+ return t ? /* @__PURE__ */ i(o, { configurationName: s, children: e }) : /* @__PURE__ */ i(m, { children: e });
131
+ }, Oe = ({
132
132
  children: t,
133
- configuration: s,
133
+ configuration: n,
134
134
  configurationName: e = "default",
135
- callbackSuccessComponent: n = R,
136
- authenticatingComponent: o = te,
137
- loadingComponent: r = ie,
138
- serviceWorkerNotSupportedComponent: d = le,
139
- authenticatingErrorComponent: l = W,
140
- sessionLostComponent: a = ae,
135
+ callbackSuccessComponent: s = W,
136
+ authenticatingComponent: o = ne,
137
+ loadingComponent: r = le,
138
+ serviceWorkerNotSupportedComponent: d = ae,
139
+ authenticatingErrorComponent: l = x,
140
+ sessionLostComponent: a = de,
141
141
  onSessionLost: u = null,
142
- onLogoutFromAnotherTab: _ = null,
143
- onLogoutFromSameTab: k = null,
144
- withCustomHistory: f = null,
145
- onEvent: w = null,
146
- getFetch: y = null,
147
- location: P = null
142
+ onLogoutFromAnotherTab: h = null,
143
+ onLogoutFromSameTab: _ = null,
144
+ withCustomHistory: y = null,
145
+ onEvent: p = null,
146
+ getFetch: S = null,
147
+ location: U = null
148
148
  }) => {
149
- if (s && s.redirect_uri && s.silent_redirect_uri && s.redirect_uri === s.silent_redirect_uri)
149
+ if (n && n.redirect_uri && n.silent_redirect_uri && n.redirect_uri === n.silent_redirect_uri)
150
150
  throw new Error("redirect_uri and silent_redirect_uri must be different");
151
- const T = (N = "default") => c.getOrCreate(y ?? J, P ?? new F())(
152
- s,
153
- N
154
- ), [B, C] = p(q), [z, M] = p(e);
151
+ const w = (L = "default") => c.getOrCreate(S ?? J, U ?? new F())(
152
+ n,
153
+ L
154
+ ), [B, T] = f(q), [z, M] = f(e);
155
155
  v(() => {
156
- const I = T(e).subscribeEvents((h, g) => {
157
- w && w(e, h, g);
156
+ const N = w(e).subscribeEvents((k, g) => {
157
+ p && p(e, k, g);
158
158
  });
159
159
  return () => {
160
- T(e).removeEventSubscription(I);
160
+ w(e).removeEventSubscription(N);
161
161
  };
162
- }, [e, w]), v(() => {
163
- const I = T(e).subscribeEvents((h, g) => {
164
- if (h === c.eventNames.refreshTokensAsync_error || h === c.eventNames.syncTokensAsync_error) {
162
+ }, [e, p]), v(() => {
163
+ const N = w(e).subscribeEvents((k, g) => {
164
+ if (k === c.eventNames.refreshTokensAsync_error || k === c.eventNames.syncTokensAsync_error) {
165
165
  if (u != null) {
166
166
  u();
167
167
  return;
168
168
  }
169
- C({ name: h, data: g });
170
- } else if (h === c.eventNames.logout_from_another_tab) {
171
- if (_ != null) {
172
- _();
169
+ T({ name: k, data: g });
170
+ } else if (k === c.eventNames.logout_from_another_tab) {
171
+ if (h != null) {
172
+ h();
173
173
  return;
174
174
  }
175
- C({ name: h, data: g });
176
- } else h === c.eventNames.logout_from_same_tab ? k?.() : h === c.eventNames.loginAsync_begin || h === c.eventNames.loginCallbackAsync_end || h === c.eventNames.loginAsync_error || h === c.eventNames.loginCallbackAsync_error ? C({ name: h, data: g }) : h === c.eventNames.service_worker_not_supported_by_browser && s.service_worker_only === !0 && C({ name: h, data: g });
175
+ T({ name: k, data: g });
176
+ } else k === c.eventNames.logout_from_same_tab ? _?.() : k === c.eventNames.loginAsync_begin || k === c.eventNames.loginCallbackAsync_end || k === c.eventNames.loginAsync_error || k === c.eventNames.loginCallbackAsync_error ? T({ name: k, data: g }) : k === c.eventNames.service_worker_not_supported_by_browser && n.service_worker_only === !0 && T({ name: k, data: g });
177
177
  });
178
178
  return queueMicrotask(() => {
179
179
  M(e);
180
180
  }), () => {
181
- T(e).removeEventSubscription(I), C(q);
181
+ w(e).removeEventSubscription(N), T(q);
182
182
  };
183
- }, [s, e]), v(() => {
183
+ }, [n, e]), v(() => {
184
184
  M(e);
185
185
  }, [e]);
186
- const K = a, $ = o, b = r, G = d, H = l, A = z !== e, L = T(e);
186
+ const K = a, $ = o, b = r, G = d, H = l, C = z !== e, P = w(e);
187
187
  switch (B.name) {
188
188
  case c.eventNames.service_worker_not_supported_by_browser:
189
189
  return /* @__PURE__ */ i(
190
- E,
190
+ O,
191
191
  {
192
192
  loadingComponent: b,
193
- isLoading: A,
193
+ isLoading: C,
194
194
  configurationName: e,
195
195
  children: /* @__PURE__ */ i(G, { configurationName: e })
196
196
  }
197
197
  );
198
198
  case c.eventNames.loginAsync_begin:
199
199
  return /* @__PURE__ */ i(
200
- E,
200
+ O,
201
201
  {
202
202
  loadingComponent: b,
203
- isLoading: A,
203
+ isLoading: C,
204
204
  configurationName: e,
205
205
  children: /* @__PURE__ */ i($, { configurationName: e })
206
206
  }
@@ -208,10 +208,10 @@ const x = "default", Z = (t, s, e = !1) => async (...n) => await s().fetchWithTo
208
208
  case c.eventNames.loginAsync_error:
209
209
  case c.eventNames.loginCallbackAsync_error:
210
210
  return /* @__PURE__ */ i(
211
- E,
211
+ O,
212
212
  {
213
213
  loadingComponent: b,
214
- isLoading: A,
214
+ isLoading: C,
215
215
  configurationName: e,
216
216
  children: /* @__PURE__ */ i(H, { configurationName: e })
217
217
  }
@@ -220,72 +220,72 @@ const x = "default", Z = (t, s, e = !1) => async (...n) => await s().fetchWithTo
220
220
  case c.eventNames.syncTokensAsync_error:
221
221
  case c.eventNames.logout_from_another_tab:
222
222
  return /* @__PURE__ */ i(
223
- E,
223
+ O,
224
224
  {
225
225
  loadingComponent: b,
226
- isLoading: A,
226
+ isLoading: C,
227
227
  configurationName: e,
228
228
  children: /* @__PURE__ */ i(K, { configurationName: e })
229
229
  }
230
230
  );
231
231
  default:
232
232
  return /* @__PURE__ */ i(
233
- E,
233
+ O,
234
234
  {
235
235
  loadingComponent: b,
236
- isLoading: A,
236
+ isLoading: C,
237
237
  configurationName: e,
238
238
  children: /* @__PURE__ */ i(
239
- ke,
239
+ _e,
240
240
  {
241
- redirect_uri: L.configuration.redirect_uri,
242
- silent_redirect_uri: L.configuration.silent_redirect_uri,
243
- silent_login_uri: L.configuration.silent_login_uri,
244
- callbackSuccessComponent: n,
241
+ redirect_uri: P.configuration.redirect_uri,
242
+ silent_redirect_uri: P.configuration.silent_redirect_uri,
243
+ silent_login_uri: P.configuration.silent_login_uri,
244
+ callbackSuccessComponent: s,
245
245
  callbackErrorComponent: l,
246
246
  authenticatingComponent: o,
247
247
  configurationName: e,
248
- withCustomHistory: f,
249
- location: P ?? new F(),
250
- children: /* @__PURE__ */ i(_e, { loadingComponent: b, configurationName: e, children: t })
248
+ withCustomHistory: y,
249
+ location: U ?? new F(),
250
+ children: /* @__PURE__ */ i(ve, { loadingComponent: b, configurationName: e, children: t })
251
251
  }
252
252
  )
253
253
  }
254
254
  );
255
255
  }
256
- }, ve = ({
256
+ }, fe = ({
257
257
  children: t,
258
- callbackPath: s = null,
258
+ callbackPath: n = null,
259
259
  extras: e = null,
260
- configurationName: n = "default"
260
+ configurationName: s = "default"
261
261
  }) => {
262
- const o = c.get, r = o(n);
262
+ const o = c.get, r = o(s);
263
263
  return v(() => {
264
- r.tokens || r.loginAsync(s, e);
265
- }, [n, s, e]), r.tokens ? /* @__PURE__ */ i(m, { children: t }) : null;
266
- }, Oe = (t, s = null, e = null, n = "default") => (o) => /* @__PURE__ */ i(ve, { callbackPath: s, extras: e, configurationName: n, children: /* @__PURE__ */ i(t, { ...o }) }), U = "default", D = (t, s) => {
264
+ r.tokens || r.loginAsync(n, e);
265
+ }, [s, n, e]), r.tokens ? /* @__PURE__ */ i(m, { children: t }) : null;
266
+ }, Ee = (t, n = null, e = null, s = "default") => (o) => /* @__PURE__ */ i(fe, { callbackPath: n, extras: e, configurationName: s, children: /* @__PURE__ */ i(t, { ...o }) }), I = "default", D = (t, n) => {
267
267
  let e = !1;
268
- return t(s) && (e = t(s).tokens != null), e;
269
- }, Ee = (t = U) => {
270
- const s = c.get, [e, n] = p(
271
- () => D(s, t)
268
+ return t(n) && (e = t(n).tokens != null), e;
269
+ }, Se = (t = I) => {
270
+ const n = c.get, [e, s] = f(
271
+ () => D(n, t)
272
272
  );
273
273
  return v(() => {
274
274
  let l = !0;
275
- const a = s(t), u = a.subscribeEvents((_, k) => {
276
- (_ === c.eventNames.logout_from_another_tab || _ === c.eventNames.logout_from_same_tab || _ === c.eventNames.token_acquired) && l && n(D(s, t));
275
+ const a = n(t), u = a.subscribeEvents((h, _) => {
276
+ (h === c.eventNames.logout_from_another_tab || h === c.eventNames.logout_from_same_tab || h === c.eventNames.token_acquired) && l && s(D(n, t));
277
277
  });
278
278
  return () => {
279
279
  l = !1, a.removeEventSubscription(u);
280
280
  };
281
- }, [t]), { login: (l = void 0, a = void 0, u = !1, _ = void 0) => s(t).loginAsync(
281
+ }, [t]), { login: (l = void 0, a = void 0, u = !1, h = void 0) => n(t).loginAsync(
282
282
  l,
283
283
  a,
284
284
  !1,
285
- _,
285
+ h,
286
286
  u
287
- ), logout: (l = void 0, a = void 0) => s(t).logoutAsync(l, a), renewTokens: async (l = void 0) => {
288
- const a = await s(t).renewTokensAsync(l);
287
+ ), logout: (l = void 0, a = void 0) => n(t).logoutAsync(l, a), renewTokens: async (l = void 0) => {
288
+ const a = await n(t).renewTokensAsync(l);
289
289
  return {
290
290
  // @ts-ignore
291
291
  accessToken: a.accessToken,
@@ -297,33 +297,33 @@ const x = "default", Z = (t, s, e = !1) => async (...n) => await s().fetchWithTo
297
297
  idTokenPayload: a.idTokenPayload
298
298
  };
299
299
  }, isAuthenticated: e };
300
- }, Y = { accessToken: null, accessTokenPayload: null }, fe = (t) => {
301
- const s = c.get, e = s(t);
300
+ }, Y = { accessToken: null, accessTokenPayload: null }, pe = (t) => {
301
+ const n = c.get, e = n(t);
302
302
  if (e.tokens) {
303
- const n = e.tokens;
303
+ const s = e.tokens;
304
304
  return {
305
- accessToken: n.accessToken,
306
- accessTokenPayload: n.accessTokenPayload,
307
- generateDemonstrationOfProofOfPossessionAsync: e.configuration.demonstrating_proof_of_possession ? (o, r) => e.generateDemonstrationOfProofOfPossessionAsync(n.accessToken, o, r) : null
305
+ accessToken: s.accessToken,
306
+ accessTokenPayload: s.accessTokenPayload,
307
+ generateDemonstrationOfProofOfPossessionAsync: e.configuration.demonstrating_proof_of_possession ? (o, r) => e.generateDemonstrationOfProofOfPossessionAsync(s.accessToken, o, r) : null
308
308
  };
309
309
  }
310
310
  return Y;
311
311
  };
312
- function pe(t, s) {
313
- return t.configuration.demonstrating_proof_of_possession ? (e, n, o = {}) => t.generateDemonstrationOfProofOfPossessionAsync(s.accessToken, e, n, o) : null;
312
+ function ye(t, n) {
313
+ return t.configuration.demonstrating_proof_of_possession ? (e, s, o = {}) => t.generateDemonstrationOfProofOfPossessionAsync(n.accessToken, e, s, o) : null;
314
314
  }
315
- const Se = (t = U) => {
316
- const s = c.get, [e, n] = p(() => fe(t));
315
+ const Pe = (t = I) => {
316
+ const n = c.get, [e, s] = f(() => pe(t));
317
317
  return v(() => {
318
318
  let o = !0;
319
- const r = s(t), d = r.subscribeEvents((l, a) => {
319
+ const r = n(t), d = r.subscribeEvents((l, a) => {
320
320
  if ((l === c.eventNames.token_renewed || l === c.eventNames.token_acquired || l === c.eventNames.logout_from_another_tab || l === c.eventNames.logout_from_same_tab || l === c.eventNames.refreshTokensAsync_error || l === c.eventNames.syncTokensAsync_error) && o) {
321
321
  const u = r.tokens;
322
- n(
322
+ s(
323
323
  u != null ? {
324
324
  accessToken: u.accessToken,
325
325
  accessTokenPayload: u.accessTokenPayload,
326
- generateDemonstrationOfProofOfPossessionAsync: pe(r, u)
326
+ generateDemonstrationOfProofOfPossessionAsync: ye(r, u)
327
327
  } : Y
328
328
  );
329
329
  }
@@ -332,21 +332,21 @@ const Se = (t = U) => {
332
332
  o = !1, r.removeEventSubscription(d);
333
333
  };
334
334
  }, [t]), e;
335
- }, j = { idToken: null, idTokenPayload: null }, ye = (t) => {
336
- const s = c.get, e = s(t);
335
+ }, j = { idToken: null, idTokenPayload: null }, be = (t) => {
336
+ const n = c.get, e = n(t);
337
337
  if (e.tokens) {
338
- const n = e.tokens;
339
- return { idToken: n.idToken, idTokenPayload: n.idTokenPayload };
338
+ const s = e.tokens;
339
+ return { idToken: s.idToken, idTokenPayload: s.idTokenPayload };
340
340
  }
341
341
  return j;
342
- }, Pe = (t = U) => {
343
- const s = c.get, [e, n] = p(() => ye(t));
342
+ }, Le = (t = I) => {
343
+ const n = c.get, [e, s] = f(() => be(t));
344
344
  return v(() => {
345
345
  let o = !0;
346
- const r = s(t), d = r.subscribeEvents((l, a) => {
346
+ const r = n(t), d = r.subscribeEvents((l, a) => {
347
347
  if ((l === c.eventNames.token_renewed || l === c.eventNames.token_acquired || l === c.eventNames.logout_from_another_tab || l === c.eventNames.logout_from_same_tab || l === c.eventNames.refreshTokensAsync_error || l === c.eventNames.syncTokensAsync_error) && o) {
348
348
  const u = r.tokens;
349
- n(
349
+ s(
350
350
  u != null ? { idToken: u.idToken, idTokenPayload: u.idTokenPayload } : j
351
351
  );
352
352
  }
@@ -356,29 +356,29 @@ const Se = (t = U) => {
356
356
  };
357
357
  }, [t]), e;
358
358
  };
359
- var be = /* @__PURE__ */ ((t) => (t.Unauthenticated = "Unauthenticated", t.Loading = "Loading user", t.Loaded = "User loaded", t.LoadingError = "Error loading user", t))(be || {});
360
- const Le = (t = "default", s = !1) => {
361
- const n = c.get(t).userInfo(), [o, r] = p({
362
- user: n,
363
- status: n ? "User loaded" : "Unauthenticated"
359
+ var ge = /* @__PURE__ */ ((t) => (t.Unauthenticated = "Unauthenticated", t.Loading = "Loading user", t.Loaded = "User loaded", t.LoadingError = "Error loading user", t))(ge || {});
360
+ const Ne = (t = "default", n = !1) => {
361
+ const s = c.get(t).userInfo(), [o, r] = f({
362
+ user: s,
363
+ status: s ? "User loaded" : "Unauthenticated"
364
364
  /* Unauthenticated */
365
- }), [d, l] = p(n ? 1 : 0), [a, u] = p(n ? 1 : 0);
365
+ }), [d, l] = f(s ? 1 : 0), a = Z(s ? 1 : 0);
366
366
  v(() => {
367
- const k = c.get(t);
368
- let f = !0;
369
- if (k && k.tokens) {
370
- const y = d === a;
371
- if (y && k.userInfo())
367
+ const h = c.get(t);
368
+ let _ = !0;
369
+ if (h && h.tokens) {
370
+ const p = d === a.current;
371
+ if (p && h.userInfo())
372
372
  return;
373
- queueMicrotask(() => {
374
- f && (r({
373
+ a.current = d, queueMicrotask(() => {
374
+ _ && r({
375
375
  ...o,
376
376
  status: "Loading user"
377
377
  /* Loading */
378
- }), u(d));
379
- }), k.userInfoAsync(!y, s).then((P) => {
380
- f && r({
381
- user: P,
378
+ });
379
+ }), h.userInfoAsync(!p, n).then((S) => {
380
+ _ && r({
381
+ user: S,
382
382
  status: "User loaded"
383
383
  /* Loaded */
384
384
  });
@@ -389,41 +389,41 @@ const Le = (t = "default", s = !1) => {
389
389
  }));
390
390
  } else
391
391
  queueMicrotask(() => {
392
- f && r({
392
+ _ && r({
393
393
  user: null,
394
394
  status: "Unauthenticated"
395
395
  /* Unauthenticated */
396
396
  });
397
397
  });
398
- const w = k.subscribeEvents((y) => {
399
- (y === c.eventNames.logout_from_another_tab || y === c.eventNames.logout_from_same_tab) && f && r({
398
+ const y = h.subscribeEvents((p) => {
399
+ (p === c.eventNames.logout_from_another_tab || p === c.eventNames.logout_from_same_tab) && _ && r({
400
400
  user: null,
401
401
  status: "Unauthenticated"
402
402
  /* Unauthenticated */
403
403
  });
404
404
  });
405
405
  return () => {
406
- f = !1, k.removeEventSubscription(w);
406
+ _ = !1, h.removeEventSubscription(y);
407
407
  };
408
- }, [d, t, s, a]);
409
- const _ = () => {
408
+ }, [d, t, n]);
409
+ const u = () => {
410
410
  l(d + 1);
411
411
  };
412
- return { oidcUser: o.user, oidcUserLoadingState: o.status, reloadOidcUser: _ };
412
+ return { oidcUser: o.user, oidcUserLoadingState: o.status, reloadOidcUser: u };
413
413
  };
414
414
  export {
415
- Ue as OidcClient,
416
- Me as OidcLocation,
417
- Ae as OidcProvider,
418
- ve as OidcSecure,
419
- be as OidcUserStatus,
420
- Fe as TokenAutomaticRenewMode,
421
- qe as TokenRenewMode,
422
- Ee as useOidc,
423
- Se as useOidcAccessToken,
424
- ee as useOidcFetch,
425
- Pe as useOidcIdToken,
426
- Le as useOidcUser,
427
- Ce as withOidcFetch,
428
- Oe as withOidcSecure
415
+ Me as OidcClient,
416
+ Fe as OidcLocation,
417
+ Oe as OidcProvider,
418
+ fe as OidcSecure,
419
+ ge as OidcUserStatus,
420
+ qe as TokenAutomaticRenewMode,
421
+ De as TokenRenewMode,
422
+ Se as useOidc,
423
+ Pe as useOidcAccessToken,
424
+ te as useOidcFetch,
425
+ Le as useOidcIdToken,
426
+ Ne as useOidcUser,
427
+ Ae as withOidcFetch,
428
+ Ee as withOidcSecure
429
429
  };
@@ -1 +1 @@
1
- (function(f,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react/jsx-runtime"),require("@axa-fr/oidc-client"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","@axa-fr/oidc-client","react"],c):(f=typeof globalThis<"u"?globalThis:f||self,c(f["react-oidc"]={},f.jsxRuntime,f.oidcClient,f.React))})(this,(function(f,c,s,d){"use strict";const F="default",z=(t,n,e=!1)=>async(...o)=>await n().fetchWithTokens(t,e)(...o),K=(t=null,n=F,e=!1)=>o=>r=>{const{fetch:i}=I(t||r.fetch,n,e);return c.jsx(o,{...r,fetch:i})},I=(t=null,n=F,e=!1)=>{const o=t||window.fetch,r=s.OidcClient.get;return{fetch:d.useCallback((u,a)=>z(o,()=>r(n),e)(u,a),[o,n,e])}},U=()=>c.jsx("div",{className:"oidc-authenticating",children:c.jsxs("div",{className:"oidc-authenticating__container",children:[c.jsx("h1",{className:"oidc-authenticating__title",children:"Error authentication"}),c.jsx("p",{className:"oidc-authenticating__content",children:"An error occurred during authentication."})]})}),$=()=>c.jsx("div",{className:"oidc-authenticating",children:c.jsxs("div",{className:"oidc-authenticating__container",children:[c.jsx("h1",{className:"oidc-authenticating__title",children:"Authentication in progress"}),c.jsx("p",{className:"oidc-authenticating__content",children:"You will be redirected to the login page."})]})}),G=()=>Math.random().toString(36).slice(2,8),H=(t,n)=>(e,o)=>{if(typeof t.CustomEvent=="function")return new t.CustomEvent(e,o);const r=o||{bubbles:!1,cancelable:!1,detail:void 0},i=n.createEvent("CustomEvent");return i.initCustomEvent(e,r.bubbles,r.cancelable,r.detail),i.prototype=t.Event.prototype,i},Q=(t,n,e)=>({replaceState:(o,r)=>{const i=e(),u=r||t.history.state;t.history.replaceState({key:i,state:u},null,o),t.dispatchEvent(n("popstate"))}}),J=()=>Q(window,H(window,document),G),m=()=>c.jsx("div",{className:"oidc-callback",children:c.jsxs("div",{className:"oidc-callback__container",children:[c.jsx("h1",{className:"oidc-callback__title",children:"Authentication complete"}),c.jsx("p",{className:"oidc-callback__content",children:"You will be redirected to your application."})]})}),V=({callBackError:t,callBackSuccess:n,configurationName:e,withCustomHistory:o})=>{const[r,i]=d.useState(!1);d.useEffect(()=>{let l=!0;return(async()=>{const O=s.OidcClient.get;try{const{callbackPath:_}=await O(e).loginCallbackAsync();(o?o():J()).replaceState(_||"/")}catch(_){l&&(console.warn(_),i(!0))}})(),()=>{l=!1}},[]);const u=t||U,a=n||m;return r?c.jsx(u,{configurationName:e}):c.jsx(a,{configurationName:e})},X=()=>c.jsx("span",{className:"oidc-loading",children:"Loading"}),Z=()=>c.jsx("div",{className:"oidc-serviceworker",children:c.jsxs("div",{className:"oidc-serviceworker__container",children:[c.jsx("h1",{className:"oidc-serviceworker__title",children:"Unable to authenticate on this browser"}),c.jsx("p",{className:"oidc-serviceworker__content",children:"Your browser is not secure enough to make authentication work. Try updating your browser or use a newer browser."})]})}),x=()=>c.jsx("div",{className:"oidc-session-lost",children:c.jsxs("div",{className:"oidc-session-lost__container",children:[c.jsx("h1",{className:"oidc-session-lost__title",children:"Session timed out"}),c.jsx("p",{className:"oidc-session-lost__content",children:"Your session has expired. Please re-authenticate."})]})}),R=({configurationName:t})=>(d.useEffect(()=>{(async()=>{s.OidcClient.get(t).silentLoginCallbackAsync()})().catch(e=>{console.error("Error during silent login callback:",e)})},[t]),null),ee=({configurationName:t})=>{const n=s.getParseQueryStringFromLocation(window.location.href),e=s.OidcClient.get,o=e(t);let r=null;for(const[i,u]of Object.entries(n))i==="state"||i==="scope"||(r===null&&(r={}),r[i]=u);return d.useEffect(()=>{o.tokens||o.loginAsync(null,r,!0,n.scope)},[]),c.jsx(c.Fragment,{})},te=({callbackErrorComponent:t,callbackSuccessComponent:n,redirect_uri:e,silent_redirect_uri:o,silent_login_uri:r,children:i,configurationName:u,withCustomHistory:a=null})=>{const l=window?s.getPath(window.location.href):"",[h,O]=d.useState(l);d.useEffect(()=>{const v=()=>O(s.getPath(window.location.href));return v(),window.addEventListener("popstate",v,!1),()=>window.removeEventListener("popstate",v,!1)},[]);const _=s.getPath(e);return o&&h===s.getPath(o)?c.jsx(R,{configurationName:u}):r&&h===s.getPath(r)?c.jsx(ee,{configurationName:u}):h===_?c.jsx(V,{callBackError:t,callBackSuccess:n,configurationName:u,withCustomHistory:a}):c.jsx(c.Fragment,{children:i})},se=d.memo(te),M={name:"",data:null},ne=({loadingComponent:t,children:n,configurationName:e})=>{const[o,r]=d.useState(!0),i=s.OidcClient.get,u=i(e);d.useEffect(()=>{let l=!0;return u&&u.tryKeepExistingSessionAsync().then(()=>{l&&r(!1)}),()=>{l=!1}},[e]);const a=t;return c.jsx(c.Fragment,{children:o?c.jsx(a,{configurationName:e}):c.jsx(c.Fragment,{children:n})})},p=({isLoading:t,loadingComponent:n,children:e,configurationName:o})=>{const r=n;return t?c.jsx(r,{configurationName:o,children:e}):c.jsx(c.Fragment,{children:e})},ce=({children:t,configuration:n,configurationName:e="default",callbackSuccessComponent:o=m,authenticatingComponent:r=$,loadingComponent:i=X,serviceWorkerNotSupportedComponent:u=Z,authenticatingErrorComponent:a=U,sessionLostComponent:l=x,onSessionLost:h=null,onLogoutFromAnotherTab:O=null,onLogoutFromSameTab:_=null,withCustomHistory:v=null,onEvent:w=null,getFetch:y=null,location:E=null})=>{if(n&&n.redirect_uri&&n.silent_redirect_uri&&n.redirect_uri===n.silent_redirect_uri)throw new Error("redirect_uri and silent_redirect_uri must be different");const T=(L="default")=>s.OidcClient.getOrCreate(y??s.getFetchDefault,E??new s.OidcLocation)(n,L),[fe,S]=d.useState(M),[ke,B]=d.useState(e);d.useEffect(()=>{const N=T(e).subscribeEvents((k,g)=>{w&&w(e,k,g)});return()=>{T(e).removeEventSubscription(N)}},[e,w]),d.useEffect(()=>{const N=T(e).subscribeEvents((k,g)=>{if(k===s.OidcClient.eventNames.refreshTokensAsync_error||k===s.OidcClient.eventNames.syncTokensAsync_error){if(h!=null){h();return}S({name:k,data:g})}else if(k===s.OidcClient.eventNames.logout_from_another_tab){if(O!=null){O();return}S({name:k,data:g})}else k===s.OidcClient.eventNames.logout_from_same_tab?_?.():k===s.OidcClient.eventNames.loginAsync_begin||k===s.OidcClient.eventNames.loginCallbackAsync_end||k===s.OidcClient.eventNames.loginAsync_error||k===s.OidcClient.eventNames.loginCallbackAsync_error?S({name:k,data:g}):k===s.OidcClient.eventNames.service_worker_not_supported_by_browser&&n.service_worker_only===!0&&S({name:k,data:g})});return queueMicrotask(()=>{B(e)}),()=>{T(e).removeEventSubscription(N),S(M)}},[n,e]),d.useEffect(()=>{B(e)},[e]);const _e=l,Oe=r,b=i,ve=u,ye=a,A=ke!==e,C=T(e);switch(fe.name){case s.OidcClient.eventNames.service_worker_not_supported_by_browser:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(ve,{configurationName:e})});case s.OidcClient.eventNames.loginAsync_begin:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(Oe,{configurationName:e})});case s.OidcClient.eventNames.loginAsync_error:case s.OidcClient.eventNames.loginCallbackAsync_error:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(ye,{configurationName:e})});case s.OidcClient.eventNames.refreshTokensAsync_error:case s.OidcClient.eventNames.syncTokensAsync_error:case s.OidcClient.eventNames.logout_from_another_tab:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(_e,{configurationName:e})});default:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(se,{redirect_uri:C.configuration.redirect_uri,silent_redirect_uri:C.configuration.silent_redirect_uri,silent_login_uri:C.configuration.silent_login_uri,callbackSuccessComponent:o,callbackErrorComponent:a,authenticatingComponent:r,configurationName:e,withCustomHistory:v,location:E??new s.OidcLocation,children:c.jsx(ne,{loadingComponent:b,configurationName:e,children:t})})})}},q=({children:t,callbackPath:n=null,extras:e=null,configurationName:o="default"})=>{const r=s.OidcClient.get,i=r(o);return d.useEffect(()=>{i.tokens||i.loginAsync(n,e)},[o,n,e]),i.tokens?c.jsx(c.Fragment,{children:t}):null},oe=(t,n=null,e=null,o="default")=>r=>c.jsx(q,{callbackPath:n,extras:e,configurationName:o,children:c.jsx(t,{...r})}),P="default",D=(t,n)=>{let e=!1;return t(n)&&(e=t(n).tokens!=null),e},re=(t=P)=>{const n=s.OidcClient.get,[e,o]=d.useState(()=>D(n,t));return d.useEffect(()=>{let a=!0;const l=n(t),h=l.subscribeEvents((O,_)=>{(O===s.OidcClient.eventNames.logout_from_another_tab||O===s.OidcClient.eventNames.logout_from_same_tab||O===s.OidcClient.eventNames.token_acquired)&&a&&o(D(n,t))});return()=>{a=!1,l.removeEventSubscription(h)}},[t]),{login:(a=void 0,l=void 0,h=!1,O=void 0)=>n(t).loginAsync(a,l,!1,O,h),logout:(a=void 0,l=void 0)=>n(t).logoutAsync(a,l),renewTokens:async(a=void 0)=>{const l=await n(t).renewTokensAsync(a);return{accessToken:l.accessToken,accessTokenPayload:l.accessTokenPayload,idToken:l.idToken,idTokenPayload:l.idTokenPayload}},isAuthenticated:e}},W={accessToken:null,accessTokenPayload:null},ie=t=>{const n=s.OidcClient.get,e=n(t);if(e.tokens){const o=e.tokens;return{accessToken:o.accessToken,accessTokenPayload:o.accessTokenPayload,generateDemonstrationOfProofOfPossessionAsync:e.configuration.demonstrating_proof_of_possession?(r,i)=>e.generateDemonstrationOfProofOfPossessionAsync(o.accessToken,r,i):null}}return W};function ae(t,n){return t.configuration.demonstrating_proof_of_possession?(e,o,r={})=>t.generateDemonstrationOfProofOfPossessionAsync(n.accessToken,e,o,r):null}const le=(t=P)=>{const n=s.OidcClient.get,[e,o]=d.useState(()=>ie(t));return d.useEffect(()=>{let r=!0;const i=n(t),u=i.subscribeEvents((a,l)=>{if((a===s.OidcClient.eventNames.token_renewed||a===s.OidcClient.eventNames.token_acquired||a===s.OidcClient.eventNames.logout_from_another_tab||a===s.OidcClient.eventNames.logout_from_same_tab||a===s.OidcClient.eventNames.refreshTokensAsync_error||a===s.OidcClient.eventNames.syncTokensAsync_error)&&r){const h=i.tokens;o(h!=null?{accessToken:h.accessToken,accessTokenPayload:h.accessTokenPayload,generateDemonstrationOfProofOfPossessionAsync:ae(i,h)}:W)}});return()=>{r=!1,i.removeEventSubscription(u)}},[t]),e},j={idToken:null,idTokenPayload:null},de=t=>{const n=s.OidcClient.get,e=n(t);if(e.tokens){const o=e.tokens;return{idToken:o.idToken,idTokenPayload:o.idTokenPayload}}return j},ue=(t=P)=>{const n=s.OidcClient.get,[e,o]=d.useState(()=>de(t));return d.useEffect(()=>{let r=!0;const i=n(t),u=i.subscribeEvents((a,l)=>{if((a===s.OidcClient.eventNames.token_renewed||a===s.OidcClient.eventNames.token_acquired||a===s.OidcClient.eventNames.logout_from_another_tab||a===s.OidcClient.eventNames.logout_from_same_tab||a===s.OidcClient.eventNames.refreshTokensAsync_error||a===s.OidcClient.eventNames.syncTokensAsync_error)&&r){const h=i.tokens;o(h!=null?{idToken:h.idToken,idTokenPayload:h.idTokenPayload}:j)}});return()=>{r=!1,i.removeEventSubscription(u)}},[t]),e};var Y=(t=>(t.Unauthenticated="Unauthenticated",t.Loading="Loading user",t.Loaded="User loaded",t.LoadingError="Error loading user",t))(Y||{});const he=(t="default",n=!1)=>{const o=s.OidcClient.get(t).userInfo(),[r,i]=d.useState({user:o,status:o?"User loaded":"Unauthenticated"}),[u,a]=d.useState(o?1:0),[l,h]=d.useState(o?1:0);d.useEffect(()=>{const _=s.OidcClient.get(t);let v=!0;if(_&&_.tokens){const y=u===l;if(y&&_.userInfo())return;queueMicrotask(()=>{v&&(i({...r,status:"Loading user"}),h(u))}),_.userInfoAsync(!y,n).then(E=>{v&&i({user:E,status:"User loaded"})}).catch(()=>i({...r,status:"Error loading user"}))}else queueMicrotask(()=>{v&&i({user:null,status:"Unauthenticated"})});const w=_.subscribeEvents(y=>{(y===s.OidcClient.eventNames.logout_from_another_tab||y===s.OidcClient.eventNames.logout_from_same_tab)&&v&&i({user:null,status:"Unauthenticated"})});return()=>{v=!1,_.removeEventSubscription(w)}},[u,t,n,l]);const O=()=>{a(u+1)};return{oidcUser:r.user,oidcUserLoadingState:r.status,reloadOidcUser:O}};Object.defineProperty(f,"OidcClient",{enumerable:!0,get:()=>s.OidcClient}),Object.defineProperty(f,"OidcLocation",{enumerable:!0,get:()=>s.OidcLocation}),Object.defineProperty(f,"TokenAutomaticRenewMode",{enumerable:!0,get:()=>s.TokenAutomaticRenewMode}),Object.defineProperty(f,"TokenRenewMode",{enumerable:!0,get:()=>s.TokenRenewMode}),f.OidcProvider=ce,f.OidcSecure=q,f.OidcUserStatus=Y,f.useOidc=re,f.useOidcAccessToken=le,f.useOidcFetch=I,f.useOidcIdToken=ue,f.useOidcUser=he,f.withOidcFetch=K,f.withOidcSecure=oe,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(f,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react/jsx-runtime"),require("@axa-fr/oidc-client"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","@axa-fr/oidc-client","react"],c):(f=typeof globalThis<"u"?globalThis:f||self,c(f["react-oidc"]={},f.jsxRuntime,f.oidcClient,f.React))})(this,(function(f,c,s,d){"use strict";const N="default",z=(t,n,e=!1)=>async(...o)=>await n().fetchWithTokens(t,e)(...o),K=(t=null,n=N,e=!1)=>o=>r=>{const{fetch:i}=F(t||r.fetch,n,e);return c.jsx(o,{...r,fetch:i})},F=(t=null,n=N,e=!1)=>{const o=t||window.fetch,r=s.OidcClient.get;return{fetch:d.useCallback((u,a)=>z(o,()=>r(n),e)(u,a),[o,n,e])}},I=()=>c.jsx("div",{className:"oidc-authenticating",children:c.jsxs("div",{className:"oidc-authenticating__container",children:[c.jsx("h1",{className:"oidc-authenticating__title",children:"Error authentication"}),c.jsx("p",{className:"oidc-authenticating__content",children:"An error occurred during authentication."})]})}),$=()=>c.jsx("div",{className:"oidc-authenticating",children:c.jsxs("div",{className:"oidc-authenticating__container",children:[c.jsx("h1",{className:"oidc-authenticating__title",children:"Authentication in progress"}),c.jsx("p",{className:"oidc-authenticating__content",children:"You will be redirected to the login page."})]})}),G=()=>Math.random().toString(36).slice(2,8),H=(t,n)=>(e,o)=>{if(typeof t.CustomEvent=="function")return new t.CustomEvent(e,o);const r=o||{bubbles:!1,cancelable:!1,detail:void 0},i=n.createEvent("CustomEvent");return i.initCustomEvent(e,r.bubbles,r.cancelable,r.detail),i.prototype=t.Event.prototype,i},Q=(t,n,e)=>({replaceState:(o,r)=>{const i=e(),u=r||t.history.state;t.history.replaceState({key:i,state:u},null,o),t.dispatchEvent(n("popstate"))}}),J=()=>Q(window,H(window,document),G),m=()=>c.jsx("div",{className:"oidc-callback",children:c.jsxs("div",{className:"oidc-callback__container",children:[c.jsx("h1",{className:"oidc-callback__title",children:"Authentication complete"}),c.jsx("p",{className:"oidc-callback__content",children:"You will be redirected to your application."})]})}),V=({callBackError:t,callBackSuccess:n,configurationName:e,withCustomHistory:o})=>{const[r,i]=d.useState(!1);d.useEffect(()=>{let l=!0;return(async()=>{const k=s.OidcClient.get;try{const{callbackPath:O}=await k(e).loginCallbackAsync();(o?o():J()).replaceState(O||"/")}catch(O){l&&(console.warn(O),i(!0))}})(),()=>{l=!1}},[]);const u=t||I,a=n||m;return r?c.jsx(u,{configurationName:e}):c.jsx(a,{configurationName:e})},X=()=>c.jsx("span",{className:"oidc-loading",children:"Loading"}),Z=()=>c.jsx("div",{className:"oidc-serviceworker",children:c.jsxs("div",{className:"oidc-serviceworker__container",children:[c.jsx("h1",{className:"oidc-serviceworker__title",children:"Unable to authenticate on this browser"}),c.jsx("p",{className:"oidc-serviceworker__content",children:"Your browser is not secure enough to make authentication work. Try updating your browser or use a newer browser."})]})}),x=()=>c.jsx("div",{className:"oidc-session-lost",children:c.jsxs("div",{className:"oidc-session-lost__container",children:[c.jsx("h1",{className:"oidc-session-lost__title",children:"Session timed out"}),c.jsx("p",{className:"oidc-session-lost__content",children:"Your session has expired. Please re-authenticate."})]})}),R=({configurationName:t})=>(d.useEffect(()=>{(async()=>{s.OidcClient.get(t).silentLoginCallbackAsync()})().catch(e=>{console.error("Error during silent login callback:",e)})},[t]),null),ee=({configurationName:t})=>{const n=s.getParseQueryStringFromLocation(window.location.href),e=s.OidcClient.get,o=e(t);let r=null;for(const[i,u]of Object.entries(n))i==="state"||i==="scope"||(r===null&&(r={}),r[i]=u);return d.useEffect(()=>{o.tokens||o.loginAsync(null,r,!0,n.scope)},[]),c.jsx(c.Fragment,{})},te=({callbackErrorComponent:t,callbackSuccessComponent:n,redirect_uri:e,silent_redirect_uri:o,silent_login_uri:r,children:i,configurationName:u,withCustomHistory:a=null})=>{const l=window?s.getPath(window.location.href):"",[h,k]=d.useState(l);d.useEffect(()=>{const y=()=>k(s.getPath(window.location.href));return y(),window.addEventListener("popstate",y,!1),()=>window.removeEventListener("popstate",y,!1)},[]);const O=s.getPath(e);return o&&h===s.getPath(o)?c.jsx(R,{configurationName:u}):r&&h===s.getPath(r)?c.jsx(ee,{configurationName:u}):h===O?c.jsx(V,{callBackError:t,callBackSuccess:n,configurationName:u,withCustomHistory:a}):c.jsx(c.Fragment,{children:i})},se=d.memo(te),M={name:"",data:null},ne=({loadingComponent:t,children:n,configurationName:e})=>{const[o,r]=d.useState(!0),i=s.OidcClient.get,u=i(e);d.useEffect(()=>{let l=!0;return u&&u.tryKeepExistingSessionAsync().then(()=>{l&&r(!1)}),()=>{l=!1}},[e]);const a=t;return c.jsx(c.Fragment,{children:o?c.jsx(a,{configurationName:e}):c.jsx(c.Fragment,{children:n})})},p=({isLoading:t,loadingComponent:n,children:e,configurationName:o})=>{const r=n;return t?c.jsx(r,{configurationName:o,children:e}):c.jsx(c.Fragment,{children:e})},ce=({children:t,configuration:n,configurationName:e="default",callbackSuccessComponent:o=m,authenticatingComponent:r=$,loadingComponent:i=X,serviceWorkerNotSupportedComponent:u=Z,authenticatingErrorComponent:a=I,sessionLostComponent:l=x,onSessionLost:h=null,onLogoutFromAnotherTab:k=null,onLogoutFromSameTab:O=null,withCustomHistory:y=null,onEvent:v=null,getFetch:S=null,location:Y=null})=>{if(n&&n.redirect_uri&&n.silent_redirect_uri&&n.redirect_uri===n.silent_redirect_uri)throw new Error("redirect_uri and silent_redirect_uri must be different");const w=(C="default")=>s.OidcClient.getOrCreate(S??s.getFetchDefault,Y??new s.OidcLocation)(n,C),[fe,T]=d.useState(M),[ke,B]=d.useState(e);d.useEffect(()=>{const L=w(e).subscribeEvents((_,g)=>{v&&v(e,_,g)});return()=>{w(e).removeEventSubscription(L)}},[e,v]),d.useEffect(()=>{const L=w(e).subscribeEvents((_,g)=>{if(_===s.OidcClient.eventNames.refreshTokensAsync_error||_===s.OidcClient.eventNames.syncTokensAsync_error){if(h!=null){h();return}T({name:_,data:g})}else if(_===s.OidcClient.eventNames.logout_from_another_tab){if(k!=null){k();return}T({name:_,data:g})}else _===s.OidcClient.eventNames.logout_from_same_tab?O?.():_===s.OidcClient.eventNames.loginAsync_begin||_===s.OidcClient.eventNames.loginCallbackAsync_end||_===s.OidcClient.eventNames.loginAsync_error||_===s.OidcClient.eventNames.loginCallbackAsync_error?T({name:_,data:g}):_===s.OidcClient.eventNames.service_worker_not_supported_by_browser&&n.service_worker_only===!0&&T({name:_,data:g})});return queueMicrotask(()=>{B(e)}),()=>{w(e).removeEventSubscription(L),T(M)}},[n,e]),d.useEffect(()=>{B(e)},[e]);const _e=l,Oe=r,b=i,ve=u,ye=a,A=ke!==e,P=w(e);switch(fe.name){case s.OidcClient.eventNames.service_worker_not_supported_by_browser:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(ve,{configurationName:e})});case s.OidcClient.eventNames.loginAsync_begin:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(Oe,{configurationName:e})});case s.OidcClient.eventNames.loginAsync_error:case s.OidcClient.eventNames.loginCallbackAsync_error:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(ye,{configurationName:e})});case s.OidcClient.eventNames.refreshTokensAsync_error:case s.OidcClient.eventNames.syncTokensAsync_error:case s.OidcClient.eventNames.logout_from_another_tab:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(_e,{configurationName:e})});default:return c.jsx(p,{loadingComponent:b,isLoading:A,configurationName:e,children:c.jsx(se,{redirect_uri:P.configuration.redirect_uri,silent_redirect_uri:P.configuration.silent_redirect_uri,silent_login_uri:P.configuration.silent_login_uri,callbackSuccessComponent:o,callbackErrorComponent:a,authenticatingComponent:r,configurationName:e,withCustomHistory:y,location:Y??new s.OidcLocation,children:c.jsx(ne,{loadingComponent:b,configurationName:e,children:t})})})}},U=({children:t,callbackPath:n=null,extras:e=null,configurationName:o="default"})=>{const r=s.OidcClient.get,i=r(o);return d.useEffect(()=>{i.tokens||i.loginAsync(n,e)},[o,n,e]),i.tokens?c.jsx(c.Fragment,{children:t}):null},oe=(t,n=null,e=null,o="default")=>r=>c.jsx(U,{callbackPath:n,extras:e,configurationName:o,children:c.jsx(t,{...r})}),E="default",q=(t,n)=>{let e=!1;return t(n)&&(e=t(n).tokens!=null),e},re=(t=E)=>{const n=s.OidcClient.get,[e,o]=d.useState(()=>q(n,t));return d.useEffect(()=>{let a=!0;const l=n(t),h=l.subscribeEvents((k,O)=>{(k===s.OidcClient.eventNames.logout_from_another_tab||k===s.OidcClient.eventNames.logout_from_same_tab||k===s.OidcClient.eventNames.token_acquired)&&a&&o(q(n,t))});return()=>{a=!1,l.removeEventSubscription(h)}},[t]),{login:(a=void 0,l=void 0,h=!1,k=void 0)=>n(t).loginAsync(a,l,!1,k,h),logout:(a=void 0,l=void 0)=>n(t).logoutAsync(a,l),renewTokens:async(a=void 0)=>{const l=await n(t).renewTokensAsync(a);return{accessToken:l.accessToken,accessTokenPayload:l.accessTokenPayload,idToken:l.idToken,idTokenPayload:l.idTokenPayload}},isAuthenticated:e}},D={accessToken:null,accessTokenPayload:null},ie=t=>{const n=s.OidcClient.get,e=n(t);if(e.tokens){const o=e.tokens;return{accessToken:o.accessToken,accessTokenPayload:o.accessTokenPayload,generateDemonstrationOfProofOfPossessionAsync:e.configuration.demonstrating_proof_of_possession?(r,i)=>e.generateDemonstrationOfProofOfPossessionAsync(o.accessToken,r,i):null}}return D};function ae(t,n){return t.configuration.demonstrating_proof_of_possession?(e,o,r={})=>t.generateDemonstrationOfProofOfPossessionAsync(n.accessToken,e,o,r):null}const le=(t=E)=>{const n=s.OidcClient.get,[e,o]=d.useState(()=>ie(t));return d.useEffect(()=>{let r=!0;const i=n(t),u=i.subscribeEvents((a,l)=>{if((a===s.OidcClient.eventNames.token_renewed||a===s.OidcClient.eventNames.token_acquired||a===s.OidcClient.eventNames.logout_from_another_tab||a===s.OidcClient.eventNames.logout_from_same_tab||a===s.OidcClient.eventNames.refreshTokensAsync_error||a===s.OidcClient.eventNames.syncTokensAsync_error)&&r){const h=i.tokens;o(h!=null?{accessToken:h.accessToken,accessTokenPayload:h.accessTokenPayload,generateDemonstrationOfProofOfPossessionAsync:ae(i,h)}:D)}});return()=>{r=!1,i.removeEventSubscription(u)}},[t]),e},W={idToken:null,idTokenPayload:null},de=t=>{const n=s.OidcClient.get,e=n(t);if(e.tokens){const o=e.tokens;return{idToken:o.idToken,idTokenPayload:o.idTokenPayload}}return W},ue=(t=E)=>{const n=s.OidcClient.get,[e,o]=d.useState(()=>de(t));return d.useEffect(()=>{let r=!0;const i=n(t),u=i.subscribeEvents((a,l)=>{if((a===s.OidcClient.eventNames.token_renewed||a===s.OidcClient.eventNames.token_acquired||a===s.OidcClient.eventNames.logout_from_another_tab||a===s.OidcClient.eventNames.logout_from_same_tab||a===s.OidcClient.eventNames.refreshTokensAsync_error||a===s.OidcClient.eventNames.syncTokensAsync_error)&&r){const h=i.tokens;o(h!=null?{idToken:h.idToken,idTokenPayload:h.idTokenPayload}:W)}});return()=>{r=!1,i.removeEventSubscription(u)}},[t]),e};var j=(t=>(t.Unauthenticated="Unauthenticated",t.Loading="Loading user",t.Loaded="User loaded",t.LoadingError="Error loading user",t))(j||{});const he=(t="default",n=!1)=>{const o=s.OidcClient.get(t).userInfo(),[r,i]=d.useState({user:o,status:o?"User loaded":"Unauthenticated"}),[u,a]=d.useState(o?1:0),l=d.useRef(o?1:0);d.useEffect(()=>{const k=s.OidcClient.get(t);let O=!0;if(k&&k.tokens){const v=u===l.current;if(v&&k.userInfo())return;l.current=u,queueMicrotask(()=>{O&&i({...r,status:"Loading user"})}),k.userInfoAsync(!v,n).then(S=>{O&&i({user:S,status:"User loaded"})}).catch(()=>i({...r,status:"Error loading user"}))}else queueMicrotask(()=>{O&&i({user:null,status:"Unauthenticated"})});const y=k.subscribeEvents(v=>{(v===s.OidcClient.eventNames.logout_from_another_tab||v===s.OidcClient.eventNames.logout_from_same_tab)&&O&&i({user:null,status:"Unauthenticated"})});return()=>{O=!1,k.removeEventSubscription(y)}},[u,t,n]);const h=()=>{a(u+1)};return{oidcUser:r.user,oidcUserLoadingState:r.status,reloadOidcUser:h}};Object.defineProperty(f,"OidcClient",{enumerable:!0,get:()=>s.OidcClient}),Object.defineProperty(f,"OidcLocation",{enumerable:!0,get:()=>s.OidcLocation}),Object.defineProperty(f,"TokenAutomaticRenewMode",{enumerable:!0,get:()=>s.TokenAutomaticRenewMode}),Object.defineProperty(f,"TokenRenewMode",{enumerable:!0,get:()=>s.TokenRenewMode}),f.OidcProvider=ce,f.OidcSecure=U,f.OidcUserStatus=j,f.useOidc=re,f.useOidcAccessToken=le,f.useOidcFetch=F,f.useOidcIdToken=ue,f.useOidcUser=he,f.withOidcFetch=K,f.withOidcSecure=oe,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axa-fr/react-oidc",
3
- "version": "7.26.5",
3
+ "version": "7.26.7",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.umd.cjs",
@@ -40,8 +40,8 @@
40
40
  "lint": "eslint src"
41
41
  },
42
42
  "dependencies": {
43
- "@axa-fr/oidc-client-service-worker": "7.26.5",
44
- "@axa-fr/oidc-client": "7.26.5"
43
+ "@axa-fr/oidc-client-service-worker": "7.26.7",
44
+ "@axa-fr/oidc-client": "7.26.7"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "react": "^17.0.0 || ^18.0.0 || ^19.0.0"
@@ -0,0 +1,90 @@
1
+ import { act, renderHook, waitFor } from '@testing-library/react';
2
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
3
+
4
+ import { OidcUserStatus, useOidcUser } from './User';
5
+
6
+ const mockSubscribeEvents = vi.fn().mockReturnValue('subscription-id');
7
+ const mockRemoveEventSubscription = vi.fn();
8
+ const mockUserInfo = vi.fn();
9
+ const mockUserInfoAsync = vi.fn();
10
+
11
+ vi.mock('@axa-fr/oidc-client', () => ({
12
+ OidcClient: {
13
+ get: vi.fn(() => ({
14
+ tokens: { access_token: 'test-token' },
15
+ userInfo: mockUserInfo,
16
+ userInfoAsync: mockUserInfoAsync,
17
+ subscribeEvents: mockSubscribeEvents,
18
+ removeEventSubscription: mockRemoveEventSubscription,
19
+ })),
20
+ eventNames: {
21
+ logout_from_another_tab: 'logout_from_another_tab',
22
+ logout_from_same_tab: 'logout_from_same_tab',
23
+ },
24
+ },
25
+ }));
26
+
27
+ describe('useOidcUser', () => {
28
+ beforeEach(() => {
29
+ vi.clearAllMocks();
30
+ mockSubscribeEvents.mockReturnValue('subscription-id');
31
+ mockRemoveEventSubscription.mockReset();
32
+ });
33
+
34
+ it('should load user information and update status correctly on reload', async () => {
35
+ const userInfo = { sub: 'user1', name: 'Test User' };
36
+ // Initial state: no cached user
37
+ mockUserInfo.mockReturnValue(null);
38
+ // userInfoAsync resolves with user info
39
+ mockUserInfoAsync.mockResolvedValue(userInfo);
40
+
41
+ const { result } = renderHook(() => useOidcUser());
42
+
43
+ // Initially unauthenticated since userInfo returns null
44
+ await waitFor(() => {
45
+ expect(result.current.oidcUserLoadingState).toBe(OidcUserStatus.Unauthenticated);
46
+ });
47
+
48
+ // Simulate the user being authenticated (tokens present)
49
+ mockUserInfo.mockReturnValue(userInfo);
50
+
51
+ // Trigger reload
52
+ act(() => {
53
+ result.current.reloadOidcUser();
54
+ });
55
+
56
+ // After reload, should eventually show loaded state (not stay stuck at loading)
57
+ await waitFor(() => {
58
+ expect(result.current.oidcUserLoadingState).toBe(OidcUserStatus.Loaded);
59
+ });
60
+
61
+ expect(result.current.oidcUser).toEqual(userInfo);
62
+ });
63
+
64
+ it('should not get stuck at Loading state after reloadOidcUser is called', async () => {
65
+ const initialUser = { sub: 'user1', name: 'Initial User' };
66
+ const reloadedUser = { sub: 'user1', name: 'Reloaded User' };
67
+
68
+ // User is already authenticated
69
+ mockUserInfo.mockReturnValue(initialUser);
70
+ // userInfoAsync resolves with new data
71
+ mockUserInfoAsync.mockResolvedValue(reloadedUser);
72
+
73
+ const { result } = renderHook(() => useOidcUser());
74
+
75
+ // Initial state: user loaded from cache
76
+ expect(result.current.oidcUserLoadingState).toBe(OidcUserStatus.Loaded);
77
+
78
+ // Trigger reload
79
+ act(() => {
80
+ result.current.reloadOidcUser();
81
+ });
82
+
83
+ // Should complete the reload and show the reloaded user (not stuck at Loading)
84
+ await waitFor(() => {
85
+ expect(result.current.oidcUser).toEqual(reloadedUser);
86
+ });
87
+
88
+ expect(result.current.oidcUserLoadingState).toBe(OidcUserStatus.Loaded);
89
+ });
90
+ });
package/src/User.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { OidcClient, type OidcUserInfo } from '@axa-fr/oidc-client';
2
- import { useEffect, useState } from 'react';
2
+ import { useEffect, useRef, useState } from 'react';
3
3
 
4
4
  export enum OidcUserStatus {
5
5
  Unauthenticated = 'Unauthenticated',
@@ -24,21 +24,21 @@ export const useOidcUser = <T extends OidcUserInfo = OidcUserInfo>(
24
24
  status: user ? OidcUserStatus.Loaded : OidcUserStatus.Unauthenticated,
25
25
  });
26
26
  const [oidcUserId, setOidcUserId] = useState<number>(user ? 1 : 0);
27
- const [oidcPreviousUserId, setPreviousOidcUserId] = useState<number>(user ? 1 : 0);
27
+ const oidcPreviousUserIdRef = useRef<number>(user ? 1 : 0);
28
28
 
29
29
  useEffect(() => {
30
30
  const oidc = OidcClient.get(configurationName);
31
31
  let isMounted = true;
32
32
  if (oidc && oidc.tokens) {
33
- const isCache = oidcUserId === oidcPreviousUserId;
33
+ const isCache = oidcUserId === oidcPreviousUserIdRef.current;
34
34
  if (isCache && oidc.userInfo<T>()) {
35
35
  return;
36
36
  }
37
+ oidcPreviousUserIdRef.current = oidcUserId;
37
38
  // Use queueMicrotask to defer setState to avoid synchronous call in effect
38
39
  queueMicrotask(() => {
39
40
  if (isMounted) {
40
41
  setOidcUser({ ...oidcUser, status: OidcUserStatus.Loading });
41
- setPreviousOidcUserId(oidcUserId);
42
42
  }
43
43
  });
44
44
  oidc
@@ -71,7 +71,7 @@ export const useOidcUser = <T extends OidcUserInfo = OidcUserInfo>(
71
71
  isMounted = false;
72
72
  oidc.removeEventSubscription(newSubscriptionId);
73
73
  };
74
- }, [oidcUserId, configurationName, demonstrating_proof_of_possession, oidcPreviousUserId]);
74
+ }, [oidcUserId, configurationName, demonstrating_proof_of_possession]);
75
75
 
76
76
  const reloadOidcUser = () => {
77
77
  setOidcUserId(oidcUserId + 1);