react-ecosistema-unp 1.7.1-8.pre-1 → 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,58 +1,70 @@
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/`, {
1
+ import { jsx as S } from "react/jsx-runtime";
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
+ if ((await fetch(`${s}api-auth/auth/refresh_token/`, {
8
8
  method: "POST",
9
- headers: { "Content-Type": "application/json" },
9
+ headers: {
10
+ "Content-Type": "application/json"
11
+ },
10
12
  credentials: "include"
11
13
  })).ok)
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`, {
14
+ return !0;
15
+ throw new Error("Error al refrescar token de Microsoft");
16
+ }, w = async (d) => {
17
+ const l = await fetch(`${s}api-auth/users/role_by_user/`, {
16
18
  method: "POST",
17
- headers: { "Content-Type": "application/json" },
18
- body: JSON.stringify({ id_usuario: r })
19
+ headers: {
20
+ "Content-Type": "application/json"
21
+ },
22
+ credentials: "include",
23
+ body: JSON.stringify({ id_usuario: d })
19
24
  });
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;
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");
32
28
  };
33
- return v(() => {
29
+ return b(() => {
34
30
  (async () => {
35
31
  try {
36
- if (!(await fetch(`${s}api-auth/auth/validate_jwt/`, {
32
+ if ((await fetch(`${s}api-auth/auth/validate_jwt/`, {
37
33
  method: "POST",
38
- headers: { "Content-Type": "application/json" },
34
+ headers: {
35
+ "Content-Type": "application/json"
36
+ },
39
37
  credentials: "include"
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));
38
+ })).ok) {
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;
51
+ } else
52
+ throw new Error("Token inválido");
45
53
  } catch {
46
54
  try {
47
- if (await m()) {
55
+ if (await g()) {
48
56
  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;
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);
54
66
  } else
55
- sessionStorage.removeItem("user_token"), window.location.href = s, c(!1);
67
+ a(!1), sessionStorage.removeItem("user_token"), window.location.href = s;
56
68
  } else
57
69
  throw new Error("No se pudo refrescar el token.");
58
70
  } catch {
@@ -60,8 +72,8 @@ const $ = () => {
60
72
  }
61
73
  }
62
74
  })();
63
- }, [p, d.pathname]), w ? /* @__PURE__ */ _(g, {}) : null;
75
+ }, [v]), k ? /* @__PURE__ */ S($, {}) : null;
64
76
  };
65
77
  export {
66
- $ as ProtectedRoute
78
+ W as ProtectedRoute
67
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-1",
5
+ "version": "1.7.18.pre-2",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {