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