2023-12-04 13:46:05 +00:00
|
|
|
const {models} = require('vn-loopback/server/server');
|
2019-01-23 12:11:44 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('validateToken', {
|
2019-01-25 11:04:35 +00:00
|
|
|
description: 'Validates the current logged user token',
|
2019-01-23 12:11:44 +00:00
|
|
|
returns: {
|
|
|
|
type: 'Boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/validateToken`,
|
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-04 13:39:26 +00:00
|
|
|
Self.validateToken = async function(token) {
|
2023-12-04 13:46:05 +00:00
|
|
|
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;
|
|
|
|
|
2023-11-04 13:39:26 +00:00
|
|
|
return isValid;
|
2019-01-23 12:11:44 +00:00
|
|
|
};
|
|
|
|
};
|