fix: use new structure

This commit is contained in:
cihatata authored and cihat committed 2025-02-11 07:43:38 +03:00
1 parent a98a3dd7a3
commit 88df88891c
22 files changed
+193 -146

No files matched your search

+24 -27
View File
@@ -8,7 +8,6 @@ import {
newPasswordValidation,
} from "../validation/joi.js";
import logger from "../utils/logger.js";
import { successMessages } from "../utils/messages.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders, tokenType } from "../utils/utils.js";
import crypto from "crypto";
@@ -23,6 +22,7 @@ class AuthController {
this.settingsService = settingsService;
this.emailService = emailService;
this.jobQueue = jobQueue;
this.stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
}
/**
@@ -78,7 +78,7 @@ class AuthController {
// If superAdmin exists, a token should be attached to all further register requests
const superAdminExists = await this.db.checkSuperadmin(req, res);
if (superAdminExists) {
await this.db.getInviteTokenAndDelete(inviteToken, req.language);
await this.db.getInviteTokenAndDelete(inviteToken);
} else {
// This is the first account, create JWT secret to use if one is not supplied by env
const jwtSecret = crypto.randomBytes(64).toString("hex");
@@ -87,7 +87,7 @@ class AuthController {
const newUser = await this.db.insertUser({ ...req.body }, req.file);
logger.info({
message: successMessages.AUTH_CREATE_USER(req.language),
message: this.stringService.authCreateUser,
service: SERVICE_NAME,
details: newUser._id,
});
@@ -118,7 +118,7 @@ class AuthController {
});
res.success({
msg: successMessages.AUTH_CREATE_USER(req.language),
msg: this.stringService.authCreateUser,
data: { user: newUser, token: token, refreshToken: refreshToken },
});
} catch (error) {
@@ -139,7 +139,6 @@ class AuthController {
* @throws {Error} If there is an error during the process, especially if there is a validation error (422) or the password is incorrect.
*/
loginUser = async (req, res, next) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
try {
await loginValidation.validateAsync(req.body);
} catch (error) {
@@ -151,12 +150,12 @@ class AuthController {
const { email, password } = req.body;
// Check if user exists
const user = await this.db.getUserByEmail(email, req.language);
const user = await this.db.getUserByEmail(email);
// Compare password
const match = await user.comparePassword(password);
if (match !== true) {
const error = new Error(stringService.authIncorrectPassword);
const error = new Error(this.stringService.authIncorrectPassword);
error.status = 401;
next(error);
return;
@@ -179,7 +178,7 @@ class AuthController {
userWithoutPassword.avatarImage = user.avatarImage;
return res.success({
msg: successMessages.AUTH_LOGIN_USER(req.language),
msg: this.stringService.authLoginUser,
data: {
user: userWithoutPassword,
token: token,
@@ -203,7 +202,6 @@ class AuthController {
* @throws {Error} If there is an error during the process such as any of the token is not received
*/
refreshAuthToken = async (req, res, next) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
try {
// check for refreshToken
@@ -211,7 +209,7 @@ class AuthController {
if (!refreshToken) {
// No refresh token provided
const error = new Error(stringService.noRefreshToken);
const error = new Error(this.stringService.noRefreshToken);
error.status = 401;
error.service = SERVICE_NAME;
error.method = "refreshAuthToken";
@@ -226,8 +224,8 @@ class AuthController {
// Invalid or expired refresh token, trigger logout
const errorMessage =
refreshErr.name === "TokenExpiredError"
? stringService.expiredAuthToken
: stringService.invalidAuthToken;
? this.stringService.expiredAuthToken
: this.stringService.invalidAuthToken;
const error = new Error(errorMessage);
error.status = 401;
error.service = SERVICE_NAME;
@@ -248,7 +246,7 @@ class AuthController {
);
return res.success({
msg: successMessages.AUTH_TOKEN_REFRESHED(req.language),
msg: this.stringService.authTokenRefreshed,
data: { user: payloadData, token: newAuthToken, refreshToken: refreshToken },
});
} catch (error) {
@@ -270,7 +268,6 @@ class AuthController {
* @throws {Error} If there is an error during the process, especially if there is a validation error (422), the user is unauthorized (401), or the password is incorrect (403).
*/
editUser = async (req, res, next) => {
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
try {
await editUserParamValidation.validateAsync(req.params);
@@ -283,7 +280,7 @@ class AuthController {
// TODO is this neccessary any longer? Verify ownership middleware should handle this
if (req.params.userId !== req.user._id.toString()) {
const error = new Error(stringService.unauthorized);
const error = new Error(this.stringService.unauthorized);
error.status = 401;
error.service = SERVICE_NAME;
next(error);
@@ -301,13 +298,13 @@ class AuthController {
// Add user email to body for DB operation
req.body.email = email;
// Get user
const user = await this.db.getUserByEmail(email, req.language);
const user = await this.db.getUserByEmail(email);
// Compare passwords
const match = await user.comparePassword(req.body.password);
// If not a match, throw a 403
// 403 instead of 401 to avoid triggering axios interceptor
if (!match) {
const error = new Error(stringService.authIncorrectPassword);
const error = new Error(this.stringService.authIncorrectPassword);
error.status = 403;
next(error);
return;
@@ -318,7 +315,7 @@ class AuthController {
const updatedUser = await this.db.updateUser(req, res);
res.success({
msg: successMessages.AUTH_UPDATE_USER(req.language),
msg: this.stringService.authUpdateUser,
data: updatedUser,
});
} catch (error) {
@@ -340,7 +337,7 @@ class AuthController {
const superAdminExists = await this.db.checkSuperadmin(req, res);
return res.success({
msg: successMessages.AUTH_ADMIN_EXISTS(req.language),
msg: this.stringService.authAdminExists,
data: superAdminExists,
});
} catch (error) {
@@ -369,7 +366,7 @@ class AuthController {
try {
const { email } = req.body;
const user = await this.db.getUserByEmail(email, req.language);
const user = await this.db.getUserByEmail(email);
const recoveryToken = await this.db.requestRecoveryToken(req, res);
const name = user.firstName;
const { clientHost } = this.settingsService.getSettings();
@@ -386,7 +383,7 @@ class AuthController {
);
return res.success({
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN(req.language),
msg: this.stringService.authCreateRecoveryToken,
data: msgId,
});
} catch (error) {
@@ -417,7 +414,7 @@ class AuthController {
await this.db.validateRecoveryToken(req, res);
return res.success({
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN(req.language),
msg: this.stringService.authVerifyRecoveryToken,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "validateRecoveryTokenController"));
@@ -450,7 +447,7 @@ class AuthController {
const token = this.issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);
return res.success({
msg: successMessages.AUTH_RESET_PASSWORD(req.language),
msg: this.stringService.authResetPassword,
data: { user, token },
});
} catch (error) {
@@ -474,7 +471,7 @@ class AuthController {
const { email } = decodedToken;
// Check if the user exists
const user = await this.db.getUserByEmail(email, req.language);
const user = await this.db.getUserByEmail(email);
// 1. Find all the monitors associated with the team ID if superadmin
const result = await this.db.getMonitorsByTeamId({
@@ -501,10 +498,10 @@ class AuthController {
await this.db.deleteMonitorsByUserId(user._id);
}
// 6. Delete the user by id
await this.db.deleteUser(user._id, req.language);
await this.db.deleteUser(user._id);
return res.success({
msg: successMessages.AUTH_DELETE_USER(req.language),
msg: this.stringService.authDeleteUser,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteUserController"));
@@ -516,7 +513,7 @@ class AuthController {
const allUsers = await this.db.getAllUsers(req, res);
return res.success({
msg: successMessages.AUTH_GET_ALL_USERS(req.language),
msg: this.stringService.authGetAllUsers,
data: allUsers,
});
} catch (error) {