salix/back/methods/vn-user/renew-token.js

42 lines
1.2 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('renewToken', {
description: 'Send email to the user',
accepts: [],
returns: {
type: 'Object',
root: true
},
http: {
path: `/renewToken`,
verb: 'POST'
}
});
Self.renewToken = async function(ctx) {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const created = ctx.req.accessToken.created;
const tokenId = ctx.req.accessToken.id;
const now = new Date();
const differenceMilliseconds = now - created;
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
const accessTokenConfig = await models.AccessTokenConfig.findOne();
if (differenceSeconds <= accessTokenConfig.renewPeriod) {
const response = {
statusCode: 200,
data: {
message: 'Token is active',
}
};
return response;
}
const accessToken = await models.AccessToken.create({userId: userId});
await models.AccessToken.destroyById(tokenId);
return {token: accessToken.id, created: accessToken.created};
};
};