react-ecosistema-unp 1.7.1-8.pre-0 → 1.7.1-8.pre-1
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,74 +1,58 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useState as
|
|
3
|
-
import { useNavigate as
|
|
4
|
-
import { j as
|
|
5
|
-
const
|
|
6
|
-
const [
|
|
7
|
-
if ((await fetch(`${s}api-auth/auth/refresh_token/`, {
|
|
1
|
+
import { jsx as _ } from "react/jsx-runtime";
|
|
2
|
+
import { useState as T, useEffect as v } from "react";
|
|
3
|
+
import { useNavigate as b, useLocation as y, Outlet as g } from "react-router-dom";
|
|
4
|
+
import { j as f } from "../../index-CuHybtft.js";
|
|
5
|
+
const $ = () => {
|
|
6
|
+
const [w, c] = T(!1), p = b(), d = y(), s = `${window.location.protocol}//${window.location.host}/`, m = async () => {
|
|
7
|
+
if (!(await fetch(`${s}api-auth/auth/refresh_token/`, {
|
|
8
8
|
method: "POST",
|
|
9
|
-
headers: {
|
|
10
|
-
"Content-Type": "application/json"
|
|
11
|
-
},
|
|
9
|
+
headers: { "Content-Type": "application/json" },
|
|
12
10
|
credentials: "include"
|
|
13
11
|
})).ok)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
body: JSON.stringify({
|
|
36
|
-
id_usuario: n
|
|
37
|
-
})
|
|
38
|
-
});
|
|
39
|
-
if (t.ok) {
|
|
40
|
-
const o = await t.json();
|
|
41
|
-
p(o.roles.map((r) => r.id_rol));
|
|
42
|
-
} else
|
|
43
|
-
throw new Error("Error al obtener roles del usuario");
|
|
44
|
-
} catch (t) {
|
|
45
|
-
console.error("Error al obtener roles del usuario", t), sessionStorage.removeItem("user_token"), window.location.href = s;
|
|
46
|
-
}
|
|
12
|
+
throw new Error("Error al refrescar token de Microsoft");
|
|
13
|
+
return !0;
|
|
14
|
+
}, u = async (r) => {
|
|
15
|
+
const a = await fetch(`${s}api-auth/users/role_by_user`, {
|
|
16
|
+
method: "POST",
|
|
17
|
+
headers: { "Content-Type": "application/json" },
|
|
18
|
+
body: JSON.stringify({ id_usuario: r })
|
|
19
|
+
});
|
|
20
|
+
if (!a.ok)
|
|
21
|
+
throw new Error("Error al obtener roles del usuario");
|
|
22
|
+
return (await a.json()).roles.map((t) => t.id_rol);
|
|
23
|
+
}, h = (r, a, o) => {
|
|
24
|
+
const n = r.datos_basicos.servicios.map((e) => e.url).filter((e) => !!e).map(
|
|
25
|
+
(e) => e.startsWith("/") ? e : `/${e}`
|
|
26
|
+
), i = o.startsWith("/") ? o : `/${o}`, l = n.some(
|
|
27
|
+
(e) => i.startsWith(e)
|
|
28
|
+
), k = r.datos_basicos.servicios.some(
|
|
29
|
+
(e) => a.includes(e.id_rol)
|
|
30
|
+
);
|
|
31
|
+
return l && k;
|
|
47
32
|
};
|
|
48
|
-
return
|
|
33
|
+
return v(() => {
|
|
49
34
|
(async () => {
|
|
50
35
|
try {
|
|
51
|
-
if ((await fetch(`${s}api-auth/auth/validate_jwt/`, {
|
|
36
|
+
if (!(await fetch(`${s}api-auth/auth/validate_jwt/`, {
|
|
52
37
|
method: "POST",
|
|
53
|
-
headers: {
|
|
54
|
-
"Content-Type": "application/json"
|
|
55
|
-
},
|
|
38
|
+
headers: { "Content-Type": "application/json" },
|
|
56
39
|
credentials: "include"
|
|
57
|
-
})).ok)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const i = d(o, c.pathname, s);
|
|
63
|
-
l(i);
|
|
64
|
-
}
|
|
65
|
-
} else
|
|
66
|
-
throw new Error("Token inválido");
|
|
40
|
+
})).ok) throw new Error("Token inválido");
|
|
41
|
+
const o = sessionStorage.getItem("user_token");
|
|
42
|
+
if (!o) throw new Error("Token no encontrado");
|
|
43
|
+
const t = f(o), n = t.datos_basicos.id_busuario, i = await u(n);
|
|
44
|
+
h(t, i, d.pathname) ? c(!0) : (sessionStorage.removeItem("user_token"), window.location.href = s, c(!1));
|
|
67
45
|
} catch {
|
|
68
46
|
try {
|
|
69
|
-
if (await
|
|
70
|
-
const
|
|
71
|
-
|
|
47
|
+
if (await m()) {
|
|
48
|
+
const t = sessionStorage.getItem("user_token");
|
|
49
|
+
if (!t) throw new Error("Token no encontrado tras refresco");
|
|
50
|
+
const n = f(t), i = n.datos_basicos.id_busuario, l = await u(i);
|
|
51
|
+
if (h(n, l, d.pathname)) {
|
|
52
|
+
c(!0);
|
|
53
|
+
return;
|
|
54
|
+
} else
|
|
55
|
+
sessionStorage.removeItem("user_token"), window.location.href = s, c(!1);
|
|
72
56
|
} else
|
|
73
57
|
throw new Error("No se pudo refrescar el token.");
|
|
74
58
|
} catch {
|
|
@@ -76,8 +60,8 @@ const V = () => {
|
|
|
76
60
|
}
|
|
77
61
|
}
|
|
78
62
|
})();
|
|
79
|
-
}, [
|
|
63
|
+
}, [p, d.pathname]), w ? /* @__PURE__ */ _(g, {}) : null;
|
|
80
64
|
};
|
|
81
65
|
export {
|
|
82
|
-
|
|
66
|
+
$ as ProtectedRoute
|
|
83
67
|
};
|