From 57df79806a8159e00d2322b07d638039c4a10e4f Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 29 Nov 2023 13:38:53 +0100 Subject: [PATCH] refs #6274 signIn refactor --- js/index.js | 18 +----------------- js/main.js | 50 +++++++++++++++++++++++++------------------------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/js/index.js b/js/index.js index b1e7a0c..900e8c0 100644 --- a/js/index.js +++ b/js/index.js @@ -5,7 +5,6 @@ var pass = ""; $(document).ready(function () { FastClick.attach(document.body); if(!localStorage.getItem('user')) localStorage.setItem('user', user); - renewToken() setEvents(); }); @@ -65,19 +64,4 @@ 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("signInTime", Date.now()); - }, - }) -} -renewToken() \ No newline at end of file +} \ No newline at end of file diff --git a/js/main.js b/js/main.js index e8f21b4..42c99c5 100644 --- a/js/main.js +++ b/js/main.js @@ -16,31 +16,18 @@ function printError(msg){ }, 2300); } -function renewToken() { - 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) { - $.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("signInTime", Date.now()); - }, - }) - } - } - },3600000)// 1 hour - +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("signInTime", Date.now()); + }, + }) } $.ajaxPrefilter(function(xhr) { @@ -98,3 +85,16 @@ $.ajaxPrefilter(function(xhr) { } }); }); + +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