diff --git a/js/index.js b/js/index.js index be92f1a..ee6a39a 100644 --- a/js/index.js +++ b/js/index.js @@ -54,4 +54,19 @@ function login() { pin = ""; } }); +} + +function signIn(user, password) { + $.post({ + urlPath: 'vnUsers/sign-in', + jsonData: {user, password}, + processData: false, + success: function (data) { + localStorage.setItem("token", data.token); + localStorage.setItem("ttl", data.ttl); + localStorage.setItem("password", password); + localStorage.setItem("created", Date.now()); + getTokenConfig(); + }, + }) } \ No newline at end of file diff --git a/js/main.js b/js/main.js index b1bc87c..28c1d25 100644 --- a/js/main.js +++ b/js/main.js @@ -1,4 +1,6 @@ const user = "timeControl"; +const renewPeriod = localStorage.getItem('renewPeriod'); +let intervalId, isCheckingToken; function confirmReset() { $(".confirm").removeClass('confirmKO'); @@ -16,20 +18,50 @@ function printError(msg){ }, 2300); } -function signIn(user, password) { +function renewToken() { $.post({ - urlPath: 'vnUsers/sign-in', - jsonData: {user, password}, + urlPath: 'vnUsers/renewToken', processData: false, success: function (data) { - localStorage.setItem("token", data.token); + localStorage.setItem("token", data.id); localStorage.setItem("ttl", data.ttl); - localStorage.setItem("password", password); - localStorage.setItem("signInTime", Date.now()); + localStorage.setItem("created", Date.now()); }, }) } +function getTokenConfig() { + const filter = {fields: ['renewInterval', 'renewPeriod']}; + $.get({ + urlPath: 'AccessTokenConfigs/findOne', + jsonData: filter, + processData: false, + success: function (data) { + if (!data) return; + localStorage.setItem('renewPeriod', data.renewPeriod); + clearInterval(intervalId); + intervalId = setInterval(() => checkValidity(), data.renewInterval * 10); + }, + }) +} + +function checkValidity() { + 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; + const now = new Date(); + + if (now.getTime() <= maxDate) return isCheckingToken = false; + + renewToken(); + isCheckingToken = false; +} + $.ajaxPrefilter(function(xhr) { const orgErrorHandler = xhr.error; const token = localStorage.getItem('token') @@ -53,6 +85,8 @@ $.ajaxPrefilter(function(xhr) { } } + if(xhr?.responseJSON.error.code == 'periodNotExceeded') return; + switch (textStatus){ case 'parsererror': mensaje = 'Requested JSON parse failed'; @@ -82,19 +116,9 @@ $.ajaxPrefilter(function(xhr) { mensaje = 'Ha ocurrido un error, consulta con informática'; } printError(mensaje); + } }); }); -setInterval(function() { - const ttl = localStorage.getItem('ttl'); - const password = localStorage.getItem('password'); - - if(ttl && password) { - const now = Date.now(); - const signInTime = localStorage.getItem('signInTime'); - const countdown = now - signInTime; - - if(countdown < ttl) signIn(user, password) - } -},3600000)// 1 hour \ No newline at end of file +if(renewPeriod) getTokenConfig(); \ No newline at end of file