2023-05-24 13:01:59 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('renewToken', {
|
|
|
|
description: 'Send email to the user',
|
|
|
|
accepts: [],
|
2023-05-25 07:51:56 +00:00
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
2023-05-24 13:01:59 +00:00
|
|
|
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;
|
2023-05-25 07:51:56 +00:00
|
|
|
const tokenId = ctx.req.accessToken.id;
|
2023-05-24 13:01:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
const accessToken = await models.AccessToken.create({userId: userId});
|
2023-05-25 07:51:56 +00:00
|
|
|
await models.AccessToken.destroyById(tokenId);
|
2023-05-24 13:01:59 +00:00
|
|
|
|
|
|
|
return {token: accessToken.id, created: accessToken.created};
|
|
|
|
};
|
|
|
|
};
|