Refactor issueToken function for issueing the refresh token

This commit is contained in:
Rushi Gandhi
2024-10-16 12:45:31 +05:30
parent cd0160f421
commit 29ce8bb116
2 changed files with 27 additions and 27 deletions

View File

@@ -12,7 +12,7 @@ require("dotenv").config();
const { errorMessages, successMessages } = require("../utils/messages");
const jwt = require("jsonwebtoken");
const SERVICE_NAME = "AuthController";
const { getTokenFromHeaders } = require("../utils/utils");
const { getTokenFromHeaders, tokenType } = require("../utils/utils");
const crypto = require("crypto");
const { handleValidationError, handleError } = require("./controllerUtils");
@@ -20,37 +20,31 @@ const { handleValidationError, handleError } = require("./controllerUtils");
* Creates and returns JWT token with an arbitrary payload
* @function
* @param {Object} payload
* @param {tokenType} typeOfToken - Whether to generate refresh token with long TTL or access token with short TTL.
* @param {Object} appSettings
* @returns {String}
* @throws {Error}
*/
const issueToken = (payload, appSettings) => {
const issueToken = (payload, typeOfToken, appSettings) => {
try {
const tokenTTL = appSettings.jwtTTL ? appSettings.jwtTTL : "2h";
return jwt.sign(payload, appSettings.jwtSecret, { expiresIn: tokenTTL });
let tokenSecret, tokenTTL, payloadData = {};
if(typeOfToken === tokenType.REFRESH_TOKEN) {
tokenSecret = appSettings.refreshTokenSecret;
tokenTTL = appSettings.refreshTokenTTL ? appSettings.refreshTokenTTL : "7d";
}
else {
tokenSecret = appSettings.jwtSecret;
tokenTTL = appSettings.jwtTTL ? appSettings.jwtTTL : "2h";
payloadData = payload;
}
return jwt.sign(payloadData, tokenSecret, { expiresIn: tokenTTL });
} catch (error) {
throw handleError(error, SERVICE_NAME, "issueToken");
}
};
/**
* Creates and returns a JWT refresh token with an arbitrary payload
* @function
* @param {Object} payload - Here we have passed MongoDB assigned _id.
* @param {Object} appSettings
* @returns {String}
* @throws {Error}
*/
const issueRefreshToken = (payload, appSettings) => {
try {
const tokenTTL = appSettings.refreshTokenTTL ? appSettings.refreshTokenTTL : "7d";
return jwt.sign(payload, appSettings.refreshTokenSecret, { expiresIn: tokenTTL });
} catch (error) {
throw handleError(error, SERVICE_NAME, "issueRefreshToken");
}
};
/**
* Registers a new user. If the user is the first account, a JWT secret is created. If not, an invite token is required.
* @async
@@ -98,8 +92,8 @@ const registerUser = async (req, res, next) => {
const appSettings = await req.settingsService.getSettings();
const token = issueToken(userForToken, appSettings);
const refreshToken = issueRefreshToken(userForToken, appSettings);
const token = issueToken(userForToken, tokenType.ACCESS_TOKEN, appSettings);
const refreshToken = issueToken({}, tokenType.REFRESH_TOKEN, appSettings);
req.emailService
.buildAndSendEmail(
@@ -165,8 +159,8 @@ const loginUser = async (req, res, next) => {
// Happy path, return token
const appSettings = await req.settingsService.getSettings();
const token = issueToken(userWithoutPassword, appSettings);
const refreshToken = issueRefreshToken(userWithoutPassword, appSettings);
const token = issueToken(userWithoutPassword, tokenType.ACCESS_TOKEN, appSettings);
const refreshToken = issueToken({}, tokenType.REFRESH_TOKEN, appSettings);
// reset avatar image
userWithoutPassword.avatarImage = user.avatarImage;
@@ -380,7 +374,7 @@ const resetPassword = async (req, res, next) => {
const user = await req.db.resetPassword(req, res);
const appSettings = await req.settingsService.getSettings();
const token = issueToken(user._doc, appSettings);
const token = issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);
res.status(200).json({
success: true,
msg: successMessages.AUTH_RESET_PASSWORD,

View File

@@ -27,7 +27,13 @@ const getTokenFromHeaders = (headers) => {
return parts[1];
};
const tokenType = Object.freeze({
ACCESS_TOKEN: "Access token",
REFRESH_TOKEN: "Refresh token"
});
module.exports = {
ParseBoolean,
getTokenFromHeaders,
tokenType,
};