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