refs #6264 perf: use functions extracted previously
This commit is contained in:
parent
b9671c0b67
commit
5656ed7a2b
|
@ -1,5 +1,6 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
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 DEFAULT_COURTESY_TIME = 60;
|
||||||
const handlePromiseLogout = (Self, {id}, courtesyTime = DEFAULT_COURTESY_TIME) => {
|
const handlePromiseLogout = (Self, {id}, courtesyTime = DEFAULT_COURTESY_TIME) => {
|
||||||
new Promise(res => {
|
new Promise(res => {
|
||||||
|
@ -31,7 +32,7 @@ module.exports = Self => {
|
||||||
const isValid = await Self.validateToken(token);
|
const isValid = await Self.validateToken(token);
|
||||||
if (isValid) throw new UserError(`The renew period has not been exceeded`, 'periodNotExceeded');
|
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
|
// Schedule to remove current token
|
||||||
handlePromiseLogout(Self, token, courtesyTime);
|
handlePromiseLogout(Self, token, courtesyTime);
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
const isTokenValid = require('./is-token-valid');
|
const {models} = require('vn-loopback/server/server');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('validateToken', {
|
Self.remoteMethod('validateToken', {
|
||||||
description: 'Validates the current logged user token',
|
description: 'Validates the current logged user token',
|
||||||
|
@ -14,7 +13,12 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.validateToken = async function(token) {
|
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;
|
return isValid;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue