let pin = ""; if ("addEventListener" in document) { document.addEventListener("DOMContentLoaded", () => { FastClick.attach(document.body); const heartEl = document.querySelector("body > h1 > span"); refreshDeviceLabel(); heartEl.addEventListener("click", () => { Swal.fire({ title: "Iniciar sesión", html: ` `, confirmButtonText: "Login", showCloseButton: true, showCancelButton: false, }).then(async (result) => { if (result.isConfirmed) signIn(document.querySelector("#user").value, document.querySelector("#pass").value, document.querySelector("#device").value); }); }); $(".btnnum").on("click", function () { pin += parseInt($(this).children().html()); $("#txtPin").text(pin); }); $(".btnCancel").on("click", function () { pin = ""; $("#txtPin").text("ID USUARIO"); }); $(".btnOk").on("click", function () { if (pin) { login(); } }); }); } async function login() { try { const res = await call("WorkerTimeControls/login", { method: "POST", body: { pin }, }); const data = await res.json(); localStorage.setItem("userData", JSON.stringify(data)); window.location = "clockIn.html"; } catch (e) { document.querySelector("#txtPin").textContent = "ID USUARIO"; pin = ""; } } async function signIn(user, password, device) { const res = await call("vnUsers/sign-in", { method: "POST", body: { user, password }, }); const data = await res.json(); localStorage.setItem("token", data.token); localStorage.setItem("ttl", data.ttl); localStorage.setItem("user", user); localStorage.setItem("device", device); localStorage.setItem("created", Date.now()); getTokenConfig(); refreshDeviceLabel(); } const refreshDeviceLabel = () => (document.querySelector("#deviceLabel").textContent = localStorage.getItem("device") ?? "");