react-ecosistema-unp 1.7.13 → 1.7.14
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.
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
window.originalFetch = window.fetch.bind(window);
|
|
2
2
|
let l = !1, h = null;
|
|
3
|
-
const
|
|
3
|
+
const c = [];
|
|
4
4
|
async function k() {
|
|
5
|
-
const e = `${`${window.location.protocol}//${window.location.host}/`}api-auth/auth/refresh_token/`,
|
|
5
|
+
const e = `${`${window.location.protocol}//${window.location.host}/`}api-auth/auth/refresh_token/`, t = await window.originalFetch(e, {
|
|
6
6
|
method: "POST",
|
|
7
7
|
credentials: "include"
|
|
8
8
|
});
|
|
9
|
-
if (!
|
|
10
|
-
throw new Error("Error al refrescar token, status " +
|
|
11
|
-
return "Token refreshed";
|
|
9
|
+
if (!t.ok)
|
|
10
|
+
throw new Error("Error al refrescar token, status " + t.status);
|
|
11
|
+
return console.warn("Token refrescado"), "Token refreshed";
|
|
12
12
|
}
|
|
13
13
|
window.fetch = async function(o, e) {
|
|
14
|
-
|
|
14
|
+
const t = typeof o == "string" ? o : o.toString();
|
|
15
|
+
if (console.log("Petición fetchInterceptor Librería:", t), t.includes("/api-auth/"))
|
|
15
16
|
return window.originalFetch(o, e);
|
|
16
17
|
e = e || {};
|
|
17
18
|
const u = sessionStorage.getItem("user_token");
|
|
@@ -20,33 +21,33 @@ window.fetch = async function(o, e) {
|
|
|
20
21
|
...u ? { Authorization: u } : {}
|
|
21
22
|
};
|
|
22
23
|
try {
|
|
23
|
-
const
|
|
24
|
-
if (
|
|
25
|
-
const
|
|
26
|
-
if (!
|
|
27
|
-
return
|
|
28
|
-
if (
|
|
29
|
-
return sessionStorage.removeItem("user_token"), setTimeout(() => {
|
|
30
|
-
window.location.href =
|
|
31
|
-
}, 1e3),
|
|
24
|
+
const i = `${window.location.protocol}//${window.location.host}/`, r = await window.originalFetch(o, e);
|
|
25
|
+
if (r.status !== 401) {
|
|
26
|
+
const n = await r.clone().text();
|
|
27
|
+
if (!n.includes("Signature has expired"))
|
|
28
|
+
return r;
|
|
29
|
+
if (n.includes("Invalid or expired token"))
|
|
30
|
+
return console.warn("El token expiró"), sessionStorage.removeItem("user_token"), setTimeout(() => {
|
|
31
|
+
window.location.href = i;
|
|
32
|
+
}, 1e3), r;
|
|
32
33
|
}
|
|
33
|
-
return new Promise((w,
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
return new Promise((w, n) => {
|
|
35
|
+
c.push({ input: o, init: e, resolve: w, reject: n }), l || (l = !0, h = k(), h.then(() => {
|
|
36
|
+
c.forEach(({ input: a, init: s, resolve: d, reject: f }) => {
|
|
36
37
|
const g = {
|
|
37
|
-
...
|
|
38
|
-
headers: { ...
|
|
38
|
+
...s,
|
|
39
|
+
headers: { ...s.headers, Authorization: sessionStorage.getItem("user_token") }
|
|
39
40
|
};
|
|
40
|
-
window.originalFetch(
|
|
41
|
+
window.originalFetch(a, g).then(d).catch(f);
|
|
41
42
|
});
|
|
42
|
-
}).catch((
|
|
43
|
-
|
|
43
|
+
}).catch((a) => {
|
|
44
|
+
c.forEach(({ reject: s }) => s(a));
|
|
44
45
|
}).finally(() => {
|
|
45
|
-
|
|
46
|
+
c.length = 0, l = !1, h = null;
|
|
46
47
|
}));
|
|
47
48
|
});
|
|
48
|
-
} catch (
|
|
49
|
-
return Promise.reject(
|
|
49
|
+
} catch (i) {
|
|
50
|
+
return Promise.reject(i);
|
|
50
51
|
}
|
|
51
52
|
};
|
|
52
53
|
const p = {};
|