react-ecosistema-unp 1.8.0-pre.13 → 1.8.0-pre.14

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,10 +1,10 @@
1
1
  import { jsx as w } from "react/jsx-runtime";
2
- import { useState as p, useEffect as S } from "react";
3
- import { useLocation as v, Outlet as I } from "react-router-dom";
4
- import _ from "../fingerprint/fingerprintService.js";
2
+ import { useState as S, useEffect as p } from "react";
3
+ import { useLocation as _, Outlet as v } from "react-router-dom";
4
+ import I from "../fingerprint/fingerprintService.js";
5
5
  const E = () => {
6
- const [g, o] = p(!1), d = v(), r = `${window.location.protocol}//${window.location.host}/`, c = `${window.location.host}`, k = async () => {
7
- const m = await _.getFingerprint(), h = localStorage.getItem("refresh_token"), s = await fetch(`${r}api-auth/auth/refresh_token/`, {
6
+ const [k, o] = S(!1), d = _(), a = `${window.location.protocol}//${window.location.host}/`, l = `${window.location.host}`, g = async () => {
7
+ const m = await I.getFingerprint(), h = localStorage.getItem("refresh_token"), s = await fetch(`${a}api-auth/auth/refresh_token/`, {
8
8
  method: "POST",
9
9
  headers: {
10
10
  "Content-Type": "application/json",
@@ -17,49 +17,49 @@ const E = () => {
17
17
  })
18
18
  });
19
19
  if (s.ok) {
20
- const l = (await s.json()).token;
21
- return localStorage.setItem("access_token", l), !0;
20
+ const n = await s.json(), i = n.access_token, r = n.refresh_token;
21
+ return localStorage.setItem("access_token", i), localStorage.setItem("refresh_token", r), !0;
22
22
  } else
23
23
  throw new Error("Error al refrescar token de Microsoft");
24
24
  };
25
- return S(() => {
25
+ return p(() => {
26
26
  const m = async () => {
27
27
  try {
28
28
  if (localStorage.getItem("access_token")) {
29
29
  const s = localStorage.getItem("auth_state");
30
30
  if (s) {
31
- const l = JSON.parse(s).user.servicios.map((e) => e.url).filter((e) => !!e), a = d.pathname, n = l.map(
31
+ const i = JSON.parse(s).user.servicios.map((e) => e.url).filter((e) => !!e), r = d.pathname, c = i.map(
32
32
  (e) => e.startsWith("/") ? e : `/${e}`
33
- ), f = a.startsWith("/") ? a : `/${a}`;
34
- n.some(
33
+ ), f = r.startsWith("/") ? r : `/${r}`;
34
+ c.some(
35
35
  (e) => f.startsWith(e)
36
- ) ? o(!0) : (console.warn("No hay match con los roles del usuario"), o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = r);
36
+ ) ? o(!0) : (console.warn("No hay match con los roles del usuario"), o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = a);
37
37
  } else
38
- o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = r;
38
+ o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = a;
39
39
  } else
40
40
  throw new Error("Token inválido");
41
41
  } catch {
42
42
  try {
43
- if (await k()) {
44
- const i = localStorage.getItem("auth_state");
45
- if (i) {
46
- const a = JSON.parse(i).user.servicios.map((t) => t.url).filter((t) => !!t), n = d.pathname, f = a.map(
43
+ if (await g()) {
44
+ const n = localStorage.getItem("auth_state");
45
+ if (n) {
46
+ const r = JSON.parse(n).user.servicios.map((t) => t.url).filter((t) => !!t), c = d.pathname, f = r.map(
47
47
  (t) => t.startsWith("/") ? t : `/${t}`
48
- ), u = n.startsWith("/") ? n : `/${n}`;
48
+ ), u = c.startsWith("/") ? c : `/${c}`;
49
49
  f.some(
50
50
  (t) => u.startsWith(t)
51
- ) ? o(!0) : (o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = r);
51
+ ) ? o(!0) : (o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = a);
52
52
  } else
53
- o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = r;
53
+ o(!1), sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = a;
54
54
  } else
55
55
  throw new Error("No se pudo refrescar el token.");
56
56
  } catch {
57
- sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = r, o(!1);
57
+ sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = a, o(!1);
58
58
  }
59
59
  }
60
60
  };
61
- c.includes("ecosistemadesarrollo") || c.includes("ecosistemapruebas") || c.includes("ecosistemaprov") || c.includes("ecosistema") ? m() : (console.warn("ProtectedRoute is disabled in the local development environment."), o(!0));
62
- }, []), g ? /* @__PURE__ */ w(I, {}) : null;
61
+ l.includes("ecosistemadesarrollo") || l.includes("ecosistemapruebas") || l.includes("ecosistemaprov") || l.includes("ecosistema") ? m() : (console.warn("ProtectedRoute is disabled in the local development environment."), o(!0));
62
+ }, []), k ? /* @__PURE__ */ w(v, {}) : null;
63
63
  };
64
64
  export {
65
65
  E as ProtectedRoute
@@ -1,29 +1,31 @@
1
- import { jsx as c } from "react/jsx-runtime";
2
- import { useState as d, useEffect as f } from "react";
3
- import { useNavigate as h, Outlet as m } from "react-router-dom";
4
- import u from "../fingerprint/fingerprintService.js";
5
- const S = () => {
6
- const [a, e] = d(!1), i = h(), t = `${window.location.protocol}//${window.location.host}/`, n = `${window.location.host}`;
7
- if (n.includes("localhost"))
1
+ import { jsx as m } from "react/jsx-runtime";
2
+ import { useState as d, useEffect as u } from "react";
3
+ import { useNavigate as w, Outlet as k } from "react-router-dom";
4
+ import p from "../fingerprint/fingerprintService.js";
5
+ const I = () => {
6
+ const [c, e] = d(!1), i = w(), t = `${window.location.protocol}//${window.location.host}/`, a = `${window.location.host}`;
7
+ if (a.includes("localhost"))
8
8
  return console.warn("ProtectedRoute is disabled in the local development environment."), null;
9
9
  const l = async () => {
10
- const o = await u.getFingerprint(), r = localStorage.getItem("refresh_token");
11
- if ((await fetch(`${t}api-auth/auth/refresh_token/`, {
10
+ const r = await p.getFingerprint(), n = localStorage.getItem("refresh_token"), o = await fetch(`${t}api-auth/auth/refresh_token/`, {
12
11
  method: "POST",
13
12
  headers: {
14
13
  "Content-Type": "application/json",
15
- "X-Fingerprint": o
14
+ "X-Fingerprint": r
16
15
  },
17
16
  credentials: "include",
18
17
  body: JSON.stringify({
19
- refresh: r
18
+ refresh: n
20
19
  })
21
- })).ok)
22
- return !0;
23
- throw new Error("Error al refrescar token de Microsoft");
20
+ });
21
+ if (o.ok) {
22
+ const s = await o.json(), f = s.access_token, h = s.refresh_token;
23
+ return localStorage.setItem("access_token", f), localStorage.setItem("refresh_token", h), !0;
24
+ } else
25
+ throw new Error("Error al refrescar token de Microsoft");
24
26
  };
25
- return f(() => {
26
- const o = async () => {
27
+ return u(() => {
28
+ const r = async () => {
27
29
  try {
28
30
  if (localStorage.getItem("access_token"))
29
31
  e(!0);
@@ -49,9 +51,9 @@ const S = () => {
49
51
  sessionStorage.removeItem("user_token"), localStorage.removeItem("auth_state"), localStorage.removeItem("access_token"), window.location.href = t, e(!1);
50
52
  }
51
53
  };
52
- n.includes("localhost") ? (console.warn("ProtectedRoute is disabled in the local development environment."), e(!0)) : o();
53
- }, [i]), a ? /* @__PURE__ */ c(m, {}) : null;
54
+ a.includes("localhost") ? (console.warn("ProtectedRoute is disabled in the local development environment."), e(!0)) : r();
55
+ }, [i]), c ? /* @__PURE__ */ m(k, {}) : null;
54
56
  };
55
57
  export {
56
- S as UserRoute
58
+ I as UserRoute
57
59
  };
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.8.0-pre.13",
5
+ "version": "1.8.0-pre.14",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {