Generate refresh token & add it to response
This commit is contained in:
1 parent
c2e00375c1
commit
05c051ae3a
2 files changed
+24
-2
No files matched your search
@@ -33,6 +33,24 @@ const issueToken = (payload, appSettings) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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({ id: 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.
|
* Registers a new user. If the user is the first account, a JWT secret is created. If not, an invite token is required.
|
||||||
* @async
|
* @async
|
||||||
@@ -81,6 +99,7 @@ const registerUser = async (req, res, next) => {
|
|||||||
const appSettings = await req.settingsService.getSettings();
|
const appSettings = await req.settingsService.getSettings();
|
||||||
|
|
||||||
const token = issueToken(userForToken, appSettings);
|
const token = issueToken(userForToken, appSettings);
|
||||||
|
const refreshToken = issueRefreshToken(userForToken._id, appSettings);
|
||||||
|
|
||||||
req.emailService
|
req.emailService
|
||||||
.buildAndSendEmail(
|
.buildAndSendEmail(
|
||||||
@@ -99,7 +118,7 @@ const registerUser = async (req, res, next) => {
|
|||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_CREATE_USER,
|
msg: successMessages.AUTH_CREATE_USER,
|
||||||
data: { user: newUser, token: token },
|
data: { user: newUser, token: token, refreshToken: refreshToken },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("ERROR", error);
|
console.log("ERROR", error);
|
||||||
@@ -147,13 +166,14 @@ const loginUser = async (req, res, next) => {
|
|||||||
// Happy path, return token
|
// Happy path, return token
|
||||||
const appSettings = await req.settingsService.getSettings();
|
const appSettings = await req.settingsService.getSettings();
|
||||||
const token = issueToken(userWithoutPassword, appSettings);
|
const token = issueToken(userWithoutPassword, appSettings);
|
||||||
|
const refreshToken = issueRefreshToken(userWithoutPassword._id, appSettings);
|
||||||
// reset avatar image
|
// reset avatar image
|
||||||
userWithoutPassword.avatarImage = user.avatarImage;
|
userWithoutPassword.avatarImage = user.avatarImage;
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_LOGIN_USER,
|
msg: successMessages.AUTH_LOGIN_USER,
|
||||||
data: { user: userWithoutPassword, token: token },
|
data: { user: userWithoutPassword, token: token, refreshToken: refreshToken },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "loginController"));
|
next(handleError(error, SERVICE_NAME, "loginController"));
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ const envConfig = {
|
|||||||
apiBaseUrl: undefined,
|
apiBaseUrl: undefined,
|
||||||
clientHost: process.env.CLIENT_HOST,
|
clientHost: process.env.CLIENT_HOST,
|
||||||
jwtSecret: process.env.JWT_SECRET,
|
jwtSecret: process.env.JWT_SECRET,
|
||||||
|
refreshTokenSecret: process.env.REFRESH_TOKEN_SECRET,
|
||||||
dbType: process.env.DB_TYPE,
|
dbType: process.env.DB_TYPE,
|
||||||
dbConnectionString: process.env.DB_CONNECTION_STRING,
|
dbConnectionString: process.env.DB_CONNECTION_STRING,
|
||||||
redisHost: process.env.REDIS_HOST,
|
redisHost: process.env.REDIS_HOST,
|
||||||
redisPort: process.env.REDIS_PORT,
|
redisPort: process.env.REDIS_PORT,
|
||||||
jwtTTL: process.env.TOKEN_TTL,
|
jwtTTL: process.env.TOKEN_TTL,
|
||||||
|
refreshTokenTTL: process.env.REFRESH_TOKEN_TTL,
|
||||||
pagespeedApiKey: process.env.PAGESPEED_API_KEY,
|
pagespeedApiKey: process.env.PAGESPEED_API_KEY,
|
||||||
systemEmailHost: process.env.SYSTEM_EMAIL_HOST,
|
systemEmailHost: process.env.SYSTEM_EMAIL_HOST,
|
||||||
systemEmailPort: process.env.SYSTEM_EMAIL_PORT,
|
systemEmailPort: process.env.SYSTEM_EMAIL_PORT,
|
||||||
|
|||||||
Reference in new issue
Block a user