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']});
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};
};
};

View File

@ -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};
};
};

View File

@ -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;

View File

@ -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;
});
}
}