Compare commits
4 Commits
b02e1f000e
...
5601ce5dac
Author | SHA1 | Date |
---|---|---|
Javier Segarra | 5601ce5dac | |
Javier Segarra | 43366d1ba8 | |
Javier Segarra | 21028e3b79 | |
Javier Segarra | 67faf076d2 |
|
@ -1,5 +1,8 @@
|
||||||
|
const tokenConfig = require('./token-config');
|
||||||
|
|
||||||
|
module.exports = async token => {
|
||||||
|
const accessTokenConfig = await tokenConfig();
|
||||||
|
|
||||||
module.exports = async(token, accessTokenConfig) => {
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const differenceMilliseconds = now - token.created;
|
const differenceMilliseconds = now - token.created;
|
||||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
const handlePromiseLogout = (Self, {id}, courtesyTime = 60) => {
|
const tokenConfig = require('./token-config');
|
||||||
|
const DEFAULT_COURTESY_TIME = 60;
|
||||||
|
const handlePromiseLogout = (Self, {id}, courtesyTime = DEFAULT_COURTESY_TIME) => {
|
||||||
new Promise(res => {
|
new Promise(res => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
res(Self.logout(id));
|
res(Self.logout(id));
|
||||||
|
@ -23,14 +25,13 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.renewToken = async function(ctx) {
|
Self.renewToken = async function(ctx) {
|
||||||
const models = Self.app.models;
|
const {accessToken: token} = ctx.req;
|
||||||
const token = ctx.req.accessToken;
|
|
||||||
|
|
||||||
// Check if current token is valid
|
// Check if current token is valid
|
||||||
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 fields = ['courtesyTime'];
|
|
||||||
const {courtesyTime} = await models.AccessTokenConfig.findOne({fields});
|
const {courtesyTime} = await tokenConfig();
|
||||||
|
|
||||||
// Schedule to remove current token
|
// Schedule to remove current token
|
||||||
handlePromiseLogout(Self, token, courtesyTime);
|
handlePromiseLogout(Self, token, courtesyTime);
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
const DEFAULT_FIELDS = ['renewPeriod', 'courtesyTime'];
|
||||||
|
const {models} = require('vn-loopback/server/server');
|
||||||
|
let currentAccessTokenConfig = null;
|
||||||
|
module.exports = async(fields = DEFAULT_FIELDS) => {
|
||||||
|
if (currentAccessTokenConfig) return currentAccessTokenConfig;
|
||||||
|
const accessTokenConfig = await models.AccessTokenConfig.findOne({fields});
|
||||||
|
if (!accessTokenConfig) currentAccessTokenConfig = accessTokenConfig;
|
||||||
|
return accessTokenConfig;
|
||||||
|
};
|
|
@ -14,9 +14,7 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.validateToken = async function(token) {
|
Self.validateToken = async function(token) {
|
||||||
const fields = ['renewPeriod', 'courtesyTime'];
|
const isValid = await isTokenValid(token);
|
||||||
const accessTokenConfig = await Self.app.models.AccessTokenConfig.findOne({fields});
|
|
||||||
const isValid = await isTokenValid(token, accessTokenConfig);
|
|
||||||
return isValid;
|
return isValid;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
-- Auto-generated SQL script #202311061003
|
||||||
|
UPDATE salix.accessTokenConfig
|
||||||
|
SET courtesyTime=60
|
||||||
|
WHERE id=1;
|
|
@ -2945,9 +2945,9 @@ INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`)
|
||||||
(2, 1, 50, 2),
|
(2, 1, 50, 2),
|
||||||
(3, 1, 0, 3);
|
(3, 1, 0, 3);
|
||||||
|
|
||||||
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`)
|
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `courtesyTime`, `renewInterval`)
|
||||||
VALUES
|
VALUES
|
||||||
(1, 21600, 300);
|
(1, 21600, 60, 300);
|
||||||
|
|
||||||
INSERT INTO `vn`.`travelConfig` (`id`, `warehouseInFk`, `warehouseOutFk`, `agencyFk`, `companyFk`)
|
INSERT INTO `vn`.`travelConfig` (`id`, `warehouseInFk`, `warehouseOutFk`, `agencyFk`, `companyFk`)
|
||||||
VALUES
|
VALUES
|
||||||
|
|
|
@ -82,7 +82,7 @@ export default class Token {
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
this.renewPeriod = data.renewPeriod;
|
this.renewPeriod = data.renewPeriod;
|
||||||
this.stopRenewer();
|
this.stopRenewer();
|
||||||
this.inservalId = setInterval(() => this.checkValidity(), data.renewInterval * 1000);
|
this.intervalId = setInterval(() => this.checkValidity(), data.renewInterval * 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ export default class Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
stopRenewer() {
|
stopRenewer() {
|
||||||
clearInterval(this.inservalId);
|
clearInterval(this.intervalId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Token.$inject = ['vnInterceptor', '$http', '$rootScope'];
|
Token.$inject = ['vnInterceptor', '$http', '$rootScope'];
|
||||||
|
|
Loading…
Reference in New Issue