feat: authController i18n
This commit is contained in:
1 parent
301208d799
commit
a98a3dd7a3
2 files changed
+15
-10
No files matched your search
@@ -8,20 +8,21 @@ import {
|
|||||||
newPasswordValidation,
|
newPasswordValidation,
|
||||||
} from "../validation/joi.js";
|
} from "../validation/joi.js";
|
||||||
import logger from "../utils/logger.js";
|
import logger from "../utils/logger.js";
|
||||||
import { errorMessages, successMessages } from "../utils/messages.js";
|
import { successMessages } from "../utils/messages.js";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { getTokenFromHeaders, tokenType } from "../utils/utils.js";
|
import { getTokenFromHeaders, tokenType } from "../utils/utils.js";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
import { handleValidationError, handleError } from "./controllerUtils.js";
|
import { handleValidationError, handleError } from "./controllerUtils.js";
|
||||||
const SERVICE_NAME = "authController";
|
const SERVICE_NAME = "authController";
|
||||||
|
import ServiceRegistry from "../service/serviceRegistry.js";
|
||||||
|
import StringService from "../service/stringService.js";
|
||||||
|
|
||||||
class AuthController {
|
class AuthController {
|
||||||
constructor(db, settingsService, emailService, jobQueue, stringService) {
|
constructor(db, settingsService, emailService, jobQueue) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
this.jobQueue = jobQueue;
|
this.jobQueue = jobQueue;
|
||||||
this.stringService = stringService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,6 +139,7 @@ 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.
|
* @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) => {
|
loginUser = async (req, res, next) => {
|
||||||
|
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
||||||
try {
|
try {
|
||||||
await loginValidation.validateAsync(req.body);
|
await loginValidation.validateAsync(req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -154,7 +156,7 @@ class AuthController {
|
|||||||
// Compare password
|
// Compare password
|
||||||
const match = await user.comparePassword(password);
|
const match = await user.comparePassword(password);
|
||||||
if (match !== true) {
|
if (match !== true) {
|
||||||
const error = new Error(this.stringService.authIncorrectPassword);
|
const error = new Error(stringService.authIncorrectPassword);
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
@@ -201,13 +203,15 @@ class AuthController {
|
|||||||
* @throws {Error} If there is an error during the process such as any of the token is not received
|
* @throws {Error} If there is an error during the process such as any of the token is not received
|
||||||
*/
|
*/
|
||||||
refreshAuthToken = async (req, res, next) => {
|
refreshAuthToken = async (req, res, next) => {
|
||||||
|
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// check for refreshToken
|
// check for refreshToken
|
||||||
const refreshToken = req.headers["x-refresh-token"];
|
const refreshToken = req.headers["x-refresh-token"];
|
||||||
|
|
||||||
if (!refreshToken) {
|
if (!refreshToken) {
|
||||||
// No refresh token provided
|
// No refresh token provided
|
||||||
const error = new Error(this.stringService.noRefreshToken);
|
const error = new Error(stringService.noRefreshToken);
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "refreshAuthToken";
|
error.method = "refreshAuthToken";
|
||||||
@@ -222,8 +226,8 @@ class AuthController {
|
|||||||
// Invalid or expired refresh token, trigger logout
|
// Invalid or expired refresh token, trigger logout
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
refreshErr.name === "TokenExpiredError"
|
refreshErr.name === "TokenExpiredError"
|
||||||
? this.stringService.expiredAuthToken
|
? stringService.expiredAuthToken
|
||||||
: this.stringService.invalidAuthToken;
|
: stringService.invalidAuthToken;
|
||||||
const error = new Error(errorMessage);
|
const error = new Error(errorMessage);
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
@@ -266,6 +270,8 @@ 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).
|
* @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) => {
|
editUser = async (req, res, next) => {
|
||||||
|
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await editUserParamValidation.validateAsync(req.params);
|
await editUserParamValidation.validateAsync(req.params);
|
||||||
await editUserBodyValidation.validateAsync(req.body);
|
await editUserBodyValidation.validateAsync(req.body);
|
||||||
@@ -277,7 +283,7 @@ class AuthController {
|
|||||||
|
|
||||||
// TODO is this neccessary any longer? Verify ownership middleware should handle this
|
// TODO is this neccessary any longer? Verify ownership middleware should handle this
|
||||||
if (req.params.userId !== req.user._id.toString()) {
|
if (req.params.userId !== req.user._id.toString()) {
|
||||||
const error = new Error(this.stringService.unauthorized);
|
const error = new Error(stringService.unauthorized);
|
||||||
error.status = 401;
|
error.status = 401;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
next(error);
|
next(error);
|
||||||
@@ -301,7 +307,7 @@ class AuthController {
|
|||||||
// If not a match, throw a 403
|
// If not a match, throw a 403
|
||||||
// 403 instead of 401 to avoid triggering axios interceptor
|
// 403 instead of 401 to avoid triggering axios interceptor
|
||||||
if (!match) {
|
if (!match) {
|
||||||
const error = new Error(this.stringService.authIncorrectPassword);
|
const error = new Error(stringService.authIncorrectPassword);
|
||||||
error.status = 403;
|
error.status = 403;
|
||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -220,7 +220,6 @@ const startApp = async () => {
|
|||||||
ServiceRegistry.get(SettingsService.SERVICE_NAME),
|
ServiceRegistry.get(SettingsService.SERVICE_NAME),
|
||||||
ServiceRegistry.get(EmailService.SERVICE_NAME),
|
ServiceRegistry.get(EmailService.SERVICE_NAME),
|
||||||
ServiceRegistry.get(JobQueue.SERVICE_NAME),
|
ServiceRegistry.get(JobQueue.SERVICE_NAME),
|
||||||
ServiceRegistry.get(StringService.SERVICE_NAME)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const monitorController = new MonitorController(
|
const monitorController = new MonitorController(
|
||||||
|
|||||||
Reference in new issue
Block a user