@versini/auth-provider 1.0.1 → 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 +17 -15
- package/dist/index.js +87 -81
- package/package.json +3 -3
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
4
|
+
type AuthProviderProps = {
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
sessionExpiration?: string;
|
|
7
|
+
tenantId: string;
|
|
8
|
+
accessType?: string;
|
|
9
|
+
};
|
|
10
10
|
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
|
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,40 +1,42 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useCallback as
|
|
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 v1.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: "1.0.
|
|
10
|
-
buildTime: "06/
|
|
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
16
|
/*!
|
|
17
|
-
@versini/auth-common v1.0.
|
|
17
|
+
@versini/auth-common v1.0.1
|
|
18
18
|
© 2024 gizmette.com
|
|
19
19
|
*/
|
|
20
20
|
try {
|
|
21
21
|
window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
|
|
22
|
-
version: "1.0.
|
|
23
|
-
buildTime: "06/
|
|
22
|
+
version: "1.0.1",
|
|
23
|
+
buildTime: "06/17/2024 03:05 PM EDT",
|
|
24
24
|
homepage: "https://github.com/aversini/auth-client",
|
|
25
25
|
license: "MIT"
|
|
26
26
|
});
|
|
27
27
|
} catch {
|
|
28
28
|
}
|
|
29
|
-
const
|
|
29
|
+
const H = {
|
|
30
30
|
ID_TOKEN: "id_token"
|
|
31
|
+
}, j = {
|
|
32
|
+
tenantId: "X-Auth-TenantId"
|
|
31
33
|
};
|
|
32
|
-
var
|
|
33
|
-
function
|
|
34
|
-
const c =
|
|
35
|
-
|
|
34
|
+
var D = typeof window < "u" ? L : w;
|
|
35
|
+
function A(e, t, n, r) {
|
|
36
|
+
const c = _(t);
|
|
37
|
+
D(() => {
|
|
36
38
|
c.current = t;
|
|
37
|
-
}, [t]),
|
|
39
|
+
}, [t]), w(() => {
|
|
38
40
|
const l = window;
|
|
39
41
|
if (!(l && l.addEventListener))
|
|
40
42
|
return;
|
|
@@ -46,23 +48,23 @@ function R(e, t, n, r) {
|
|
|
46
48
|
};
|
|
47
49
|
}, [e, n, r]);
|
|
48
50
|
}
|
|
49
|
-
function
|
|
50
|
-
const t =
|
|
51
|
+
function N(e) {
|
|
52
|
+
const t = _(() => {
|
|
51
53
|
throw new Error("Cannot call an event handler while rendering.");
|
|
52
54
|
});
|
|
53
|
-
return
|
|
55
|
+
return D(() => {
|
|
54
56
|
t.current = e;
|
|
55
|
-
}, [e]),
|
|
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
|
|
61
|
-
function
|
|
62
|
-
const { initializeWithValue: r = !0 } = n, c =
|
|
62
|
+
var I = typeof window > "u";
|
|
63
|
+
function f(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 =
|
|
67
|
+
), l = p(
|
|
66
68
|
(o) => {
|
|
67
69
|
if (n.deserializer)
|
|
68
70
|
return n.deserializer(o);
|
|
@@ -78,9 +80,9 @@ function I(e, t, n = {}) {
|
|
|
78
80
|
return T;
|
|
79
81
|
},
|
|
80
82
|
[n, t]
|
|
81
|
-
), u =
|
|
83
|
+
), u = p(() => {
|
|
82
84
|
const o = t instanceof Function ? t() : t;
|
|
83
|
-
if (
|
|
85
|
+
if (I)
|
|
84
86
|
return o;
|
|
85
87
|
try {
|
|
86
88
|
const a = window.localStorage.getItem(e);
|
|
@@ -88,8 +90,8 @@ 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 =
|
|
92
|
-
|
|
93
|
+
}, [t, e, l]), [h, g] = C(() => r ? u() : t instanceof Function ? t() : t), m = N((o) => {
|
|
94
|
+
I && console.warn(
|
|
93
95
|
`Tried setting localStorage key “${e}” even though environment is not a client`
|
|
94
96
|
);
|
|
95
97
|
try {
|
|
@@ -98,67 +100,61 @@ function I(e, t, n = {}) {
|
|
|
98
100
|
} catch (a) {
|
|
99
101
|
console.warn(`Error setting localStorage key “${e}”:`, a);
|
|
100
102
|
}
|
|
101
|
-
}), i =
|
|
102
|
-
|
|
103
|
+
}), i = N(() => {
|
|
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
108
|
window.localStorage.removeItem(e), g(o), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
|
|
107
109
|
});
|
|
108
|
-
|
|
110
|
+
w(() => {
|
|
109
111
|
g(u());
|
|
110
112
|
}, [e]);
|
|
111
|
-
const
|
|
113
|
+
const y = p(
|
|
112
114
|
(o) => {
|
|
113
115
|
o.key && o.key !== e || g(u());
|
|
114
116
|
},
|
|
115
117
|
[e, u]
|
|
116
118
|
);
|
|
117
|
-
return
|
|
119
|
+
return A("storage", y), A("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
|
|
121
|
-
s.push((
|
|
122
|
-
function
|
|
126
|
+
for (var E = 0; E < 256; ++E)
|
|
127
|
+
s.push((E + 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
|
|
126
|
-
function
|
|
127
|
-
if (!
|
|
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
|
|
135
|
+
return k(W);
|
|
130
136
|
}
|
|
131
|
-
var
|
|
132
|
-
const
|
|
133
|
-
randomUUID:
|
|
137
|
+
var q = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
138
|
+
const O = {
|
|
139
|
+
randomUUID: q
|
|
134
140
|
};
|
|
135
|
-
function
|
|
136
|
-
if (
|
|
137
|
-
return
|
|
141
|
+
function B(e, t, n) {
|
|
142
|
+
if (O.randomUUID && !t && !e)
|
|
143
|
+
return O.randomUUID();
|
|
138
144
|
e = e || {};
|
|
139
|
-
var r = e.random || (e.rng ||
|
|
140
|
-
return r[6] = r[6] & 15 | 64, r[8] = r[8] & 63 | 128,
|
|
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
|
|
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 =
|
|
155
|
-
`${
|
|
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
|
-
|
|
157
|
+
[j.tenantId]: `${e.tenantId}`
|
|
162
158
|
},
|
|
163
159
|
body: JSON.stringify({ ...e, nonce: t })
|
|
164
160
|
}
|
|
@@ -175,25 +171,35 @@ const N = () => {
|
|
|
175
171
|
return console.error(t), { status: 500, data: [] };
|
|
176
172
|
}
|
|
177
173
|
};
|
|
178
|
-
function
|
|
179
|
-
const t =
|
|
180
|
-
return
|
|
174
|
+
function ee(e) {
|
|
175
|
+
const t = _();
|
|
176
|
+
return w(() => {
|
|
181
177
|
t.current = e;
|
|
182
178
|
}), t.current;
|
|
183
179
|
}
|
|
184
|
-
const
|
|
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 = ({
|
|
185
191
|
children: e,
|
|
186
192
|
sessionExpiration: t,
|
|
187
193
|
tenantId: n,
|
|
188
194
|
accessType: r
|
|
189
195
|
}) => {
|
|
190
|
-
const [c, l, u] =
|
|
196
|
+
const [c, l, u] = f(
|
|
191
197
|
`@@auth@@::${n}::@@access@@`,
|
|
192
198
|
""
|
|
193
|
-
), [h, g, m] =
|
|
199
|
+
), [h, g, m] = f(
|
|
194
200
|
`@@auth@@::${n}::@@refresh@@`,
|
|
195
201
|
""
|
|
196
|
-
), [i,
|
|
202
|
+
), [i, y, o] = f(
|
|
197
203
|
`@@auth@@::${n}::@@user@@`,
|
|
198
204
|
""
|
|
199
205
|
), [a, T] = C({
|
|
@@ -203,8 +209,8 @@ const Q = ({
|
|
|
203
209
|
idToken: i,
|
|
204
210
|
logoutReason: "",
|
|
205
211
|
userId: ""
|
|
206
|
-
}), v =
|
|
207
|
-
|
|
212
|
+
}), v = ee(i) || "";
|
|
213
|
+
w(() => {
|
|
208
214
|
v !== i && i !== "" ? T({
|
|
209
215
|
isAuthenticated: !0,
|
|
210
216
|
accessToken: c,
|
|
@@ -217,22 +223,22 @@ const Q = ({
|
|
|
217
223
|
accessToken: "",
|
|
218
224
|
refreshToken: "",
|
|
219
225
|
idToken: "",
|
|
220
|
-
logoutReason:
|
|
226
|
+
logoutReason: J,
|
|
221
227
|
userId: ""
|
|
222
228
|
});
|
|
223
229
|
}, [c, h, i, v, a.userId]);
|
|
224
|
-
const b = async (
|
|
230
|
+
const b = async ($, V) => {
|
|
225
231
|
var S;
|
|
226
|
-
const d = await
|
|
232
|
+
const d = await Z({
|
|
227
233
|
params: {
|
|
228
|
-
type: r ||
|
|
229
|
-
username:
|
|
230
|
-
password:
|
|
234
|
+
type: r || H.ID_TOKEN,
|
|
235
|
+
username: $,
|
|
236
|
+
password: V,
|
|
231
237
|
sessionExpiration: t,
|
|
232
238
|
tenantId: n
|
|
233
239
|
}
|
|
234
240
|
});
|
|
235
|
-
return (S = d.data) != null && S.idToken ? (
|
|
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({
|
|
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
|
+
}, P = () => {
|
|
244
250
|
u(), m(), o();
|
|
245
251
|
};
|
|
246
|
-
return /* @__PURE__ */
|
|
247
|
-
},
|
|
252
|
+
return /* @__PURE__ */ z(x.Provider, { value: { ...a, login: b, logout: P }, children: e });
|
|
253
|
+
}, re = (e = x) => F(e);
|
|
248
254
|
export {
|
|
249
|
-
|
|
250
|
-
|
|
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.
|
|
3
|
+
"version": "1.0.2",
|
|
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.
|
|
47
|
+
"@versini/auth-common": "1.0.1",
|
|
48
48
|
"@versini/ui-hooks": "3.0.0",
|
|
49
49
|
"uuid": "10.0.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "ec250274e7b1c3b9c74a9cd7b24052a5e7e8a8c8"
|
|
52
52
|
}
|