@versini/auth-provider 2.0.0 → 2.0.2

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.
Files changed (2) hide show
  1. package/dist/index.js +252 -161
  2. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
- import { jsx as V } from "react/jsx-runtime";
2
- import { useCallback as p, useState as U, useEffect as w, useRef as _, useLayoutEffect as z, createContext as M, useContext as F } from "react";
1
+ import { jsx as z } from "react/jsx-runtime";
2
+ import { useCallback as w, useState as x, useEffect as f, useRef as R, useLayoutEffect as F, createContext as M, useContext as H } from "react";
3
3
  /*!
4
- @versini/auth-provider v2.0.0
4
+ @versini/auth-provider v2.0.2
5
5
  © 2024 gizmette.com
6
6
  */
7
7
  try {
8
8
  window.__VERSINI_AUTH_CLIENT__ || (window.__VERSINI_AUTH_CLIENT__ = {
9
- version: "2.0.0",
10
- buildTime: "06/17/2024 04:39 PM EDT",
9
+ version: "2.0.2",
10
+ buildTime: "06/20/2024 03:39 AM EDT",
11
11
  homepage: "https://github.com/aversini/auth-client",
12
12
  license: "MIT"
13
13
  });
@@ -20,238 +20,329 @@ try {
20
20
  try {
21
21
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
22
22
  version: "2.0.0",
23
- buildTime: "06/17/2024 04:39 PM EDT",
23
+ buildTime: "06/20/2024 03:38 AM EDT",
24
24
  homepage: "https://github.com/aversini/auth-client",
25
25
  license: "MIT"
26
26
  });
27
27
  } catch {
28
28
  }
29
- const H = {
29
+ const X = {
30
30
  ID_TOKEN: "id_token"
31
- }, j = {
31
+ }, G = {
32
32
  CLIENT_ID: "X-Auth-ClientId"
33
33
  };
34
- var D = typeof window < "u" ? z : w;
35
- function N(e, t, n, r) {
36
- const c = _(t);
37
- D(() => {
38
- c.current = t;
39
- }, [t]), w(() => {
40
- const l = window;
41
- if (!(l && l.addEventListener))
34
+ var j = typeof window < "u" ? F : f;
35
+ function b(t, e, o, n) {
36
+ const c = R(e);
37
+ j(() => {
38
+ c.current = e;
39
+ }, [e]), f(() => {
40
+ const d = window;
41
+ if (!(d && d.addEventListener))
42
42
  return;
43
43
  const u = (h) => {
44
44
  c.current(h);
45
45
  };
46
- return l.addEventListener(e, u, r), () => {
47
- l.removeEventListener(e, u, r);
46
+ return d.addEventListener(t, u, n), () => {
47
+ d.removeEventListener(t, u, n);
48
48
  };
49
- }, [e, n, r]);
49
+ }, [t, o, n]);
50
50
  }
51
- function A(e) {
52
- const t = _(() => {
51
+ function A(t) {
52
+ const e = R(() => {
53
53
  throw new Error("Cannot call an event handler while rendering.");
54
54
  });
55
- return D(() => {
56
- t.current = e;
57
- }, [e]), p((...n) => {
58
- var r;
59
- return (r = t.current) == null ? void 0 : r.call(t, ...n);
60
- }, [t]);
55
+ return j(() => {
56
+ e.current = t;
57
+ }, [t]), w((...o) => {
58
+ var n;
59
+ return (n = e.current) == null ? void 0 : n.call(e, ...o);
60
+ }, [e]);
61
61
  }
62
- var I = typeof window > "u";
63
- function E(e, t, n = {}) {
64
- const { initializeWithValue: r = !0 } = n, c = p(
65
- (o) => n.serializer ? n.serializer(o) : JSON.stringify(o),
66
- [n]
67
- ), l = p(
68
- (o) => {
69
- if (n.deserializer)
70
- return n.deserializer(o);
71
- if (o === "undefined")
62
+ var k = typeof window > "u";
63
+ function S(t, e, o = {}) {
64
+ const { initializeWithValue: n = !0 } = o, c = w(
65
+ (r) => o.serializer ? o.serializer(r) : JSON.stringify(r),
66
+ [o]
67
+ ), d = w(
68
+ (r) => {
69
+ if (o.deserializer)
70
+ return o.deserializer(r);
71
+ if (r === "undefined")
72
72
  return;
73
- const a = t instanceof Function ? t() : t;
73
+ const a = e instanceof Function ? e() : e;
74
74
  let g;
75
75
  try {
76
- g = JSON.parse(o);
77
- } catch (v) {
78
- return console.error("Error parsing JSON:", v), a;
76
+ g = JSON.parse(r);
77
+ } catch (y) {
78
+ return console.error("Error parsing JSON:", y), a;
79
79
  }
80
80
  return g;
81
81
  },
82
- [n, t]
83
- ), u = p(() => {
84
- const o = t instanceof Function ? t() : t;
85
- if (I)
86
- return o;
82
+ [o, e]
83
+ ), u = w(() => {
84
+ const r = e instanceof Function ? e() : e;
85
+ if (k)
86
+ return r;
87
87
  try {
88
- const a = window.localStorage.getItem(e);
89
- return a ? l(a) : o;
88
+ const a = window.localStorage.getItem(t);
89
+ return a ? d(a) : r;
90
90
  } catch (a) {
91
- return console.warn(`Error reading localStorage key “${e}”:`, a), o;
91
+ return console.warn(`Error reading localStorage key “${t}”:`, a), r;
92
92
  }
93
- }, [t, e, l]), [h, T] = U(() => r ? u() : t instanceof Function ? t() : t), m = A((o) => {
94
- I && console.warn(
95
- `Tried setting localStorage key “${e}” even though environment is not a client`
93
+ }, [e, t, d]), [h, p] = x(() => n ? u() : e instanceof Function ? e() : e), _ = A((r) => {
94
+ k && console.warn(
95
+ `Tried setting localStorage key “${t}” even though environment is not a client`
96
96
  );
97
97
  try {
98
- const a = o instanceof Function ? o(u()) : o;
99
- window.localStorage.setItem(e, c(a)), T(a), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
98
+ const a = r instanceof Function ? r(u()) : r;
99
+ window.localStorage.setItem(t, c(a)), p(a), window.dispatchEvent(new StorageEvent("local-storage", { key: t }));
100
100
  } catch (a) {
101
- console.warn(`Error setting localStorage key “${e}”:`, a);
101
+ console.warn(`Error setting localStorage key “${t}”:`, a);
102
102
  }
103
103
  }), i = A(() => {
104
- I && console.warn(
105
- `Tried removing localStorage key “${e}” even though environment is not a client`
104
+ k && console.warn(
105
+ `Tried removing localStorage key “${t}” even though environment is not a client`
106
106
  );
107
- const o = t instanceof Function ? t() : t;
108
- window.localStorage.removeItem(e), T(o), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
107
+ const r = e instanceof Function ? e() : e;
108
+ window.localStorage.removeItem(t), p(r), window.dispatchEvent(new StorageEvent("local-storage", { key: t }));
109
109
  });
110
- w(() => {
111
- T(u());
112
- }, [e]);
113
- const y = p(
114
- (o) => {
115
- o.key && o.key !== e || T(u());
110
+ f(() => {
111
+ p(u());
112
+ }, [t]);
113
+ const v = w(
114
+ (r) => {
115
+ r.key && r.key !== t || p(u());
116
116
  },
117
- [e, u]
117
+ [t, u]
118
118
  );
119
- return N("storage", y), N("local-storage", y), [h, m, i];
119
+ return b("storage", v), b("local-storage", v), [h, _, i];
120
+ }
121
+ new TextEncoder();
122
+ const L = new TextDecoder(), K = (t) => {
123
+ const e = atob(t), o = new Uint8Array(e.length);
124
+ for (let n = 0; n < e.length; n++)
125
+ o[n] = e.charCodeAt(n);
126
+ return o;
127
+ }, B = (t) => {
128
+ let e = t;
129
+ e instanceof Uint8Array && (e = L.decode(e)), e = e.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
130
+ try {
131
+ return K(e);
132
+ } catch {
133
+ throw new TypeError("The input to be decoded is not correctly encoded.");
134
+ }
135
+ };
136
+ class Y extends Error {
137
+ static get code() {
138
+ return "ERR_JOSE_GENERIC";
139
+ }
140
+ constructor(e) {
141
+ var o;
142
+ super(e), this.code = "ERR_JOSE_GENERIC", this.name = this.constructor.name, (o = Error.captureStackTrace) == null || o.call(Error, this, this.constructor);
143
+ }
144
+ }
145
+ class T extends Y {
146
+ constructor() {
147
+ super(...arguments), this.code = "ERR_JWT_INVALID";
148
+ }
149
+ static get code() {
150
+ return "ERR_JWT_INVALID";
151
+ }
152
+ }
153
+ function q(t) {
154
+ return typeof t == "object" && t !== null;
120
155
  }
121
- const J = "Oops! It looks like your session has expired. For your security, please log in again to continue.", X = "You forgot to wrap your component in <AuthProvider>.", R = {
156
+ function Q(t) {
157
+ if (!q(t) || Object.prototype.toString.call(t) !== "[object Object]")
158
+ return !1;
159
+ if (Object.getPrototypeOf(t) === null)
160
+ return !0;
161
+ let e = t;
162
+ for (; Object.getPrototypeOf(e) !== null; )
163
+ e = Object.getPrototypeOf(e);
164
+ return Object.getPrototypeOf(t) === e;
165
+ }
166
+ const Z = B;
167
+ function N(t) {
168
+ if (typeof t != "string")
169
+ throw new T("JWTs must use Compact JWS serialization, JWT must be a string");
170
+ const { 1: e, length: o } = t.split(".");
171
+ if (o === 5)
172
+ throw new T("Only JWTs using Compact JWS serialization can be decoded");
173
+ if (o !== 3)
174
+ throw new T("Invalid JWT");
175
+ if (!e)
176
+ throw new T("JWTs must contain a payload");
177
+ let n;
178
+ try {
179
+ n = Z(e);
180
+ } catch {
181
+ throw new T("Failed to base64url decode the payload");
182
+ }
183
+ let c;
184
+ try {
185
+ c = JSON.parse(L.decode(n));
186
+ } catch {
187
+ throw new T("Failed to parse the decoded payload as JSON");
188
+ }
189
+ if (!Q(c))
190
+ throw new T("Invalid JWT Claims Set");
191
+ return c;
192
+ }
193
+ const C = "Oops! It looks like your session has expired. For your security, please log in again to continue.", ee = "You forgot to wrap your component in <AuthProvider>.", J = {
122
194
  dev: "https://auth.gizmette.local.com:3003",
123
- prod: "https://auth.gizmette.com"
195
+ prod: "https://mylogin.gizmette.com"
124
196
  };
125
197
  var s = [];
126
- for (var f = 0; f < 256; ++f)
127
- s.push((f + 256).toString(16).slice(1));
128
- function K(e, t = 0) {
129
- return (s[e[t + 0]] + s[e[t + 1]] + s[e[t + 2]] + s[e[t + 3]] + "-" + s[e[t + 4]] + s[e[t + 5]] + "-" + s[e[t + 6]] + s[e[t + 7]] + "-" + s[e[t + 8]] + s[e[t + 9]] + "-" + s[e[t + 10]] + s[e[t + 11]] + s[e[t + 12]] + s[e[t + 13]] + s[e[t + 14]] + s[e[t + 15]]).toLowerCase();
198
+ for (var O = 0; O < 256; ++O)
199
+ s.push((O + 256).toString(16).slice(1));
200
+ function te(t, e = 0) {
201
+ return (s[t[e + 0]] + s[t[e + 1]] + s[t[e + 2]] + s[t[e + 3]] + "-" + s[t[e + 4]] + s[t[e + 5]] + "-" + s[t[e + 6]] + s[t[e + 7]] + "-" + s[t[e + 8]] + s[t[e + 9]] + "-" + s[t[e + 10]] + s[t[e + 11]] + s[t[e + 12]] + s[t[e + 13]] + s[t[e + 14]] + s[t[e + 15]]).toLowerCase();
130
202
  }
131
- var k, W = new Uint8Array(16);
132
- function Y() {
133
- if (!k && (k = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !k))
203
+ var m, oe = new Uint8Array(16);
204
+ function ne() {
205
+ if (!m && (m = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !m))
134
206
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
135
- return k(W);
207
+ return m(oe);
136
208
  }
137
- var q = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
138
- const C = {
139
- randomUUID: q
209
+ var re = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
210
+ const D = {
211
+ randomUUID: re
140
212
  };
141
- function B(e, t, n) {
142
- if (C.randomUUID && !t && !e)
143
- return C.randomUUID();
144
- e = e || {};
145
- var r = e.random || (e.rng || Y)();
146
- return r[6] = r[6] & 15 | 64, r[8] = r[8] & 63 | 128, K(r);
213
+ function se(t, e, o) {
214
+ if (D.randomUUID && !e && !t)
215
+ return D.randomUUID();
216
+ t = t || {};
217
+ var n = t.random || (t.rng || ne)();
218
+ return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, te(n);
147
219
  }
148
- const G = process.env.NODE_ENV === "production", Q = !G, Z = async ({ params: e = {} }) => {
220
+ const ce = process.env.NODE_ENV === "production", ae = !ce, ie = async ({ params: t = {} }) => {
149
221
  try {
150
- const t = B(), n = await fetch(
151
- Q ? `${R.dev}/authenticate` : `${R.prod}/authenticate`,
222
+ const e = se(), o = await fetch(
223
+ ae ? `${J.dev}/authenticate` : `${J.prod}/authenticate`,
152
224
  {
153
225
  credentials: "include",
154
226
  method: "POST",
155
227
  headers: {
156
228
  "Content-Type": "application/json",
157
- [j.CLIENT_ID]: `${e.clientId}`
229
+ [G.CLIENT_ID]: `${t.clientId}`
158
230
  },
159
- body: JSON.stringify({ ...e, nonce: t })
231
+ body: JSON.stringify({ ...t, nonce: e })
160
232
  }
161
233
  );
162
- if (n.status !== 200)
163
- return { status: n.status, data: [] };
164
- const { data: r, errors: c } = await n.json();
165
- return r.nonce !== t ? { status: 500, data: [] } : {
166
- status: n.status,
167
- data: r,
234
+ if (o.status !== 200)
235
+ return { status: o.status, data: [] };
236
+ const { data: n, errors: c } = await o.json();
237
+ return n.nonce !== e ? { status: 500, data: [] } : {
238
+ status: o.status,
239
+ data: n,
168
240
  errors: c
169
241
  };
170
- } catch (t) {
171
- return console.error(t), { status: 500, data: [] };
242
+ } catch (e) {
243
+ return console.error(e), { status: 500, data: [] };
172
244
  }
173
245
  };
174
- function ee(e) {
175
- const t = _();
176
- return w(() => {
177
- t.current = e;
178
- }), t.current;
246
+ function ue(t) {
247
+ const e = R();
248
+ return f(() => {
249
+ e.current = t;
250
+ }), e.current;
179
251
  }
180
- const O = () => {
181
- throw new Error(X);
182
- }, x = M({
252
+ const U = () => {
253
+ throw new Error(ee);
254
+ }, P = M({
183
255
  isAuthenticated: !1,
184
- login: O,
185
- logout: O,
256
+ login: U,
257
+ logout: U,
186
258
  accessToken: void 0,
187
259
  refreshToken: void 0,
188
260
  idToken: void 0,
189
261
  logoutReason: ""
190
- }), oe = ({
191
- children: e,
192
- sessionExpiration: t,
193
- clientId: n,
194
- accessType: r
262
+ }), he = ({
263
+ children: t,
264
+ sessionExpiration: e,
265
+ clientId: o,
266
+ accessType: n
195
267
  }) => {
196
- const [c, l, u] = E(
197
- `@@auth@@::${n}::@@access@@`,
268
+ const [c, d, u] = S(
269
+ `@@auth@@::${o}::@@access@@`,
198
270
  ""
199
- ), [h, T, m] = E(
200
- `@@auth@@::${n}::@@refresh@@`,
271
+ ), [h, p, _] = S(
272
+ `@@auth@@::${o}::@@refresh@@`,
201
273
  ""
202
- ), [i, y, o] = E(
203
- `@@auth@@::${n}::@@user@@`,
274
+ ), [i, v, r] = S(
275
+ `@@auth@@::${o}::@@user@@`,
204
276
  ""
205
- ), [a, g] = U({
277
+ ), [a, g] = x({
206
278
  isAuthenticated: !!i,
207
279
  accessToken: c,
208
280
  refreshToken: h,
209
281
  idToken: i,
210
282
  logoutReason: "",
211
283
  userId: ""
212
- }), v = ee(i) || "";
213
- w(() => {
214
- v !== i && i !== "" ? g({
215
- isAuthenticated: !0,
216
- accessToken: c,
217
- refreshToken: h,
218
- idToken: i,
219
- logoutReason: "",
220
- userId: a.userId
221
- }) : v !== i && i === "" && g({
222
- isAuthenticated: !1,
223
- accessToken: "",
224
- refreshToken: "",
225
- idToken: "",
226
- logoutReason: J,
227
- userId: ""
228
- });
229
- }, [c, h, i, v, a.userId]);
230
- const $ = async (P, L) => {
231
- var S;
232
- const d = await Z({
284
+ }), y = ue(i) || "";
285
+ f(() => {
286
+ if (y !== i && i !== "")
287
+ try {
288
+ const { _id: E } = N(i);
289
+ g({
290
+ isAuthenticated: !0,
291
+ accessToken: c,
292
+ refreshToken: h,
293
+ idToken: i,
294
+ logoutReason: "",
295
+ userId: E || ""
296
+ });
297
+ } catch {
298
+ g({
299
+ isAuthenticated: !1,
300
+ accessToken: "",
301
+ refreshToken: "",
302
+ idToken: "",
303
+ logoutReason: C,
304
+ userId: ""
305
+ });
306
+ }
307
+ else
308
+ y !== i && i === "" && g({
309
+ isAuthenticated: !1,
310
+ accessToken: "",
311
+ refreshToken: "",
312
+ idToken: "",
313
+ logoutReason: C,
314
+ userId: ""
315
+ });
316
+ }, [c, h, i, y]);
317
+ const $ = async (E, W) => {
318
+ const l = await ie({
233
319
  params: {
234
- type: r || H.ID_TOKEN,
235
- username: P,
236
- password: L,
237
- sessionExpiration: t,
238
- clientId: n
320
+ type: n || X.ID_TOKEN,
321
+ username: E,
322
+ password: W,
323
+ sessionExpiration: e,
324
+ clientId: o
239
325
  }
240
326
  });
241
- return (S = d.data) != null && S.idToken ? (y(d.data.idToken), d.data.accessToken && l(d.data.accessToken), d.data.refreshToken && T(d.data.refreshToken), g({
242
- isAuthenticated: !0,
243
- idToken: d.data.idToken,
244
- accessToken: d.data.accessToken,
245
- refreshToken: d.data.refreshToken,
246
- userId: d.data.userId,
247
- logoutReason: ""
248
- }), !0) : !1;
249
- }, b = () => {
250
- u(), m(), o();
327
+ try {
328
+ const { _id: I } = N(l.data.idToken);
329
+ return I ? (v(l.data.idToken), l.data.accessToken && d(l.data.accessToken), l.data.refreshToken && p(l.data.refreshToken), g({
330
+ isAuthenticated: !0,
331
+ idToken: l.data.idToken,
332
+ accessToken: l.data.accessToken,
333
+ refreshToken: l.data.refreshToken,
334
+ userId: I,
335
+ logoutReason: ""
336
+ }), !0) : !1;
337
+ } catch {
338
+ return !1;
339
+ }
340
+ }, V = () => {
341
+ u(), _(), r();
251
342
  };
252
- return /* @__PURE__ */ V(x.Provider, { value: { ...a, login: $, logout: b }, children: e });
253
- }, re = (e = x) => F(e);
343
+ return /* @__PURE__ */ z(P.Provider, { value: { ...a, login: $, logout: V }, children: t });
344
+ }, ge = (t = P) => H(t);
254
345
  export {
255
- oe as AuthProvider,
256
- re as useAuth
346
+ he as AuthProvider,
347
+ ge as useAuth
257
348
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/auth-provider",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -46,7 +46,8 @@
46
46
  "dependencies": {
47
47
  "@versini/auth-common": "2.0.0",
48
48
  "@versini/ui-hooks": "3.0.0",
49
+ "jose": "5.4.1",
49
50
  "uuid": "10.0.0"
50
51
  },
51
- "gitHead": "3dbd373d2db89d99a1ca36e0bbe1bc987d8eaaae"
52
+ "gitHead": "e60af859117bfbd5af8dd27504cd1ca214c28194"
52
53
  }