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

40 lines
1.2 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('renewToken', {
description: 'Send email to the user',
accepts: [],
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;
}
await models.AccessToken.destroyAll({userId: userId});
// await models.AccessToken.destroyById(tokenId);
const accessToken = await models.AccessToken.create({userId: userId});
return {token: accessToken.id, created: accessToken.created};
};
};