Add app settings model, routes, db ops, service, use settings for issueToken

This commit is contained in:
Alex Holliday committed 2024-09-26 13:41:50 +08:00
1 parent b2bc441c7f
commit 992bf099fa
12 files changed
+210 -19

No files matched your search

+16 -8
View File
@@ -25,10 +25,15 @@ const { getTokenFromHeaders } = require("../utils/utils");
* @param {Object} payload
* @returns {String}
*/
const issueToken = (payload) => {
//TODO Add proper expiration date
const tokenTTL = process.env.TOKEN_TTL ? process.env.TOKEN_TTL : "2h";
return jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: tokenTTL });
const issueToken = async (payload, appSettings) => {
try {
const tokenTTL = appSettings.jwtTTL ? appSettings.jwtTTL : "2h";
return jwt.sign(payload, appSettings.jwtSecret, { expiresIn: tokenTTL });
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "issueToken") : null;
next(error);
}
};
/**
@@ -67,7 +72,8 @@ const registerController = async (req, res, next) => {
delete userForToken.profileImage;
delete userForToken.avatarImage;
const token = issueToken(userForToken);
const appSettings = await req.settingsService.getSettings();
const token = issueToken(userForToken, appSettings);
req.emailService
.buildAndSendEmail(
@@ -130,8 +136,8 @@ const loginController = async (req, res, next) => {
delete userWithoutPassword.avatarImage;
// Happy path, return token
const token = issueToken(userWithoutPassword);
const appSettings = req.settingsService.getSettings();
const token = await issueToken(userWithoutPassword, appSettings);
// reset avatar image
userWithoutPassword.avatarImage = user.avatarImage;
@@ -425,7 +431,9 @@ const resetPasswordController = async (req, res, next) => {
}
try {
const user = await req.db.resetPassword(req, res);
const token = issueToken(user._doc);
const appSettings = await req.settingsService.getSettings();
const token = issueToken(user._doc, appSettings);
res.status(200).json({
success: true,
msg: successMessages.AUTH_RESET_PASSWORD,
+4 -4
View File
@@ -34,7 +34,7 @@ const createCheck = async (req, res, next) => {
.status(200)
.json({ success: true, msg: successMessages.CHECK_CREATE, data: check });
} catch (error) {
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "createCheck") : null;
next(error);
}
@@ -62,7 +62,7 @@ const getChecks = async (req, res, next) => {
data: { checksCount, checks },
});
} catch (error) {
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "getChecks") : null;
next(error);
}
@@ -88,7 +88,7 @@ const getTeamChecks = async (req, res, next) => {
data: checkData,
});
} catch (error) {
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "getTeamChecks") : null;
next(error);
}
@@ -114,7 +114,7 @@ const deleteChecks = async (req, res, next) => {
data: { deletedCount },
});
} catch (error) {
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "deleteChecks") : null;
next(error);
}
+20
View File
@@ -0,0 +1,20 @@
const { successMessages } = require("../utils/messages");
const SERVICE_NAME = "SettingsController";
const getAppSettings = async (req, res, next) => {
try {
const settings = await req.db.getAppSettings();
return res.status(200).json({
success: true,
msg: successMessages.GET_APP_SETTINGS,
data: settings,
});
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "getAppSettings") : null;
}
};
module.exports = {
getAppSettings,
};