refs #5554 fix: ya no muestra el error `The renew period has not been exceeded`
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2023-06-28 11:56:42 +02:00
parent a9885268c2
commit 66bc2de058
4 changed files with 24 additions and 24 deletions

View File

@ -27,17 +27,13 @@ module.exports = Self => {
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod']}); 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`); throw new UserError(`The renew period has not been exceeded`);
await Self.logout(tokenId); await Self.logout(tokenId);
const user = await Self.findById(userId); const user = await Self.findById(userId);
const accessToken = await user.createAccessToken(); const accessToken = await user.createAccessToken();
const renewPeriod = accessToken.ttl > accessTokenConfig.renewPeriod return {token: accessToken.id};
? accessToken.ttl
: accessTokenConfig.renewPeriod;
return {token: accessToken.id, renewPeriod};
}; };
}; };

View File

@ -76,6 +76,6 @@ module.exports = Self => {
let loginInfo = Object.assign({password}, userInfo); let loginInfo = Object.assign({password}, userInfo);
token = await Self.login(loginInfo, 'user'); token = await Self.login(loginInfo, 'user');
return {token: token.id, created: token.created, ttl: token.ttl}; return {token: token.id, ttl: token.ttl};
}; };
}; };

View File

@ -59,12 +59,13 @@ export default class Auth {
password: password || undefined password: password || undefined
}; };
const now = new Date();
return this.$http.post('VnUsers/signIn', params) return this.$http.post('VnUsers/signIn', params)
.then(json => this.onLoginOk(json, remember)); .then(json => this.onLoginOk(json, now, remember));
} }
onLoginOk(json, remember) { onLoginOk(json, now, remember) {
this.vnToken.set(json.data.token, json.data.created, json.data.ttl, remember); this.vnToken.set(json.data.token, now, json.data.ttl, remember);
return this.loadAcls().then(() => { return this.loadAcls().then(() => {
let continueHash = this.$state.params.continue; let continueHash = this.$state.params.continue;

View File

@ -3,7 +3,7 @@ import Component from 'core/lib/component';
import './style.scss'; import './style.scss';
export class Layout extends Component { export class Layout extends Component {
constructor($element, $, vnModules, vnToken) { constructor($element, $, vnModules) {
super($element, $); super($element, $);
this.modules = vnModules.get(); this.modules = vnModules.get();
} }
@ -36,6 +36,7 @@ export class Layout extends Component {
this.$http.get('AccessTokenConfigs').then(json => { this.$http.get('AccessTokenConfigs').then(json => {
const firtsResult = json.data[0]; const firtsResult = json.data[0];
if (!firtsResult) return; if (!firtsResult) return;
this.renewPeriod = firtsResult.renewPeriod;
this.renewInterval = firtsResult.renewInterval; this.renewInterval = firtsResult.renewInterval;
const intervalMilliseconds = firtsResult.renewInterval * 1000; const intervalMilliseconds = firtsResult.renewInterval * 1000;
@ -44,29 +45,31 @@ export class Layout extends Component {
} }
checkTokenValidity() { checkTokenValidity() {
console.log(this.vnToken.renewPeriod); this.vnToken.renewPeriod = this.vnToken.renewPeriod < this.renewPeriod
const fecha = new Date(this.vnToken.created); // Fecha representada por un objeto Date ? this.vnToken.renewPeriod
const duracionEnSegundos = this.vnToken.renewPeriod; // Duración en segundos (número) : this.renewPeriod;
const fechaEnMilisegundos = fecha.getTime(); // Convertir fecha a milisegundos
const duracionEnMilisegundos = duracionEnSegundos * 1000; // Convertir duración a milisegundos const secondsToRenew = this.vnToken.renewPeriod;
const nuevaFechaEnMilisegundos = fechaEnMilisegundos + duracionEnMilisegundos; // Sumar los milisegundos const milisecondsToRenew = secondsToRenew * 1000;
const nuevaFecha = new Date(nuevaFechaEnMilisegundos); // Crear una nueva fecha a partir de los milisegundos const created = new Date(this.vnToken.created);
const createdInMiliseconds = created.getTime();
const maxDateInMiliseconds = createdInMiliseconds + milisecondsToRenew;
const maxDate = new Date(maxDateInMiliseconds);
const now = new Date(); const now = new Date();
console.log(now > nuevaFecha, now, nuevaFecha); if (now > maxDate) {
if (now > nuevaFecha) {
this.$http.post('VnUsers/renewToken') this.$http.post('VnUsers/renewToken')
.then(json => { .then(json => {
if (json.data.token) { if (json.data.token) {
let remember = true; let remember = true;
if (window.sessionStorage.vnToken) remember = false; 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 => { .catch(err => {
if (res.status !== 405) if (err.status !== 400)
throw res; throw err;
}); });
} }
} }