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 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 () => {
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
- return !0;
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
- }
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 b(() => {
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
- 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
- }
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 k()) {
70
- const r = sessionStorage.getItem("user_token"), a = d(r, c.pathname, s);
71
- l(a);
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
- }, [w, c]), f ? /* @__PURE__ */ S(U, {}) : null;
63
+ }, [p, d.pathname]), w ? /* @__PURE__ */ _(g, {}) : null;
80
64
  };
81
65
  export {
82
- V as ProtectedRoute
66
+ $ as ProtectedRoute
83
67
  };
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-1",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {