refs #6264 perf: use functions extracted previously

This commit is contained in:
Javier Segarra 2023-12-04 14:46:05 +01:00
parent b9671c0b67
commit 5656ed7a2b
2 changed files with 10 additions and 5 deletions

View File

@ -1,5 +1,6 @@
const UserError = require('vn-loopback/util/user-error');
const tokenConfig = require('./token-config');
const {models} = require('vn-loopback/server/server');
const DEFAULT_COURTESY_TIME = 60;
const handlePromiseLogout = (Self, {id}, courtesyTime = DEFAULT_COURTESY_TIME) => {
new Promise(res => {
@ -31,7 +32,7 @@ module.exports = Self => {
const isValid = await Self.validateToken(token);
if (isValid) throw new UserError(`The renew period has not been exceeded`, 'periodNotExceeded');
const {courtesyTime} = await tokenConfig();
const {courtesyTime} = await models.AccessTokenConfig.findOne({fields: ['renewPeriod', 'courtesyTime']});
// Schedule to remove current token
handlePromiseLogout(Self, token, courtesyTime);

View File

@ -1,5 +1,4 @@
const isTokenValid = require('./is-token-valid');
const {models} = require('vn-loopback/server/server');
module.exports = Self => {
Self.remoteMethod('validateToken', {
description: 'Validates the current logged user token',
@ -14,7 +13,12 @@ module.exports = Self => {
});
Self.validateToken = async function(token) {
const isValid = await isTokenValid(token);
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod', 'courtesyTime']});
const now = Date.now();
const differenceMilliseconds = now - token.created;
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
const isValid = differenceSeconds < accessTokenConfig.renewPeriod - accessTokenConfig.courtesyTime;
return isValid;
};
};