react-ecosistema-unp 1.5.11 → 1.6.0-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,22 +1,22 @@
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(
5
- localStorage.getItem("access_token")
6
- ), [t, a] = o(
7
- localStorage.getItem("user_token")
1
+ import { jsx as a } from "react/jsx-runtime";
2
+ import { createContext as u, useState as s, useEffect as c } from "react";
3
+ const i = u(void 0), g = ({ children: o }) => {
4
+ const [e, n] = s(
5
+ sessionStorage.getItem("access_token")
6
+ ), [t, r] = s(
7
+ sessionStorage.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 c(() => {
10
+ e && t ? (sessionStorage.setItem("access_token", e), sessionStorage.setItem("user_token", t)) : (sessionStorage.removeItem("access_token"), sessionStorage.removeItem("user_token"));
11
+ }, [e, t]), /* @__PURE__ */ a(
12
+ i.Provider,
13
13
  {
14
- value: { authToken: e, setAuthToken: s, userToken: t, setUserToken: a },
15
- children: r
14
+ value: { authToken: e, setAuthToken: n, userToken: t, setUserToken: r },
15
+ children: o
16
16
  }
17
17
  );
18
18
  };
19
19
  export {
20
- u as AuthContext,
20
+ i as AuthContext,
21
21
  g as AuthProvider
22
22
  };
@@ -0,0 +1,6 @@
1
+ declare global {
2
+ interface Window {
3
+ originalFetch: typeof fetch;
4
+ }
5
+ }
6
+ export {};
@@ -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
  };
@@ -1,8 +1,9 @@
1
1
  import { jsx as a } from "react/jsx-runtime";
2
2
  import { useState as i, useEffect as s } from "react";
3
3
  import { useNavigate as l, Outlet as c } from "react-router-dom";
4
- const h = () => {
5
- const [e, n] = i(!1), r = l(), o = `${window.location.protocol}//${window.location.host}/`;
4
+ import "./fetchInterceptor.js";
5
+ const w = () => {
6
+ const [e, r] = i(!1), n = l(), o = `${window.location.protocol}//${window.location.host}/`;
6
7
  return s(() => {
7
8
  (async () => {
8
9
  try {
@@ -16,7 +17,7 @@ const h = () => {
16
17
  credentials: "include"
17
18
  }
18
19
  )).ok)
19
- n(!0);
20
+ r(!0);
20
21
  else
21
22
  return window.location.href = o;
22
23
  } catch (t) {
@@ -24,8 +25,8 @@ const h = () => {
24
25
  } finally {
25
26
  }
26
27
  })();
27
- }, [r]), e ? /* @__PURE__ */ a(c, {}) : null;
28
+ }, [n]), e ? /* @__PURE__ */ a(c, {}) : null;
28
29
  };
29
30
  export {
30
- h as UserRoute
31
+ w as UserRoute
31
32
  };
@@ -0,0 +1,41 @@
1
+ window.originalFetch = window.fetch.bind(window);
2
+ let c = !1, i = null;
3
+ const o = [];
4
+ async function g() {
5
+ const t = `${window.location.protocol}//${window.location.host}/`, e = await window.originalFetch(
6
+ `${t}api-auth/auth/refresh_token/`,
7
+ {
8
+ method: "POST",
9
+ credentials: "include"
10
+ }
11
+ );
12
+ if (!e.ok)
13
+ throw new Error("Error al refrescar token");
14
+ const r = (await e.json()).token;
15
+ return sessionStorage.setItem("user_token", `Bearer ${r}`), `Bearer ${r}`;
16
+ }
17
+ window.fetch = async function(t, e) {
18
+ e = e || {};
19
+ const n = sessionStorage.getItem("user_token");
20
+ e.headers = {
21
+ ...e.headers,
22
+ ...n ? { Authorization: n } : {}
23
+ };
24
+ try {
25
+ const r = await window.originalFetch(t, e);
26
+ return r.status !== 401 ? r : new Promise((w, l) => {
27
+ o.push({ input: t, init: e, resolve: w, reject: l }), c || (c = !0, i = g(), i.then((s) => {
28
+ o.forEach(({ input: a, init: h, resolve: u, reject: d }) => {
29
+ const f = { ...h, headers: { ...h.headers, Authorization: s } };
30
+ window.originalFetch(a, f).then(u).catch(d);
31
+ });
32
+ }).catch((s) => {
33
+ o.forEach(({ reject: a }) => a(s));
34
+ }).finally(() => {
35
+ o.length = 0, c = !1, i = null;
36
+ }));
37
+ });
38
+ } catch (r) {
39
+ return Promise.reject(r);
40
+ }
41
+ };
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.5.11",
5
+ "version": "1.6.0-pre.1",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {