react-ecosistema-unp 1.6.2 → 1.6.9
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,51 +1,56 @@
|
|
|
1
1
|
window.originalFetch = window.fetch.bind(window);
|
|
2
|
-
let
|
|
3
|
-
const
|
|
4
|
-
async function
|
|
5
|
-
const e = `${`${window.location.protocol}//${window.location.host}/`}api-auth/auth/refresh_token/`,
|
|
2
|
+
let l = !1, h = null;
|
|
3
|
+
const i = [];
|
|
4
|
+
async function p() {
|
|
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
|
-
|
|
12
|
-
if (!n.token)
|
|
13
|
-
throw new Error("No se recibió token en la respuesta de refresh");
|
|
14
|
-
const t = n.token;
|
|
15
|
-
return sessionStorage.setItem("user_token", `${t}`), `${t}`;
|
|
9
|
+
if (!t.ok)
|
|
10
|
+
throw new Error("Error al refrescar token, status " + t.status);
|
|
11
|
+
return "Token refreshed";
|
|
16
12
|
}
|
|
17
13
|
window.fetch = async function(o, e) {
|
|
18
|
-
const
|
|
19
|
-
if (console.log("URL petición:",
|
|
14
|
+
const t = typeof o == "string" ? o : o.toString();
|
|
15
|
+
if (console.log("URL petición:", t), t.includes("/api-auth/"))
|
|
20
16
|
return window.originalFetch(o, e);
|
|
21
17
|
e = e || {};
|
|
22
|
-
const
|
|
18
|
+
const u = sessionStorage.getItem("user_token");
|
|
23
19
|
e.headers = {
|
|
24
20
|
...e.headers,
|
|
25
|
-
...
|
|
21
|
+
...u ? { Authorization: u } : {}
|
|
26
22
|
};
|
|
27
23
|
try {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
const c = `${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 sessionStorage.removeItem("user_token"), setTimeout(() => {
|
|
31
|
+
window.location.href = c;
|
|
32
|
+
}, 1e3), r;
|
|
33
|
+
}
|
|
34
|
+
return new Promise((w, n) => {
|
|
35
|
+
i.push({ input: o, init: e, resolve: w, reject: n }), l || (l = !0, h = p(), h.then(() => {
|
|
36
|
+
i.forEach(({ input: a, init: s, resolve: d, reject: f }) => {
|
|
32
37
|
const g = {
|
|
33
|
-
...
|
|
34
|
-
headers: { ...
|
|
38
|
+
...s,
|
|
39
|
+
headers: { ...s.headers, Authorization: sessionStorage.getItem("user_token") }
|
|
35
40
|
};
|
|
36
|
-
window.originalFetch(
|
|
41
|
+
window.originalFetch(a, g).then(d).catch(f);
|
|
37
42
|
});
|
|
38
43
|
}).catch((a) => {
|
|
39
|
-
|
|
44
|
+
i.forEach(({ reject: s }) => s(a));
|
|
40
45
|
}).finally(() => {
|
|
41
|
-
|
|
46
|
+
i.length = 0, l = !1, h = null;
|
|
42
47
|
}));
|
|
43
48
|
});
|
|
44
|
-
} catch (
|
|
45
|
-
return Promise.reject(
|
|
49
|
+
} catch (c) {
|
|
50
|
+
return Promise.reject(c);
|
|
46
51
|
}
|
|
47
52
|
};
|
|
48
|
-
const
|
|
53
|
+
const k = {};
|
|
49
54
|
export {
|
|
50
|
-
|
|
55
|
+
k as default
|
|
51
56
|
};
|