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