@arkitektbedriftene/fe-lib 7.2.0 → 7.3.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.
@@ -22,7 +22,8 @@ export type ImpersonationData = {
22
22
  export declare class AuthManager {
23
23
  private userManager;
24
24
  private storageKey;
25
- constructor(settings: UserManagerSettings);
25
+ private meEndpoint;
26
+ constructor(settings: UserManagerSettings, meEndpoint: string);
26
27
  /**
27
28
  * Get impersonation user from localStorage
28
29
  */
@@ -43,7 +44,7 @@ export declare class AuthManager {
43
44
  /**
44
45
  * Set impersonation with access token
45
46
  */
46
- setImpersonation(accessToken: string): void;
47
+ setImpersonation(accessToken: string): Promise<void>;
47
48
  /**
48
49
  * Clear impersonation and return to normal user
49
50
  */
@@ -51,5 +52,5 @@ export declare class AuthManager {
51
52
  /**
52
53
  * Handle impersonation callback from URL parameters
53
54
  */
54
- handleImpersonationCallback(urlStr: string): void;
55
+ handleImpersonationCallback(urlStr: string): Promise<void>;
55
56
  }
package/dist/oidc.es.js CHANGED
@@ -1,79 +1,80 @@
1
- var v = Object.defineProperty;
2
- var R = (t, e, r) => e in t ? v(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
- var g = (t, e, r) => R(t, typeof e != "symbol" ? e + "" : e, r);
1
+ var E = Object.defineProperty;
2
+ var v = (t, e, r) => e in t ? E(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var f = (t, e, r) => v(t, typeof e != "symbol" ? e + "" : e, r);
4
4
  import { jsx as w } from "react/jsx-runtime";
5
- import { createContext as I, useContext as k, useMemo as U, useEffect as f, useState as L, useRef as x, useCallback as d } from "react";
6
- import { UserManager as P } from "oidc-client-ts";
5
+ import { createContext as I, useContext as U, useMemo as k, useEffect as h, useState as R, useRef as x, useCallback as m } from "react";
6
+ import { UserManager as L } from "oidc-client-ts";
7
7
  export * from "oidc-client-ts";
8
- import T from "use-local-storage-state";
9
- import { jwtDecode as y } from "jwt-decode";
10
- const E = I(
8
+ import P from "use-local-storage-state";
9
+ import { jwtDecode as T } from "jwt-decode";
10
+ const y = I(
11
11
  null
12
12
  ), b = () => {
13
- const t = k(E);
13
+ const t = U(y);
14
14
  if (!t)
15
15
  throw new Error(
16
16
  "useImpersonationContext must be used within a ImpersonationContextProvider"
17
17
  );
18
18
  return t;
19
- }, V = ({
19
+ }, q = ({
20
20
  children: t
21
21
  }) => {
22
- const [e, r] = T("impersonationState", {
22
+ const [e, r] = P("impersonationState", {
23
23
  defaultValue: {}
24
- }), n = U(() => ({
24
+ }), s = k(() => ({
25
25
  accessToken: e == null ? void 0 : e.accessToken,
26
26
  userInfo: e == null ? void 0 : e.userInfo,
27
27
  setImpersonation: (o, i) => {
28
28
  r({ accessToken: o, userInfo: i });
29
29
  }
30
30
  }), []);
31
- return /* @__PURE__ */ w(E.Provider, { value: n, children: t });
32
- }, q = (t) => {
31
+ return /* @__PURE__ */ w(y.Provider, { value: s, children: t });
32
+ }, V = (t) => {
33
33
  const { setImpersonation: e } = b();
34
- f(() => {
35
- const { search: r } = window.location, n = new URLSearchParams(r).get(
34
+ h(() => {
35
+ const { search: r } = window.location, s = new URLSearchParams(r).get(
36
36
  "impersonateAccessToken"
37
37
  );
38
- if (n && e) {
39
- const o = y(n);
40
- e(n, o), t();
38
+ if (s && e) {
39
+ const o = T(s);
40
+ e(s, o), t();
41
41
  }
42
42
  }, []);
43
43
  };
44
44
  function B(t) {
45
- return JSON.parse(t).map((n) => {
45
+ return JSON.parse(t).map((s) => {
46
46
  const [
47
47
  o,
48
48
  i,
49
49
  u,
50
50
  a,
51
51
  l
52
- ] = n.split("|"), m = a.split(",");
52
+ ] = s.split("|"), d = a.split(",");
53
53
  return {
54
54
  CustomerExternalId: o,
55
55
  CustomerFirmId: i,
56
56
  CustomerFirmName: u,
57
- CustomerUserRoles: m,
57
+ CustomerUserRoles: d,
58
58
  CustomerUserIsAdmin: l === "True" || l === "1"
59
59
  };
60
60
  });
61
61
  }
62
- const G = [
62
+ const $ = [
63
63
  "97",
64
64
  "131",
65
65
  "132",
66
66
  "141"
67
- ], H = [
67
+ ], G = [
68
68
  "3"
69
- ], Q = [
69
+ ], H = [
70
70
  "0"
71
71
  ];
72
- class W {
73
- constructor(e) {
74
- g(this, "userManager");
75
- g(this, "storageKey", "impersonationState");
76
- this.userManager = new P(e);
72
+ class Q {
73
+ constructor(e, r) {
74
+ f(this, "userManager");
75
+ f(this, "storageKey", "impersonationState");
76
+ f(this, "meEndpoint");
77
+ this.userManager = new L(e), this.meEndpoint = r;
77
78
  }
78
79
  /**
79
80
  * Get impersonation user from localStorage
@@ -132,15 +133,19 @@ class W {
132
133
  /**
133
134
  * Set impersonation with access token
134
135
  */
135
- setImpersonation(e) {
136
+ async setImpersonation(e) {
136
137
  try {
137
- const n = {
138
- impersonatedUser: y(e),
138
+ const o = {
139
+ impersonatedUser: await (await fetch(this.meEndpoint, {
140
+ headers: {
141
+ Authorization: `Bearer ${e}`
142
+ }
143
+ })).json(),
139
144
  impersonationToken: e
140
145
  };
141
- localStorage.setItem(this.storageKey, JSON.stringify(n));
146
+ localStorage.setItem(this.storageKey, JSON.stringify(o));
142
147
  } catch (r) {
143
- console.error("Failed to decode impersonation token:", r);
148
+ console.error("Failed to set impersonation token:", r);
144
149
  }
145
150
  }
146
151
  /**
@@ -152,11 +157,11 @@ class W {
152
157
  /**
153
158
  * Handle impersonation callback from URL parameters
154
159
  */
155
- handleImpersonationCallback(e) {
156
- const n = new URL(e).searchParams.get("impersonateAccessToken");
157
- if (!n)
160
+ async handleImpersonationCallback(e) {
161
+ const s = new URL(e).searchParams.get("impersonateAccessToken");
162
+ if (!s)
158
163
  throw new Error("Missing url param 'impersonateAccessToken'");
159
- this.setImpersonation(n);
164
+ return this.setImpersonation(s);
160
165
  }
161
166
  }
162
167
  const O = ({
@@ -164,36 +169,36 @@ const O = ({
164
169
  context: e,
165
170
  children: r
166
171
  }) => {
167
- const [n, o] = L({
172
+ const [s, o] = R({
168
173
  user: null,
169
174
  isLoading: !0,
170
175
  isAuthenticated: !1,
171
176
  isError: !1,
172
177
  error: null
173
178
  }), i = x(!1);
174
- f(() => {
179
+ h(() => {
175
180
  i.current || (i.current = !0, (async () => {
176
181
  try {
177
- const s = await t.getUser();
182
+ const n = await t.getUser();
178
183
  o({
179
- user: s,
184
+ user: n,
180
185
  isLoading: !1,
181
- isAuthenticated: s ? !s.expired : !1,
186
+ isAuthenticated: n ? !n.expired : !1,
182
187
  isError: !1,
183
188
  error: null
184
189
  });
185
- } catch (s) {
190
+ } catch (n) {
186
191
  o({
187
192
  user: null,
188
193
  isLoading: !1,
189
194
  isAuthenticated: !1,
190
195
  isError: !0,
191
- error: s instanceof Error ? s : new Error("Unknown error during auth")
196
+ error: n instanceof Error ? n : new Error("Unknown error during auth")
192
197
  });
193
198
  }
194
199
  })());
195
- }, [t]), f(() => {
196
- const s = (p) => {
200
+ }, [t]), h(() => {
201
+ const n = (p) => {
197
202
  o({
198
203
  user: p,
199
204
  isLoading: !1,
@@ -202,10 +207,10 @@ const O = ({
202
207
  error: null
203
208
  });
204
209
  };
205
- t.events.addUserLoaded(s);
210
+ t.events.addUserLoaded(n);
206
211
  const c = () => {
207
212
  o({
208
- ...n,
213
+ ...s,
209
214
  user: null,
210
215
  isAuthenticated: !1
211
216
  });
@@ -213,41 +218,41 @@ const O = ({
213
218
  t.events.addUserUnloaded(c);
214
219
  const S = (p) => {
215
220
  o({
216
- ...n,
221
+ ...s,
217
222
  isLoading: !1,
218
223
  isError: !0,
219
224
  error: p
220
225
  });
221
226
  };
222
227
  return t.events.addSilentRenewError(S), () => {
223
- t.events.removeUserLoaded(s), t.events.removeUserUnloaded(c), t.events.removeSilentRenewError(S);
228
+ t.events.removeUserLoaded(n), t.events.removeUserUnloaded(c), t.events.removeSilentRenewError(S);
224
229
  };
225
230
  }, [t]);
226
- const u = d(async () => {
227
- const s = await t.signinCallback();
231
+ const u = m(async () => {
232
+ const n = await t.signinCallback();
228
233
  return o({
229
- user: s ?? null,
234
+ user: n ?? null,
230
235
  isLoading: !1,
231
- isAuthenticated: s ? !s.expired : !1,
236
+ isAuthenticated: n ? !n.expired : !1,
232
237
  isError: !1,
233
238
  error: null
234
- }), s ?? void 0;
235
- }, [t]), a = d(
236
- async (s) => {
239
+ }), n ?? void 0;
240
+ }, [t]), a = m(
241
+ async (n) => {
237
242
  try {
238
- await t.signinRedirect(s);
243
+ await t.signinRedirect(n);
239
244
  } catch (c) {
240
245
  console.error(c);
241
246
  }
242
247
  },
243
248
  [t]
244
- ), l = d(
245
- async (s) => await t.signinPopup(s),
249
+ ), l = m(
250
+ async (n) => await t.signinPopup(n),
246
251
  [t]
247
- ), m = d(
248
- async (s) => {
252
+ ), d = m(
253
+ async (n) => {
249
254
  try {
250
- return await t.signinSilent(s);
255
+ return await t.signinSilent(n);
251
256
  } catch (c) {
252
257
  return o({
253
258
  user: null,
@@ -259,36 +264,36 @@ const O = ({
259
264
  }
260
265
  },
261
266
  [t]
262
- ), h = d(
263
- async (s) => {
267
+ ), g = m(
268
+ async (n) => {
264
269
  try {
265
- await t.signoutRedirect(s);
270
+ await t.signoutRedirect(n);
266
271
  } catch (c) {
267
272
  console.error(c);
268
273
  }
269
274
  },
270
275
  [t]
271
- ), C = U(
276
+ ), C = k(
272
277
  () => ({
273
- state: n,
278
+ state: s,
274
279
  handleSigninCallback: u,
275
280
  redirectToSignin: a,
276
- signinSilent: m,
281
+ signinSilent: d,
277
282
  signinPopup: l,
278
- logout: h
283
+ logout: g
279
284
  }),
280
285
  [
281
- n,
286
+ s,
282
287
  u,
283
288
  a,
284
- m,
289
+ d,
285
290
  l,
286
- h
291
+ g
287
292
  ]
288
293
  );
289
294
  return /* @__PURE__ */ w(e.Provider, { value: C, children: r });
290
295
  }, A = (t) => {
291
- const e = k(t);
296
+ const e = U(t);
292
297
  if (!e)
293
298
  throw new Error("useAuthContext must be used within an AuthProvider");
294
299
  return e;
@@ -296,15 +301,15 @@ const O = ({
296
301
  const { state: e } = A(t);
297
302
  return e;
298
303
  }, K = (t, e) => {
299
- const { state: r, handleSigninCallback: n } = A(t), o = x(!1);
300
- return f(() => {
301
- o.current || (o.current = !0, n().then(
304
+ const { state: r, handleSigninCallback: s } = A(t), o = x(!1);
305
+ return h(() => {
306
+ o.current || (o.current = !0, s().then(
302
307
  (i) => new Promise(
303
308
  (u) => setTimeout(() => u(i), 0)
304
309
  )
305
310
  ).then((i) => e == null ? void 0 : e(i)));
306
- }, [n]), r;
307
- }, X = (t) => {
311
+ }, [s]), r;
312
+ }, W = (t) => {
308
313
  const e = I(null);
309
314
  return {
310
315
  AuthContext: e,
@@ -319,13 +324,13 @@ const O = ({
319
324
  };
320
325
  };
321
326
  export {
322
- W as AuthManager,
323
- V as ImpersonationContextProvider,
324
- G as ROLESAdminInCompany,
325
- Q as ROLESAnsatt,
326
- H as ROLESMAKSAdministrator,
327
- X as createAuthContext,
327
+ Q as AuthManager,
328
+ q as ImpersonationContextProvider,
329
+ $ as ROLESAdminInCompany,
330
+ H as ROLESAnsatt,
331
+ G as ROLESMAKSAdministrator,
332
+ W as createAuthContext,
328
333
  B as parseUserProfileFCString,
329
- q as useImpersonationCallback,
334
+ V as useImpersonationCallback,
330
335
  b as useImpersonationContext
331
336
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkitektbedriftene/fe-lib",
3
- "version": "7.2.0",
3
+ "version": "7.3.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./oidc": {