@versini/auth-provider 1.0.1 → 2.0.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.
package/dist/index.d.ts CHANGED
@@ -1,23 +1,25 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as react from 'react';
3
3
 
4
- declare const AuthProvider: ({ children, sessionExpiration, tenantId, accessType, }: {
5
- children: React.ReactNode;
6
- sessionExpiration?: string;
7
- tenantId: string;
8
- accessType?: string;
9
- }) => react_jsx_runtime.JSX.Element;
4
+ type AuthProviderProps = {
5
+ children: React.ReactNode;
6
+ sessionExpiration?: string;
7
+ clientId: string;
8
+ accessType?: string;
9
+ };
10
10
 
11
- type AuthContextType = {
12
- login: (username: string, password: string) => Promise<boolean>;
13
- logout: () => void;
14
- isAuthenticated: boolean;
15
- accessToken?: string;
16
- refreshToken?: string;
17
- idToken?: string;
18
- logoutReason?: string;
11
+ type AuthContextProps = {
12
+ login: (username: string, password: string) => Promise<boolean>;
13
+ logout: () => void;
14
+ isAuthenticated: boolean;
15
+ accessToken?: string;
16
+ refreshToken?: string;
17
+ idToken?: string;
18
+ logoutReason?: string;
19
19
  };
20
20
 
21
- declare const useAuth: (context?: react.Context<AuthContextType>) => AuthContextType;
21
+ declare const AuthProvider: ({ children, sessionExpiration, clientId, accessType, }: AuthProviderProps) => react_jsx_runtime.JSX.Element;
22
+
23
+ declare const useAuth: (context?: react.Context<AuthContextProps>) => AuthContextProps;
22
24
 
23
25
  export { AuthProvider, useAuth };
package/dist/index.js CHANGED
@@ -1,40 +1,42 @@
1
1
  import { jsx as V } from "react/jsx-runtime";
2
- import { useCallback as w, useState as C, useEffect as y, useRef as E, useLayoutEffect as P, createContext as M, useContext as z } from "react";
2
+ import { useCallback as p, useState as U, useEffect as w, useRef as _, useLayoutEffect as z, createContext as M, useContext as F } from "react";
3
3
  /*!
4
- @versini/auth-provider v1.0.0
4
+ @versini/auth-provider v2.0.0
5
5
  © 2024 gizmette.com
6
6
  */
7
7
  try {
8
8
  window.__VERSINI_AUTH_CLIENT__ || (window.__VERSINI_AUTH_CLIENT__ = {
9
- version: "1.0.0",
10
- buildTime: "06/15/2024 02:36 PM EDT",
9
+ version: "2.0.0",
10
+ buildTime: "06/17/2024 04:39 PM EDT",
11
11
  homepage: "https://github.com/aversini/auth-client",
12
12
  license: "MIT"
13
13
  });
14
14
  } catch {
15
15
  }
16
16
  /*!
17
- @versini/auth-common v1.0.0
17
+ @versini/auth-common v2.0.0
18
18
  © 2024 gizmette.com
19
19
  */
20
20
  try {
21
21
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
22
- version: "1.0.0",
23
- buildTime: "06/15/2024 02:35 PM EDT",
22
+ version: "2.0.0",
23
+ buildTime: "06/17/2024 04:39 PM EDT",
24
24
  homepage: "https://github.com/aversini/auth-client",
25
25
  license: "MIT"
26
26
  });
27
27
  } catch {
28
28
  }
29
- const F = {
29
+ const H = {
30
30
  ID_TOKEN: "id_token"
31
+ }, j = {
32
+ CLIENT_ID: "X-Auth-ClientId"
31
33
  };
32
- var O = typeof window < "u" ? P : y;
33
- function R(e, t, n, r) {
34
- const c = E(t);
35
- O(() => {
34
+ var D = typeof window < "u" ? z : w;
35
+ function N(e, t, n, r) {
36
+ const c = _(t);
37
+ D(() => {
36
38
  c.current = t;
37
- }, [t]), y(() => {
39
+ }, [t]), w(() => {
38
40
  const l = window;
39
41
  if (!(l && l.addEventListener))
40
42
  return;
@@ -47,40 +49,40 @@ function R(e, t, n, r) {
47
49
  }, [e, n, r]);
48
50
  }
49
51
  function A(e) {
50
- const t = E(() => {
52
+ const t = _(() => {
51
53
  throw new Error("Cannot call an event handler while rendering.");
52
54
  });
53
- return O(() => {
55
+ return D(() => {
54
56
  t.current = e;
55
- }, [e]), w((...n) => {
57
+ }, [e]), p((...n) => {
56
58
  var r;
57
59
  return (r = t.current) == null ? void 0 : r.call(t, ...n);
58
60
  }, [t]);
59
61
  }
60
- var f = typeof window > "u";
61
- function I(e, t, n = {}) {
62
- const { initializeWithValue: r = !0 } = n, c = w(
62
+ var I = typeof window > "u";
63
+ function E(e, t, n = {}) {
64
+ const { initializeWithValue: r = !0 } = n, c = p(
63
65
  (o) => n.serializer ? n.serializer(o) : JSON.stringify(o),
64
66
  [n]
65
- ), l = w(
67
+ ), l = p(
66
68
  (o) => {
67
69
  if (n.deserializer)
68
70
  return n.deserializer(o);
69
71
  if (o === "undefined")
70
72
  return;
71
73
  const a = t instanceof Function ? t() : t;
72
- let T;
74
+ let g;
73
75
  try {
74
- T = JSON.parse(o);
76
+ g = JSON.parse(o);
75
77
  } catch (v) {
76
78
  return console.error("Error parsing JSON:", v), a;
77
79
  }
78
- return T;
80
+ return g;
79
81
  },
80
82
  [n, t]
81
- ), u = w(() => {
83
+ ), u = p(() => {
82
84
  const o = t instanceof Function ? t() : t;
83
- if (f)
85
+ if (I)
84
86
  return o;
85
87
  try {
86
88
  const a = window.localStorage.getItem(e);
@@ -88,77 +90,71 @@ function I(e, t, n = {}) {
88
90
  } catch (a) {
89
91
  return console.warn(`Error reading localStorage key “${e}”:`, a), o;
90
92
  }
91
- }, [t, e, l]), [h, g] = C(() => r ? u() : t instanceof Function ? t() : t), m = A((o) => {
92
- f && console.warn(
93
+ }, [t, e, l]), [h, T] = U(() => r ? u() : t instanceof Function ? t() : t), m = A((o) => {
94
+ I && console.warn(
93
95
  `Tried setting localStorage key “${e}” even though environment is not a client`
94
96
  );
95
97
  try {
96
98
  const a = o instanceof Function ? o(u()) : o;
97
- window.localStorage.setItem(e, c(a)), g(a), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
99
+ window.localStorage.setItem(e, c(a)), T(a), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
98
100
  } catch (a) {
99
101
  console.warn(`Error setting localStorage key “${e}”:`, a);
100
102
  }
101
103
  }), i = A(() => {
102
- f && console.warn(
104
+ I && console.warn(
103
105
  `Tried removing localStorage key “${e}” even though environment is not a client`
104
106
  );
105
107
  const o = t instanceof Function ? t() : t;
106
- window.localStorage.removeItem(e), g(o), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
108
+ window.localStorage.removeItem(e), T(o), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
107
109
  });
108
- y(() => {
109
- g(u());
110
+ w(() => {
111
+ T(u());
110
112
  }, [e]);
111
- const k = w(
113
+ const y = p(
112
114
  (o) => {
113
- o.key && o.key !== e || g(u());
115
+ o.key && o.key !== e || T(u());
114
116
  },
115
117
  [e, u]
116
118
  );
117
- return R("storage", k), R("local-storage", k), [h, m, i];
119
+ return N("storage", y), N("local-storage", y), [h, m, i];
118
120
  }
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 = {
122
+ dev: "https://auth.gizmette.local.com:3003",
123
+ prod: "https://auth.gizmette.com"
124
+ };
119
125
  var s = [];
120
- for (var _ = 0; _ < 256; ++_)
121
- s.push((_ + 256).toString(16).slice(1));
122
- function H(e, t = 0) {
126
+ for (var f = 0; f < 256; ++f)
127
+ s.push((f + 256).toString(16).slice(1));
128
+ function K(e, t = 0) {
123
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();
124
130
  }
125
- var p, j = new Uint8Array(16);
126
- function J() {
127
- if (!p && (p = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !p))
131
+ var k, W = new Uint8Array(16);
132
+ function Y() {
133
+ if (!k && (k = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !k))
128
134
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
129
- return p(j);
135
+ return k(W);
130
136
  }
131
- var K = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
132
- const U = {
133
- randomUUID: K
137
+ var q = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
138
+ const C = {
139
+ randomUUID: q
134
140
  };
135
- function X(e, t, n) {
136
- if (U.randomUUID && !t && !e)
137
- return U.randomUUID();
141
+ function B(e, t, n) {
142
+ if (C.randomUUID && !t && !e)
143
+ return C.randomUUID();
138
144
  e = e || {};
139
- var r = e.random || (e.rng || J)();
140
- return r[6] = r[6] & 15 | 64, r[8] = r[8] & 63 | 128, H(r);
145
+ var r = e.random || (e.rng || Y)();
146
+ return r[6] = r[6] & 15 | 64, r[8] = r[8] & 63 | 128, K(r);
141
147
  }
142
- const N = () => {
143
- throw new Error("You forgot to wrap your component in <AuthProvider>.");
144
- }, x = M({
145
- isAuthenticated: !1,
146
- login: N,
147
- logout: N,
148
- accessToken: void 0,
149
- refreshToken: void 0,
150
- idToken: void 0,
151
- logoutReason: ""
152
- }), B = "Oops! It looks like your session has expired. For your security, please log in again to continue.", W = async ({ params: e = {} }) => {
148
+ const G = process.env.NODE_ENV === "production", Q = !G, Z = async ({ params: e = {} }) => {
153
149
  try {
154
- const t = X(), n = await fetch(
155
- `${process.env.PUBLIC_AUTH_SERVER_URL}/authenticate`,
150
+ const t = B(), n = await fetch(
151
+ Q ? `${R.dev}/authenticate` : `${R.prod}/authenticate`,
156
152
  {
157
153
  credentials: "include",
158
154
  method: "POST",
159
155
  headers: {
160
156
  "Content-Type": "application/json",
161
- "X-Auth-TenantId": `${e.tenantId}`
157
+ [j.CLIENT_ID]: `${e.clientId}`
162
158
  },
163
159
  body: JSON.stringify({ ...e, nonce: t })
164
160
  }
@@ -175,64 +171,74 @@ const N = () => {
175
171
  return console.error(t), { status: 500, data: [] };
176
172
  }
177
173
  };
178
- function Y(e) {
179
- const t = E();
180
- return y(() => {
174
+ function ee(e) {
175
+ const t = _();
176
+ return w(() => {
181
177
  t.current = e;
182
178
  }), t.current;
183
179
  }
184
- const Q = ({
180
+ const O = () => {
181
+ throw new Error(X);
182
+ }, x = M({
183
+ isAuthenticated: !1,
184
+ login: O,
185
+ logout: O,
186
+ accessToken: void 0,
187
+ refreshToken: void 0,
188
+ idToken: void 0,
189
+ logoutReason: ""
190
+ }), oe = ({
185
191
  children: e,
186
192
  sessionExpiration: t,
187
- tenantId: n,
193
+ clientId: n,
188
194
  accessType: r
189
195
  }) => {
190
- const [c, l, u] = I(
196
+ const [c, l, u] = E(
191
197
  `@@auth@@::${n}::@@access@@`,
192
198
  ""
193
- ), [h, g, m] = I(
199
+ ), [h, T, m] = E(
194
200
  `@@auth@@::${n}::@@refresh@@`,
195
201
  ""
196
- ), [i, k, o] = I(
202
+ ), [i, y, o] = E(
197
203
  `@@auth@@::${n}::@@user@@`,
198
204
  ""
199
- ), [a, T] = C({
205
+ ), [a, g] = U({
200
206
  isAuthenticated: !!i,
201
207
  accessToken: c,
202
208
  refreshToken: h,
203
209
  idToken: i,
204
210
  logoutReason: "",
205
211
  userId: ""
206
- }), v = Y(i) || "";
207
- y(() => {
208
- v !== i && i !== "" ? T({
212
+ }), v = ee(i) || "";
213
+ w(() => {
214
+ v !== i && i !== "" ? g({
209
215
  isAuthenticated: !0,
210
216
  accessToken: c,
211
217
  refreshToken: h,
212
218
  idToken: i,
213
219
  logoutReason: "",
214
220
  userId: a.userId
215
- }) : v !== i && i === "" && T({
221
+ }) : v !== i && i === "" && g({
216
222
  isAuthenticated: !1,
217
223
  accessToken: "",
218
224
  refreshToken: "",
219
225
  idToken: "",
220
- logoutReason: B,
226
+ logoutReason: J,
221
227
  userId: ""
222
228
  });
223
229
  }, [c, h, i, v, a.userId]);
224
- const b = async (D, L) => {
230
+ const $ = async (P, L) => {
225
231
  var S;
226
- const d = await W({
232
+ const d = await Z({
227
233
  params: {
228
- type: r || F.ID_TOKEN,
229
- username: D,
234
+ type: r || H.ID_TOKEN,
235
+ username: P,
230
236
  password: L,
231
237
  sessionExpiration: t,
232
- tenantId: n
238
+ clientId: n
233
239
  }
234
240
  });
235
- return (S = d.data) != null && S.idToken ? (k(d.data.idToken), d.data.accessToken && l(d.data.accessToken), d.data.refreshToken && g(d.data.refreshToken), T({
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({
236
242
  isAuthenticated: !0,
237
243
  idToken: d.data.idToken,
238
244
  accessToken: d.data.accessToken,
@@ -240,12 +246,12 @@ const Q = ({
240
246
  userId: d.data.userId,
241
247
  logoutReason: ""
242
248
  }), !0) : !1;
243
- }, $ = () => {
249
+ }, b = () => {
244
250
  u(), m(), o();
245
251
  };
246
- return /* @__PURE__ */ V(x.Provider, { value: { ...a, login: b, logout: $ }, children: e });
247
- }, Z = (e = x) => z(e);
252
+ return /* @__PURE__ */ V(x.Provider, { value: { ...a, login: $, logout: b }, children: e });
253
+ }, re = (e = x) => F(e);
248
254
  export {
249
- Q as AuthProvider,
250
- Z as useAuth
255
+ oe as AuthProvider,
256
+ re as useAuth
251
257
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/auth-provider",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -44,9 +44,9 @@
44
44
  "react-dom": "18.3.1"
45
45
  },
46
46
  "dependencies": {
47
- "@versini/auth-common": "1.0.0",
47
+ "@versini/auth-common": "2.0.0",
48
48
  "@versini/ui-hooks": "3.0.0",
49
49
  "uuid": "10.0.0"
50
50
  },
51
- "gitHead": "42b498e39b3aba7457dc24eb26dce6b0e8ea6d07"
51
+ "gitHead": "3dbd373d2db89d99a1ca36e0bbe1bc987d8eaaae"
52
52
  }