From 6b61d4cca1326decd0baec8f90bad67ab13e639a Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 27 Jun 2023 15:34:35 +0200 Subject: [PATCH] fix: muestra mensaje de error --- back/methods/vn-user/renew-token.js | 6 +++++- back/methods/vn-user/signIn.js | 2 +- front/core/services/auth.js | 2 +- front/core/services/token.js | 8 +++++++- front/salix/components/layout/index.js | 22 ++++++++++++++++------ 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 41470dfea..62751c936 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -34,6 +34,10 @@ module.exports = Self => { const user = await Self.findById(userId); const accessToken = await user.createAccessToken(); - return {token: accessToken.id, created: accessToken.created}; + const renewPeriod = accessToken.ttl > accessTokenConfig.renewPeriod + ? accessToken.ttl + : accessTokenConfig.renewPeriod; + + return {token: accessToken.id, renewPeriod}; }; }; diff --git a/back/methods/vn-user/signIn.js b/back/methods/vn-user/signIn.js index c98f1da54..0abb1a95f 100644 --- a/back/methods/vn-user/signIn.js +++ b/back/methods/vn-user/signIn.js @@ -76,6 +76,6 @@ module.exports = Self => { let loginInfo = Object.assign({password}, userInfo); token = await Self.login(loginInfo, 'user'); - return {token: token.id, created: token.created}; + return {token: token.id, created: token.created, ttl: token.ttl}; }; }; diff --git a/front/core/services/auth.js b/front/core/services/auth.js index ef6c07637..a37098b2e 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -64,7 +64,7 @@ export default class Auth { } onLoginOk(json, remember) { - this.vnToken.set(json.data.token, json.data.created, remember); + this.vnToken.set(json.data.token, json.data.created, json.data.ttl, remember); return this.loadAcls().then(() => { let continueHash = this.$state.params.continue; diff --git a/front/core/services/token.js b/front/core/services/token.js index c1bb5a173..12696faec 100644 --- a/front/core/services/token.js +++ b/front/core/services/token.js @@ -10,32 +10,38 @@ export default class Token { try { this.token = sessionStorage.getItem('vnToken'); this.created = sessionStorage.getItem('vnTokenCreated'); + this.renewPeriod = sessionStorage.getItem('vnTokenRenewPeriod'); if (!this.token) { this.token = localStorage.getItem('vnToken'); this.created = localStorage.getItem('vnTokenCreated'); + this.renewPeriod = localStorage.getItem('vnTokenRenewPeriod'); } } catch (e) {} } - set(token, created, remember) { + set(token, created, renewPeriod, remember) { this.unset(); try { if (remember) { localStorage.setItem('vnToken', token); localStorage.setItem('vnTokenCreated', created); + localStorage.setItem('vnTokenRenewPeriod', renewPeriod); } else { sessionStorage.setItem('vnToken', token); sessionStorage.setItem('vnTokenCreated', created); + sessionStorage.setItem('vnTokenRenewPeriod', renewPeriod); } } catch (e) {} this.token = token; this.created = created; + this.renewPeriod = renewPeriod; } unset() { localStorage.removeItem('vnToken'); sessionStorage.removeItem('vnToken'); this.token = null; this.created = null; + this.renewPeriod = null; } } diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index dc2313f4f..099e3fd6e 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -36,7 +36,6 @@ export class Layout extends Component { this.$http.get('AccessTokenConfigs').then(json => { const firtsResult = json.data[0]; if (!firtsResult) return; - this.renewPeriod = firtsResult.renewPeriod; this.renewInterval = firtsResult.renewInterval; const intervalMilliseconds = firtsResult.renewInterval * 1000; @@ -45,18 +44,29 @@ export class Layout extends Component { } checkTokenValidity() { - const now = new Date(); - const differenceMilliseconds = now - new Date(this.vnToken.created); - const differenceSeconds = Math.floor(differenceMilliseconds / 1000); + console.log(this.vnToken.renewPeriod); + const fecha = new Date(this.vnToken.created); // Fecha representada por un objeto Date + const duracionEnSegundos = this.vnToken.renewPeriod; // Duración en segundos (número) + const fechaEnMilisegundos = fecha.getTime(); // Convertir fecha a milisegundos + const duracionEnMilisegundos = duracionEnSegundos * 1000; // Convertir duración a milisegundos + const nuevaFechaEnMilisegundos = fechaEnMilisegundos + duracionEnMilisegundos; // Sumar los milisegundos + const nuevaFecha = new Date(nuevaFechaEnMilisegundos); // Crear una nueva fecha a partir de los milisegundos - if (differenceSeconds > this.renewPeriod) { + // const created = new Date(this.vnToken.created); + // const createdInSeconds = Math.floor(created.getTime() / 1000); + // const maxSeconds = createdInSeconds + this.vnToken.ttl; + // const maxDate = new Date(maxSeconds); + + const now = new Date(); + console.log(now > nuevaFecha, now, nuevaFecha); + if (now > nuevaFecha) { this.$http.post('VnUsers/renewToken') .then(json => { if (json.data.token) { let remember = true; if (window.sessionStorage.vnToken) remember = false; - this.vnToken.set(json.data.token, json.data.created, remember); + this.vnToken.set(json.data.token, new Date(), json.data.renewPeriod, remember); } }); }