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

43 lines
1.3 KiB
JavaScript
Raw Normal View History

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;
}
await Self.logout(tokenId);
const user = await Self.findById(userId);
const accessToken = await user.createAccessToken();
return {token: accessToken.id, created: accessToken.created};
};
};