@versini/auth-provider 1.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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +237 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Arno Versini
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react from 'react';
|
|
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;
|
|
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;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
declare const useAuth: (context?: react.Context<AuthContextType>) => AuthContextType;
|
|
22
|
+
|
|
23
|
+
export { AuthProvider, useAuth };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
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";
|
|
3
|
+
/*!
|
|
4
|
+
@versini/auth-provider v0.0.0
|
|
5
|
+
© 2024 gizmette.com
|
|
6
|
+
*/
|
|
7
|
+
try {
|
|
8
|
+
window.__VERSINI_AUTH_CLIENT__ || (window.__VERSINI_AUTH_CLIENT__ = {
|
|
9
|
+
version: "0.0.0",
|
|
10
|
+
buildTime: "06/14/2024 11:46 AM EDT",
|
|
11
|
+
homepage: "https://github.com/aversini/auth-client",
|
|
12
|
+
license: "MIT"
|
|
13
|
+
});
|
|
14
|
+
} catch {
|
|
15
|
+
}
|
|
16
|
+
var N = typeof window < "u" ? V : k;
|
|
17
|
+
function A(e, t, n, r) {
|
|
18
|
+
const i = S(t);
|
|
19
|
+
N(() => {
|
|
20
|
+
i.current = t;
|
|
21
|
+
}, [t]), k(() => {
|
|
22
|
+
const l = window;
|
|
23
|
+
if (!(l && l.addEventListener))
|
|
24
|
+
return;
|
|
25
|
+
const u = (h) => {
|
|
26
|
+
i.current(h);
|
|
27
|
+
};
|
|
28
|
+
return l.addEventListener(e, u, r), () => {
|
|
29
|
+
l.removeEventListener(e, u, r);
|
|
30
|
+
};
|
|
31
|
+
}, [e, n, r]);
|
|
32
|
+
}
|
|
33
|
+
function R(e) {
|
|
34
|
+
const t = S(() => {
|
|
35
|
+
throw new Error("Cannot call an event handler while rendering.");
|
|
36
|
+
});
|
|
37
|
+
return N(() => {
|
|
38
|
+
t.current = e;
|
|
39
|
+
}, [e]), y((...n) => {
|
|
40
|
+
var r;
|
|
41
|
+
return (r = t.current) == null ? void 0 : r.call(t, ...n);
|
|
42
|
+
}, [t]);
|
|
43
|
+
}
|
|
44
|
+
var m = typeof window > "u";
|
|
45
|
+
function I(e, t, n = {}) {
|
|
46
|
+
const { initializeWithValue: r = !0 } = n, i = y(
|
|
47
|
+
(o) => n.serializer ? n.serializer(o) : JSON.stringify(o),
|
|
48
|
+
[n]
|
|
49
|
+
), l = y(
|
|
50
|
+
(o) => {
|
|
51
|
+
if (n.deserializer)
|
|
52
|
+
return n.deserializer(o);
|
|
53
|
+
if (o === "undefined")
|
|
54
|
+
return;
|
|
55
|
+
const a = t instanceof Function ? t() : t;
|
|
56
|
+
let T;
|
|
57
|
+
try {
|
|
58
|
+
T = JSON.parse(o);
|
|
59
|
+
} catch (v) {
|
|
60
|
+
return console.error("Error parsing JSON:", v), a;
|
|
61
|
+
}
|
|
62
|
+
return T;
|
|
63
|
+
},
|
|
64
|
+
[n, t]
|
|
65
|
+
), u = y(() => {
|
|
66
|
+
const o = t instanceof Function ? t() : t;
|
|
67
|
+
if (m)
|
|
68
|
+
return o;
|
|
69
|
+
try {
|
|
70
|
+
const a = window.localStorage.getItem(e);
|
|
71
|
+
return a ? l(a) : o;
|
|
72
|
+
} catch (a) {
|
|
73
|
+
return console.warn(`Error reading localStorage key “${e}”:`, a), o;
|
|
74
|
+
}
|
|
75
|
+
}, [t, e, l]), [h, g] = C(() => r ? u() : t instanceof Function ? t() : t), f = R((o) => {
|
|
76
|
+
m && console.warn(
|
|
77
|
+
`Tried setting localStorage key “${e}” even though environment is not a client`
|
|
78
|
+
);
|
|
79
|
+
try {
|
|
80
|
+
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 }));
|
|
82
|
+
} catch (a) {
|
|
83
|
+
console.warn(`Error setting localStorage key “${e}”:`, a);
|
|
84
|
+
}
|
|
85
|
+
}), c = R(() => {
|
|
86
|
+
m && console.warn(
|
|
87
|
+
`Tried removing localStorage key “${e}” even though environment is not a client`
|
|
88
|
+
);
|
|
89
|
+
const o = t instanceof Function ? t() : t;
|
|
90
|
+
window.localStorage.removeItem(e), g(o), window.dispatchEvent(new StorageEvent("local-storage", { key: e }));
|
|
91
|
+
});
|
|
92
|
+
k(() => {
|
|
93
|
+
g(u());
|
|
94
|
+
}, [e]);
|
|
95
|
+
const w = y(
|
|
96
|
+
(o) => {
|
|
97
|
+
o.key && o.key !== e || g(u());
|
|
98
|
+
},
|
|
99
|
+
[e, u]
|
|
100
|
+
);
|
|
101
|
+
return A("storage", w), A("local-storage", w), [h, f, c];
|
|
102
|
+
}
|
|
103
|
+
var s = [];
|
|
104
|
+
for (var E = 0; E < 256; ++E)
|
|
105
|
+
s.push((E + 256).toString(16).slice(1));
|
|
106
|
+
function H(e, t = 0) {
|
|
107
|
+
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
|
+
}
|
|
109
|
+
var p, j = new Uint8Array(16);
|
|
110
|
+
function J() {
|
|
111
|
+
if (!p && (p = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !p))
|
|
112
|
+
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
113
|
+
return p(j);
|
|
114
|
+
}
|
|
115
|
+
var K = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
116
|
+
const U = {
|
|
117
|
+
randomUUID: K
|
|
118
|
+
};
|
|
119
|
+
function M(e, t, n) {
|
|
120
|
+
if (U.randomUUID && !t && !e)
|
|
121
|
+
return U.randomUUID();
|
|
122
|
+
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);
|
|
125
|
+
}
|
|
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 = {} }) => {
|
|
139
|
+
try {
|
|
140
|
+
const t = M(), n = await fetch(
|
|
141
|
+
`${process.env.PUBLIC_AUTH_SERVER_URL}/authenticate`,
|
|
142
|
+
{
|
|
143
|
+
credentials: "include",
|
|
144
|
+
method: "POST",
|
|
145
|
+
headers: {
|
|
146
|
+
"Content-Type": "application/json",
|
|
147
|
+
"X-Auth-TenantId": `${e.tenantId}`
|
|
148
|
+
},
|
|
149
|
+
body: JSON.stringify({ ...e, nonce: t })
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
if (n.status !== 200)
|
|
153
|
+
return { status: n.status, data: [] };
|
|
154
|
+
const { data: r, errors: i } = await n.json();
|
|
155
|
+
return r.nonce !== t ? { status: 500, data: [] } : {
|
|
156
|
+
status: n.status,
|
|
157
|
+
data: r,
|
|
158
|
+
errors: i
|
|
159
|
+
};
|
|
160
|
+
} catch (t) {
|
|
161
|
+
return console.error(t), { status: 500, data: [] };
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
function W(e) {
|
|
165
|
+
const t = S();
|
|
166
|
+
return k(() => {
|
|
167
|
+
t.current = e;
|
|
168
|
+
}), t.current;
|
|
169
|
+
}
|
|
170
|
+
const Q = ({
|
|
171
|
+
children: e,
|
|
172
|
+
sessionExpiration: t,
|
|
173
|
+
tenantId: n,
|
|
174
|
+
accessType: r
|
|
175
|
+
}) => {
|
|
176
|
+
const [i, l, u] = I(
|
|
177
|
+
`@@auth@@::${n}::@@access@@`,
|
|
178
|
+
""
|
|
179
|
+
), [h, g, f] = I(
|
|
180
|
+
`@@auth@@::${n}::@@refresh@@`,
|
|
181
|
+
""
|
|
182
|
+
), [c, w, o] = I(
|
|
183
|
+
`@@auth@@::${n}::@@user@@`,
|
|
184
|
+
""
|
|
185
|
+
), [a, T] = C({
|
|
186
|
+
isAuthenticated: !!c,
|
|
187
|
+
accessToken: i,
|
|
188
|
+
refreshToken: h,
|
|
189
|
+
idToken: c,
|
|
190
|
+
logoutReason: "",
|
|
191
|
+
userId: ""
|
|
192
|
+
}), v = W(c) || "";
|
|
193
|
+
k(() => {
|
|
194
|
+
v !== c && c !== "" ? T({
|
|
195
|
+
isAuthenticated: !0,
|
|
196
|
+
accessToken: i,
|
|
197
|
+
refreshToken: h,
|
|
198
|
+
idToken: c,
|
|
199
|
+
logoutReason: "",
|
|
200
|
+
userId: a.userId
|
|
201
|
+
}) : v !== c && c === "" && T({
|
|
202
|
+
isAuthenticated: !1,
|
|
203
|
+
accessToken: "",
|
|
204
|
+
refreshToken: "",
|
|
205
|
+
idToken: "",
|
|
206
|
+
logoutReason: Y,
|
|
207
|
+
userId: ""
|
|
208
|
+
});
|
|
209
|
+
}, [i, h, c, v, a.userId]);
|
|
210
|
+
const $ = async (D, O) => {
|
|
211
|
+
var _;
|
|
212
|
+
const d = await B({
|
|
213
|
+
params: {
|
|
214
|
+
type: r || X.ID_TOKEN,
|
|
215
|
+
username: D,
|
|
216
|
+
password: O,
|
|
217
|
+
sessionExpiration: t,
|
|
218
|
+
tenantId: n
|
|
219
|
+
}
|
|
220
|
+
});
|
|
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({
|
|
222
|
+
isAuthenticated: !0,
|
|
223
|
+
idToken: d.data.idToken,
|
|
224
|
+
accessToken: d.data.accessToken,
|
|
225
|
+
refreshToken: d.data.refreshToken,
|
|
226
|
+
userId: d.data.userId,
|
|
227
|
+
logoutReason: ""
|
|
228
|
+
}), !0) : !1;
|
|
229
|
+
}, b = () => {
|
|
230
|
+
u(), f(), o();
|
|
231
|
+
};
|
|
232
|
+
return /* @__PURE__ */ P(L.Provider, { value: { ...a, login: $, logout: b }, children: e });
|
|
233
|
+
}, Z = (e = L) => F(e);
|
|
234
|
+
export {
|
|
235
|
+
Q as AuthProvider,
|
|
236
|
+
Z as useAuth
|
|
237
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@versini/auth-provider",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Arno Versini",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/aversini/auth-client",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git@github.com:aversini/auth-client.git"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build:check": "tsc",
|
|
22
|
+
"build:js": "vite build",
|
|
23
|
+
"build:types": "tsup",
|
|
24
|
+
"build": "npm-run-all --serial clean build:check build:js build:types",
|
|
25
|
+
"clean": "rimraf dist tmp",
|
|
26
|
+
"dev:js": "vite build --watch --mode development",
|
|
27
|
+
"dev:types": "tsup --watch src",
|
|
28
|
+
"dev": "npm-run-all clean --parallel dev:js dev:types",
|
|
29
|
+
"lint": "biome lint src",
|
|
30
|
+
"start": "static-server dist --port 5173",
|
|
31
|
+
"stats:pr": "bundlesize -c bundlesize.config.js -p \"$npm_package_version\" -o tmp/stats.json --silent",
|
|
32
|
+
"stats:release": "bundlesize -c bundlesize.config.js -p \"$npm_package_version\" -o stats/stats.json --silent",
|
|
33
|
+
"stats:report": "bundlesize -c bundlesize.config.js --type report -o tmp/pr-stats.md --silent",
|
|
34
|
+
"stats": "bundlesize -c bundlesize.config.js -p \"$npm_package_version\"",
|
|
35
|
+
"test:watch": "vitest",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": "^18.3.1",
|
|
40
|
+
"react-dom": "^18.3.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"react": "18.3.1",
|
|
44
|
+
"react-dom": "18.3.1"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@versini/ui-hooks": "3.0.0",
|
|
48
|
+
"uuid": "10.0.0"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "937770e14e560247681631016fb197f382f85b2b"
|
|
51
|
+
}
|