@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 +17 -15
- package/dist/index.js +111 -91
- package/package.json +3 -2
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,52 +1,70 @@
|
|
|
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
|
|
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: "
|
|
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.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
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}, [t]),
|
|
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
|
-
|
|
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
|
|
34
|
-
const t =
|
|
51
|
+
function N(e) {
|
|
52
|
+
const t = _(() => {
|
|
35
53
|
throw new Error("Cannot call an event handler while rendering.");
|
|
36
54
|
});
|
|
37
|
-
return
|
|
55
|
+
return D(() => {
|
|
38
56
|
t.current = e;
|
|
39
|
-
}, [e]),
|
|
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
|
|
45
|
-
function
|
|
46
|
-
const { initializeWithValue: r = !0 } = n,
|
|
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 =
|
|
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 =
|
|
83
|
+
), u = p(() => {
|
|
66
84
|
const o = t instanceof Function ? t() : t;
|
|
67
|
-
if (
|
|
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),
|
|
76
|
-
|
|
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,
|
|
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
|
-
}),
|
|
86
|
-
|
|
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
|
-
|
|
110
|
+
w(() => {
|
|
93
111
|
g(u());
|
|
94
112
|
}, [e]);
|
|
95
|
-
const
|
|
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",
|
|
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
|
|
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
|
|
110
|
-
function
|
|
111
|
-
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))
|
|
112
134
|
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
113
|
-
return
|
|
135
|
+
return k(W);
|
|
114
136
|
}
|
|
115
|
-
var
|
|
116
|
-
const
|
|
117
|
-
randomUUID:
|
|
137
|
+
var q = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
138
|
+
const O = {
|
|
139
|
+
randomUUID: q
|
|
118
140
|
};
|
|
119
|
-
function
|
|
120
|
-
if (
|
|
121
|
-
return
|
|
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 ||
|
|
124
|
-
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);
|
|
125
147
|
}
|
|
126
|
-
const
|
|
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 =
|
|
141
|
-
`${
|
|
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
|
-
|
|
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:
|
|
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:
|
|
168
|
+
errors: c
|
|
159
169
|
};
|
|
160
170
|
} catch (t) {
|
|
161
171
|
return console.error(t), { status: 500, data: [] };
|
|
162
172
|
}
|
|
163
173
|
};
|
|
164
|
-
function
|
|
165
|
-
const t =
|
|
166
|
-
return
|
|
174
|
+
function ee(e) {
|
|
175
|
+
const t = _();
|
|
176
|
+
return w(() => {
|
|
167
177
|
t.current = e;
|
|
168
178
|
}), t.current;
|
|
169
179
|
}
|
|
170
|
-
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 = ({
|
|
171
191
|
children: e,
|
|
172
192
|
sessionExpiration: t,
|
|
173
193
|
tenantId: n,
|
|
174
194
|
accessType: r
|
|
175
195
|
}) => {
|
|
176
|
-
const [
|
|
196
|
+
const [c, l, u] = f(
|
|
177
197
|
`@@auth@@::${n}::@@access@@`,
|
|
178
198
|
""
|
|
179
|
-
), [h, g,
|
|
199
|
+
), [h, g, m] = f(
|
|
180
200
|
`@@auth@@::${n}::@@refresh@@`,
|
|
181
201
|
""
|
|
182
|
-
), [
|
|
202
|
+
), [i, y, o] = f(
|
|
183
203
|
`@@auth@@::${n}::@@user@@`,
|
|
184
204
|
""
|
|
185
205
|
), [a, T] = C({
|
|
186
|
-
isAuthenticated: !!
|
|
187
|
-
accessToken:
|
|
206
|
+
isAuthenticated: !!i,
|
|
207
|
+
accessToken: c,
|
|
188
208
|
refreshToken: h,
|
|
189
|
-
idToken:
|
|
209
|
+
idToken: i,
|
|
190
210
|
logoutReason: "",
|
|
191
211
|
userId: ""
|
|
192
|
-
}), v =
|
|
193
|
-
|
|
194
|
-
v !==
|
|
212
|
+
}), v = ee(i) || "";
|
|
213
|
+
w(() => {
|
|
214
|
+
v !== i && i !== "" ? T({
|
|
195
215
|
isAuthenticated: !0,
|
|
196
|
-
accessToken:
|
|
216
|
+
accessToken: c,
|
|
197
217
|
refreshToken: h,
|
|
198
|
-
idToken:
|
|
218
|
+
idToken: i,
|
|
199
219
|
logoutReason: "",
|
|
200
220
|
userId: a.userId
|
|
201
|
-
}) : v !==
|
|
221
|
+
}) : v !== i && i === "" && T({
|
|
202
222
|
isAuthenticated: !1,
|
|
203
223
|
accessToken: "",
|
|
204
224
|
refreshToken: "",
|
|
205
225
|
idToken: "",
|
|
206
|
-
logoutReason:
|
|
226
|
+
logoutReason: J,
|
|
207
227
|
userId: ""
|
|
208
228
|
});
|
|
209
|
-
}, [
|
|
210
|
-
const
|
|
211
|
-
var
|
|
212
|
-
const d = await
|
|
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 ||
|
|
215
|
-
username:
|
|
216
|
-
password:
|
|
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 (
|
|
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
|
-
},
|
|
230
|
-
u(),
|
|
249
|
+
}, P = () => {
|
|
250
|
+
u(), m(), o();
|
|
231
251
|
};
|
|
232
|
-
return /* @__PURE__ */
|
|
233
|
-
},
|
|
252
|
+
return /* @__PURE__ */ z(x.Provider, { value: { ...a, login: b, logout: P }, children: e });
|
|
253
|
+
}, re = (e = x) => F(e);
|
|
234
254
|
export {
|
|
235
|
-
|
|
236
|
-
|
|
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.
|
|
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": "
|
|
51
|
+
"gitHead": "ec250274e7b1c3b9c74a9cd7b24052a5e7e8a8c8"
|
|
51
52
|
}
|