react-ecosistema-unp 1.6.0-pre.7 → 1.6.0-pre.9

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,22 +1,20 @@
1
- import { jsx as n } from "react/jsx-runtime";
2
- import { createContext as c, useState as o, useEffect as l } from "react";
3
- const u = c(void 0), g = ({ children: r }) => {
4
- const [e, s] = o(
1
+ import { jsx as u } from "react/jsx-runtime";
2
+ import { createContext as a, useState as e } from "react";
3
+ const c = a(void 0), i = ({ children: t }) => {
4
+ const [o, r] = e(
5
5
  localStorage.getItem("access_token")
6
- ), [t, a] = o(
6
+ ), [n, s] = e(
7
7
  localStorage.getItem("user_token")
8
8
  );
9
- return l(() => {
10
- e && t ? (localStorage.setItem("access_token", e), localStorage.setItem("user_token", t)) : (localStorage.removeItem("access_token"), localStorage.removeItem("user_token"));
11
- }, [e, t]), /* @__PURE__ */ n(
12
- u.Provider,
9
+ return /* @__PURE__ */ u(
10
+ c.Provider,
13
11
  {
14
- value: { authToken: e, setAuthToken: s, userToken: t, setUserToken: a },
15
- children: r
12
+ value: { authToken: o, setAuthToken: r, userToken: n, setUserToken: s },
13
+ children: t
16
14
  }
17
15
  );
18
16
  };
19
17
  export {
20
- u as AuthContext,
21
- g as AuthProvider
18
+ c as AuthContext,
19
+ i as AuthProvider
22
20
  };
@@ -1,9 +1,10 @@
1
- import { jsx as h } from "react/jsx-runtime";
2
- import { useState as m, useEffect as p } from "react";
1
+ import { jsx as m } from "react/jsx-runtime";
2
+ import { useState as h, useEffect as p } from "react";
3
3
  import { useNavigate as w, useLocation as u, Outlet as k } from "react-router-dom";
4
4
  import { j as v } from "../../index-CuHybtft.js";
5
- const V = () => {
6
- const [i, e] = m(!1), r = w(), c = u(), o = `${window.location.protocol}//${window.location.host}/`;
5
+ import "./fetchInterceptor.js";
6
+ const W = () => {
7
+ const [i, e] = h(!1), r = w(), c = u(), o = `${window.location.protocol}//${window.location.host}/`;
7
8
  return p(() => {
8
9
  (async () => {
9
10
  try {
@@ -34,8 +35,8 @@ const V = () => {
34
35
  } finally {
35
36
  }
36
37
  })();
37
- }, [r]), i ? /* @__PURE__ */ h(k, {}) : null;
38
+ }, [r]), i ? /* @__PURE__ */ m(k, {}) : null;
38
39
  };
39
40
  export {
40
- V as ProtectedRoute
41
+ W as ProtectedRoute
41
42
  };
@@ -11,12 +11,13 @@ async function k() {
11
11
  const n = await r.json();
12
12
  if (!n.token)
13
13
  throw new Error("No se recibió token en la respuesta de refresh");
14
- return `${n.token}`;
14
+ const t = n.token;
15
+ return sessionStorage.setItem("user_token", `${t}`), `${t}`;
15
16
  }
16
- window.fetch = async function(t, e) {
17
- const r = typeof t == "string" ? t : t.toString();
18
- if (r.includes("/auth/refresh_token") || r.includes("/auth/validate"))
19
- return window.originalFetch(t, e);
17
+ window.fetch = async function(o, e) {
18
+ const r = typeof o == "string" ? o : o.toString();
19
+ if (console.log("URL petición:", r), r.includes("/api-auth/"))
20
+ return window.originalFetch(o, e);
20
21
  e = e || {};
21
22
  const n = sessionStorage.getItem("user_token");
22
23
  e.headers = {
@@ -24,9 +25,9 @@ window.fetch = async function(t, e) {
24
25
  ...n ? { Authorization: n } : {}
25
26
  };
26
27
  try {
27
- const o = await window.originalFetch(t, e);
28
- return o.status !== 401 && !(await o.text()).includes("Signature has expired") ? o : new Promise((l, w) => {
29
- s.push({ input: t, init: e, resolve: l, reject: w }), c || (c = !0, h = k(), h.then((a) => {
28
+ const t = await window.originalFetch(o, e);
29
+ return console.log("Response petición:", t), t.status !== 401 && !(await t.text()).includes("Signature has expired") ? t : new Promise((l, w) => {
30
+ s.push({ input: o, init: e, resolve: l, reject: w }), c || (c = !0, h = k(), h.then((a) => {
30
31
  s.forEach(({ input: i, init: u, resolve: d, reject: f }) => {
31
32
  const g = {
32
33
  ...u,
@@ -40,7 +41,7 @@ window.fetch = async function(t, e) {
40
41
  s.length = 0, c = !1, h = null;
41
42
  }));
42
43
  });
43
- } catch (o) {
44
- return Promise.reject(o);
44
+ } catch (t) {
45
+ return Promise.reject(t);
45
46
  }
46
47
  };
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.6.0-pre.7",
5
+ "version": "1.6.0-pre.9",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {