diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 62751c936..c3b25bf96 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -27,17 +27,13 @@ module.exports = Self => { const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod']}); - if (differenceSeconds <= accessTokenConfig.renewPeriod) + if (differenceSeconds < accessTokenConfig.renewPeriod) throw new UserError(`The renew period has not been exceeded`); await Self.logout(tokenId); const user = await Self.findById(userId); const accessToken = await user.createAccessToken(); - const renewPeriod = accessToken.ttl > accessTokenConfig.renewPeriod - ? accessToken.ttl - : accessTokenConfig.renewPeriod; - - return {token: accessToken.id, renewPeriod}; + return {token: accessToken.id}; }; }; diff --git a/back/methods/vn-user/signIn.js b/back/methods/vn-user/signIn.js index 0abb1a95f..e52d68df5 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, ttl: token.ttl}; + return {token: token.id, ttl: token.ttl}; }; }; diff --git a/front/core/services/auth.js b/front/core/services/auth.js index a37098b2e..92ff4b061 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -59,12 +59,13 @@ export default class Auth { password: password || undefined }; + const now = new Date(); return this.$http.post('VnUsers/signIn', params) - .then(json => this.onLoginOk(json, remember)); + .then(json => this.onLoginOk(json, now, remember)); } - onLoginOk(json, remember) { - this.vnToken.set(json.data.token, json.data.created, json.data.ttl, remember); + onLoginOk(json, now, remember) { + this.vnToken.set(json.data.token, now, json.data.ttl, remember); return this.loadAcls().then(() => { let continueHash = this.$state.params.continue; diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index 08430c2e5..950bed666 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -3,7 +3,7 @@ import Component from 'core/lib/component'; import './style.scss'; export class Layout extends Component { - constructor($element, $, vnModules, vnToken) { + constructor($element, $, vnModules) { super($element, $); this.modules = vnModules.get(); } @@ -36,6 +36,7 @@ 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; @@ -44,29 +45,31 @@ export class Layout extends Component { } checkTokenValidity() { - 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 + this.vnToken.renewPeriod = this.vnToken.renewPeriod < this.renewPeriod + ? this.vnToken.renewPeriod + : this.renewPeriod; + + const secondsToRenew = this.vnToken.renewPeriod; + const milisecondsToRenew = secondsToRenew * 1000; + const created = new Date(this.vnToken.created); + const createdInMiliseconds = created.getTime(); + const maxDateInMiliseconds = createdInMiliseconds + milisecondsToRenew; + const maxDate = new Date(maxDateInMiliseconds); const now = new Date(); - console.log(now > nuevaFecha, now, nuevaFecha); - if (now > nuevaFecha) { + if (now > maxDate) { 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, now, json.data.renewPeriod, remember); + this.vnToken.set(json.data.token, now, this.vnToken.renewPeriod, remember); } }) - .catch(res => { - if (res.status !== 405) - throw res; + .catch(err => { + if (err.status !== 400) + throw err; }); } }