Skip to content

Commit

Permalink
Fixed: Multiple tokens creation for already logged in users
Browse files Browse the repository at this point in the history
Issue hagopj13#274 resolved
  • Loading branch information
mdmuhtasimfuadfahim committed Oct 25, 2023
1 parent 179ae84 commit 869a47e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/services/token.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,26 @@ const verifyToken = async (token, type) => {
* @returns {Promise<Object>}
*/
const generateAuthTokens = async (user) => {
let refreshTokenDoc = await Token.findOne({ user: user._id });
const accessTokenExpires = moment().add(config.jwt.accessExpirationMinutes, 'minutes');
const accessToken = generateToken(user.id, accessTokenExpires, tokenTypes.ACCESS);

const refreshTokenExpires = moment().add(config.jwt.refreshExpirationDays, 'days');
const refreshToken = generateToken(user.id, refreshTokenExpires, tokenTypes.REFRESH);
await saveToken(refreshToken, user.id, refreshTokenExpires, tokenTypes.REFRESH);
if (!refreshTokenDoc) {
const refreshToken = generateToken(user.id, refreshTokenExpires, tokenTypes.REFRESH);
await saveToken(refreshToken, user.id, refreshTokenExpires, tokenTypes.REFRESH);

refreshTokenDoc = {};
refreshTokenDoc.token = refreshToken;
}

return {
access: {
token: accessToken,
expires: accessTokenExpires.toDate(),
},
refresh: {
token: refreshToken,
token: refreshTokenDoc.token,
expires: refreshTokenExpires.toDate(),
},
};
Expand Down

0 comments on commit 869a47e

Please sign in to comment.