2024-06-04 12:38:09 +00:00
|
|
|
const renewPeriod = localStorage.getItem("renewPeriod");
|
2023-12-05 14:56:51 +00:00
|
|
|
let intervalId, isCheckingToken;
|
2024-06-04 12:38:09 +00:00
|
|
|
const txtConfirm = document.querySelector(".txtConfirm");
|
2024-06-18 14:39:32 +00:00
|
|
|
const confirmBtn = document.querySelector(".confirm");
|
2023-11-29 08:26:04 +00:00
|
|
|
|
2021-10-20 13:13:30 +00:00
|
|
|
function confirmReset() {
|
2024-06-18 14:39:32 +00:00
|
|
|
confirmBtn.classList.remove("confirmKO", "fade-in", "fade-out");
|
2024-06-18 15:00:09 +00:00
|
|
|
confirmBtn.style.hidden = true;
|
2024-06-04 12:38:09 +00:00
|
|
|
txtConfirm.textContent = "";
|
2021-10-20 13:13:30 +00:00
|
|
|
}
|
2021-10-19 08:52:45 +00:00
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
function printError(msg) {
|
2021-10-20 13:13:30 +00:00
|
|
|
confirmReset();
|
2024-06-04 12:38:09 +00:00
|
|
|
const txtConfirm = document.querySelector(".txtConfirm");
|
|
|
|
txtConfirm.textContent = msg;
|
2024-06-18 14:39:32 +00:00
|
|
|
confirmBtn.classList.add("confirmKO");
|
2024-06-18 15:00:09 +00:00
|
|
|
confirmBtn.style.hidden = false;
|
2024-06-18 14:39:32 +00:00
|
|
|
|
|
|
|
confirmBtn.classList.add("fade-in");
|
2024-06-19 07:52:07 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
confirmBtn.classList.add("fade-out");
|
|
|
|
setTimeout(confirmReset, 200);
|
|
|
|
}, 2300);
|
2021-10-19 08:52:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
async function renewToken() {
|
2024-06-19 07:52:07 +00:00
|
|
|
const data = await call("vnUsers/renewToken", { method: "POST" });
|
2023-11-29 08:26:04 +00:00
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
localStorage.setItem("ttl", data.ttl);
|
|
|
|
localStorage.setItem("created", Date.now());
|
2023-12-05 14:56:51 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
async function getTokenConfig() {
|
2024-06-18 14:39:32 +00:00
|
|
|
const data = await call("AccessTokenConfigs/findOne", {
|
2024-06-14 15:01:06 +00:00
|
|
|
params: { filter: { fields: ["renewInterval", "renewPeriod"] } },
|
2024-06-04 12:38:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
localStorage.setItem("renewPeriod", data.renewPeriod);
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
|
|
|
intervalId = setInterval(() => {
|
|
|
|
const created = +localStorage.getItem("created");
|
|
|
|
const ttl = localStorage.getItem("ttl");
|
|
|
|
|
|
|
|
if (isCheckingToken || !created) return;
|
|
|
|
isCheckingToken = true;
|
|
|
|
|
|
|
|
const renewPeriodInSeconds = Math.min(ttl, renewPeriod) * 1000;
|
|
|
|
const maxDate = created + renewPeriodInSeconds;
|
2023-12-05 14:56:51 +00:00
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
if (new Date().getTime() <= maxDate) return (isCheckingToken = false);
|
2022-12-12 09:21:02 +00:00
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
renewToken();
|
|
|
|
isCheckingToken = false;
|
|
|
|
}, data.renewInterval * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function call(url, { method = "GET", body = {}, params = {} }) {
|
|
|
|
const controller = new AbortController();
|
|
|
|
const { signal } = controller;
|
|
|
|
const timeoutId = setTimeout(() => controller.abort(), 2000);
|
|
|
|
const opts = {
|
|
|
|
method,
|
2021-10-19 08:52:45 +00:00
|
|
|
headers: {
|
2024-06-04 12:38:09 +00:00
|
|
|
Authorization: localStorage.getItem("token"),
|
|
|
|
"Content-Type": "application/json; charset=utf-8",
|
2021-10-19 08:52:45 +00:00
|
|
|
},
|
2024-06-04 12:38:09 +00:00
|
|
|
signal,
|
|
|
|
};
|
|
|
|
|
2024-06-14 15:01:06 +00:00
|
|
|
if (method === "GET") {
|
|
|
|
const searchParams = new URLSearchParams();
|
2024-06-14 15:06:27 +00:00
|
|
|
for (let [key, value] of Object.entries(params)) {
|
|
|
|
searchParams.append(key, typeof value === "object" ? JSON.stringify(value) : value);
|
2024-06-14 15:01:06 +00:00
|
|
|
}
|
|
|
|
url += "?" + searchParams.toString();
|
|
|
|
} else if (method === "POST") opts.body = JSON.stringify(body);
|
2024-06-04 12:38:09 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch(`/api/${url}`, opts);
|
2024-06-18 14:39:32 +00:00
|
|
|
const data = await res.json();
|
|
|
|
|
|
|
|
if (res.ok) return data;
|
2024-06-04 12:38:09 +00:00
|
|
|
clearTimeout(timeoutId);
|
2024-06-18 14:39:32 +00:00
|
|
|
throw data.error;
|
2024-06-04 12:38:09 +00:00
|
|
|
} catch (e) {
|
2024-06-14 13:57:23 +00:00
|
|
|
let mensaje = "Ha ocurrido un error, consulta con informática";
|
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
switch (e.name) {
|
|
|
|
case "SyntaxError":
|
|
|
|
mensaje = "Requested JSON parse failed";
|
|
|
|
break;
|
|
|
|
case "TimeoutError":
|
|
|
|
mensaje = "Time out error";
|
|
|
|
break;
|
|
|
|
case "AbortError":
|
|
|
|
mensaje = "Ajax request aborted";
|
|
|
|
break;
|
|
|
|
case "UserError":
|
|
|
|
mensaje = e.message;
|
|
|
|
break;
|
2024-06-14 13:57:23 +00:00
|
|
|
default:
|
2024-06-18 14:39:32 +00:00
|
|
|
switch (e.statusCode) {
|
2024-06-04 12:38:09 +00:00
|
|
|
case 0:
|
|
|
|
mensaje = "Not connect: Verify Network";
|
|
|
|
break;
|
|
|
|
case 504:
|
|
|
|
mensaje = "No se ha podido conectar con Salix, consulta con informática";
|
2024-01-31 08:04:49 +00:00
|
|
|
break;
|
2024-06-04 12:38:09 +00:00
|
|
|
case 555:
|
2024-06-14 13:57:23 +00:00
|
|
|
mensaje = "Error 555";
|
2024-06-04 12:38:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-06-18 14:39:32 +00:00
|
|
|
if (e.statusCode >= 400 && e.statusCode < 500) mensaje = e.message;
|
2024-06-04 12:38:09 +00:00
|
|
|
}
|
2021-10-19 08:52:45 +00:00
|
|
|
}
|
2024-06-04 12:38:09 +00:00
|
|
|
printError(mensaje);
|
2024-06-18 14:39:32 +00:00
|
|
|
throw e;
|
2024-06-04 12:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-29 12:38:53 +00:00
|
|
|
|
2024-06-04 12:38:09 +00:00
|
|
|
if (renewPeriod) getTokenConfig();
|