@arkitektbedriftene/fe-lib 7.2.0 → 7.3.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.
@@ -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, n) => e in t ? E(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
3
+ var f = (t, e, n) => v(t, typeof e != "symbol" ? e + "" : e, n);
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, n] = 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
- r({ accessToken: o, userInfo: i });
28
+ n({ 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: n } = window.location, s = new URLSearchParams(n).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, n) {
74
+ f(this, "userManager");
75
+ f(this, "storageKey", "impersonationState");
76
+ f(this, "meEndpoint");
77
+ this.userManager = new L(e), this.meEndpoint = n;
77
78
  }
78
79
  /**
79
80
  * Get impersonation user from localStorage
@@ -82,11 +83,11 @@ class W {
82
83
  try {
83
84
  const e = localStorage.getItem(this.storageKey);
84
85
  if (e) {
85
- const r = JSON.parse(e);
86
- if (r.impersonationToken && r.impersonatedUser) {
87
- if (Math.floor(Date.now() / 1e3) >= r.impersonatedUser.exp)
86
+ const n = JSON.parse(e);
87
+ if (n.impersonationToken && n.impersonatedUser) {
88
+ if (Math.floor(Date.now() / 1e3) >= n.impersonatedUser.exp)
88
89
  throw new Error("Impersonation token has expired");
89
- return r;
90
+ return n;
90
91
  }
91
92
  }
92
93
  } catch (e) {
@@ -105,10 +106,10 @@ class W {
105
106
  profile: e.impersonatedUser,
106
107
  impersonating: !0
107
108
  };
108
- const r = await this.userManager.getUser();
109
- return !r || r.expired ? null : {
110
- access_token: r.access_token,
111
- profile: r.profile,
109
+ const n = await this.userManager.getUser();
110
+ return !n || n.expired ? null : {
111
+ access_token: n.access_token,
112
+ profile: n.profile,
112
113
  impersonating: !1
113
114
  };
114
115
  }
@@ -132,15 +133,20 @@ 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
+ "Content-Type": "application/json",
142
+ Authorization: `Bearer ${e}`
143
+ }
144
+ })).json(),
139
145
  impersonationToken: e
140
146
  };
141
- localStorage.setItem(this.storageKey, JSON.stringify(n));
142
- } catch (r) {
143
- console.error("Failed to decode impersonation token:", r);
147
+ localStorage.setItem(this.storageKey, JSON.stringify(o));
148
+ } catch (n) {
149
+ console.error("Failed to set impersonation token:", n);
144
150
  }
145
151
  }
146
152
  /**
@@ -152,48 +158,48 @@ class W {
152
158
  /**
153
159
  * Handle impersonation callback from URL parameters
154
160
  */
155
- handleImpersonationCallback(e) {
156
- const n = new URL(e).searchParams.get("impersonateAccessToken");
157
- if (!n)
161
+ async handleImpersonationCallback(e) {
162
+ const s = new URL(e).searchParams.get("impersonateAccessToken");
163
+ if (!s)
158
164
  throw new Error("Missing url param 'impersonateAccessToken'");
159
- this.setImpersonation(n);
165
+ return this.setImpersonation(s);
160
166
  }
161
167
  }
162
168
  const O = ({
163
169
  userManager: t,
164
170
  context: e,
165
- children: r
171
+ children: n
166
172
  }) => {
167
- const [n, o] = L({
173
+ const [s, o] = R({
168
174
  user: null,
169
175
  isLoading: !0,
170
176
  isAuthenticated: !1,
171
177
  isError: !1,
172
178
  error: null
173
179
  }), i = x(!1);
174
- f(() => {
180
+ h(() => {
175
181
  i.current || (i.current = !0, (async () => {
176
182
  try {
177
- const s = await t.getUser();
183
+ const r = await t.getUser();
178
184
  o({
179
- user: s,
185
+ user: r,
180
186
  isLoading: !1,
181
- isAuthenticated: s ? !s.expired : !1,
187
+ isAuthenticated: r ? !r.expired : !1,
182
188
  isError: !1,
183
189
  error: null
184
190
  });
185
- } catch (s) {
191
+ } catch (r) {
186
192
  o({
187
193
  user: null,
188
194
  isLoading: !1,
189
195
  isAuthenticated: !1,
190
196
  isError: !0,
191
- error: s instanceof Error ? s : new Error("Unknown error during auth")
197
+ error: r instanceof Error ? r : new Error("Unknown error during auth")
192
198
  });
193
199
  }
194
200
  })());
195
- }, [t]), f(() => {
196
- const s = (p) => {
201
+ }, [t]), h(() => {
202
+ const r = (p) => {
197
203
  o({
198
204
  user: p,
199
205
  isLoading: !1,
@@ -202,10 +208,10 @@ const O = ({
202
208
  error: null
203
209
  });
204
210
  };
205
- t.events.addUserLoaded(s);
211
+ t.events.addUserLoaded(r);
206
212
  const c = () => {
207
213
  o({
208
- ...n,
214
+ ...s,
209
215
  user: null,
210
216
  isAuthenticated: !1
211
217
  });
@@ -213,41 +219,41 @@ const O = ({
213
219
  t.events.addUserUnloaded(c);
214
220
  const S = (p) => {
215
221
  o({
216
- ...n,
222
+ ...s,
217
223
  isLoading: !1,
218
224
  isError: !0,
219
225
  error: p
220
226
  });
221
227
  };
222
228
  return t.events.addSilentRenewError(S), () => {
223
- t.events.removeUserLoaded(s), t.events.removeUserUnloaded(c), t.events.removeSilentRenewError(S);
229
+ t.events.removeUserLoaded(r), t.events.removeUserUnloaded(c), t.events.removeSilentRenewError(S);
224
230
  };
225
231
  }, [t]);
226
- const u = d(async () => {
227
- const s = await t.signinCallback();
232
+ const u = m(async () => {
233
+ const r = await t.signinCallback();
228
234
  return o({
229
- user: s ?? null,
235
+ user: r ?? null,
230
236
  isLoading: !1,
231
- isAuthenticated: s ? !s.expired : !1,
237
+ isAuthenticated: r ? !r.expired : !1,
232
238
  isError: !1,
233
239
  error: null
234
- }), s ?? void 0;
235
- }, [t]), a = d(
236
- async (s) => {
240
+ }), r ?? void 0;
241
+ }, [t]), a = m(
242
+ async (r) => {
237
243
  try {
238
- await t.signinRedirect(s);
244
+ await t.signinRedirect(r);
239
245
  } catch (c) {
240
246
  console.error(c);
241
247
  }
242
248
  },
243
249
  [t]
244
- ), l = d(
245
- async (s) => await t.signinPopup(s),
250
+ ), l = m(
251
+ async (r) => await t.signinPopup(r),
246
252
  [t]
247
- ), m = d(
248
- async (s) => {
253
+ ), d = m(
254
+ async (r) => {
249
255
  try {
250
- return await t.signinSilent(s);
256
+ return await t.signinSilent(r);
251
257
  } catch (c) {
252
258
  return o({
253
259
  user: null,
@@ -259,36 +265,36 @@ const O = ({
259
265
  }
260
266
  },
261
267
  [t]
262
- ), h = d(
263
- async (s) => {
268
+ ), g = m(
269
+ async (r) => {
264
270
  try {
265
- await t.signoutRedirect(s);
271
+ await t.signoutRedirect(r);
266
272
  } catch (c) {
267
273
  console.error(c);
268
274
  }
269
275
  },
270
276
  [t]
271
- ), C = U(
277
+ ), C = k(
272
278
  () => ({
273
- state: n,
279
+ state: s,
274
280
  handleSigninCallback: u,
275
281
  redirectToSignin: a,
276
- signinSilent: m,
282
+ signinSilent: d,
277
283
  signinPopup: l,
278
- logout: h
284
+ logout: g
279
285
  }),
280
286
  [
281
- n,
287
+ s,
282
288
  u,
283
289
  a,
284
- m,
290
+ d,
285
291
  l,
286
- h
292
+ g
287
293
  ]
288
294
  );
289
- return /* @__PURE__ */ w(e.Provider, { value: C, children: r });
295
+ return /* @__PURE__ */ w(e.Provider, { value: C, children: n });
290
296
  }, A = (t) => {
291
- const e = k(t);
297
+ const e = U(t);
292
298
  if (!e)
293
299
  throw new Error("useAuthContext must be used within an AuthProvider");
294
300
  return e;
@@ -296,15 +302,15 @@ const O = ({
296
302
  const { state: e } = A(t);
297
303
  return e;
298
304
  }, 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(
305
+ const { state: n, handleSigninCallback: s } = A(t), o = x(!1);
306
+ return h(() => {
307
+ o.current || (o.current = !0, s().then(
302
308
  (i) => new Promise(
303
309
  (u) => setTimeout(() => u(i), 0)
304
310
  )
305
311
  ).then((i) => e == null ? void 0 : e(i)));
306
- }, [n]), r;
307
- }, X = (t) => {
312
+ }, [s]), n;
313
+ }, W = (t) => {
308
314
  const e = I(null);
309
315
  return {
310
316
  AuthContext: e,
@@ -319,13 +325,13 @@ const O = ({
319
325
  };
320
326
  };
321
327
  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,
328
+ Q as AuthManager,
329
+ q as ImpersonationContextProvider,
330
+ $ as ROLESAdminInCompany,
331
+ H as ROLESAnsatt,
332
+ G as ROLESMAKSAdministrator,
333
+ W as createAuthContext,
328
334
  B as parseUserProfileFCString,
329
- q as useImpersonationCallback,
335
+ V as useImpersonationCallback,
330
336
  b as useImpersonationContext
331
337
  };
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.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./oidc": {