2023-05-24 13:01:59 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('renewToken', {
|
|
|
|
description: 'Send email to the user',
|
|
|
|
accepts: [],
|
|
|
|
http: {
|
|
|
|
path: `/renewToken`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-05-24 13:16:04 +00:00
|
|
|
Self.renewToken = async function(ctx) {
|
2023-05-24 13:01:59 +00:00
|
|
|
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;
|
2023-05-24 13:16:04 +00:00
|
|
|
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
|
2023-05-24 13:01:59 +00:00
|
|
|
|
|
|
|
const accessTokenConfig = await models.AccessTokenConfig.findOne();
|
|
|
|
if (differenceSeconds <= accessTokenConfig.renewPeriod) {
|
|
|
|
const response = {
|
|
|
|
statusCode: 200,
|
|
|
|
data: {
|
|
|
|
message: 'Token is active',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2023-05-24 13:16:04 +00:00
|
|
|
await models.AccessToken.destroyAll({userId: userId});
|
|
|
|
// await models.AccessToken.destroyById(tokenId);
|
|
|
|
|
|
|
|
const accessToken = await models.AccessToken.create({userId: userId});
|
2023-05-24 13:01:59 +00:00
|
|
|
|
|
|
|
return {token: accessToken.id, created: accessToken.created};
|
|
|
|
};
|
|
|
|
};
|