const renewPeriod = localStorage.getItem("renewPeriod"); let intervalId, isCheckingToken; const txtConfirm = document.querySelector(".txtConfirm"); const confirm = document.querySelector(".confirm"); function confirmReset() { confirm.classList.remove("confirmKO"); confirm.style.display = "none"; txtConfirm.textContent = ""; } // WIP: fadeIn / fadeOut function printError(msg) { confirmReset(); const txtConfirm = document.querySelector(".txtConfirm"); txtConfirm.textContent = msg; const confirm = document.querySelector(".confirm"); confirm.classList.add("confirmKO"); confirm.style.display = "block"; $(".confirm").fadeIn(200); setTimeout(() => { $(".confirm").fadeOut(200); setTimeout(confirmReset, 200); }, 2300); } async function renewToken() { const res = await call("AccessTokens/renewToken", {}); const data = await res.json(); localStorage.setItem("ttl", data.ttl); localStorage.setItem("created", Date.now()); } async function getTokenConfig() { const res = await call("AccessTokenConfigs/findOne", { params: { filter: { fields: ["renewInterval", "renewPeriod"] } }, }); const data = await res.json(); 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; if (new Date().getTime() <= maxDate) return (isCheckingToken = false); renewToken(); isCheckingToken = false; }, data.renewInterval * 1000); } /// WIP: MUST try thru it! async function call(url, { method = "GET", body = {}, params = {} }) { const controller = new AbortController(); const { signal } = controller; const timeoutId = setTimeout(() => controller.abort(), 2000); const opts = { method, headers: { Authorization: localStorage.getItem("token"), "Content-Type": "application/json; charset=utf-8", }, signal, }; if (method === "GET") { const searchParams = new URLSearchParams(); for (let [key, value] of Object.entries(params)) { searchParams.append(key, typeof value === "object" ? JSON.stringify(value) : value); } url += "?" + searchParams.toString(); } else if (method === "POST") opts.body = JSON.stringify(body); try { const res = await fetch(`/api/${url}`, opts); clearTimeout(timeoutId); return res; } catch (e) { let mensaje = "Ha ocurrido un error, consulta con informática"; 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; default: switch (e.code) { case 0: mensaje = "Not connect: Verify Network"; break; case 504: mensaje = "No se ha podido conectar con Salix, consulta con informática"; break; case 555: mensaje = "Error 555"; break; default: if (e.status >= 400 && e.status < 500) mensaje = e.message; } } printError(mensaje); } } if (renewPeriod) getTokenConfig();