@@ -8,19 +8,19 @@ import {
8
8
newPasswordValidation ,
9
9
} from "../validation/joi.js" ;
10
10
import logger from "../utils/logger.js" ;
11
- import { errorMessages , successMessages } from "../utils/messages.js" ;
12
11
import jwt from "jsonwebtoken" ;
13
12
import { getTokenFromHeaders , tokenType } from "../utils/utils.js" ;
14
13
import crypto from "crypto" ;
15
14
import { handleValidationError , handleError } from "./controllerUtils.js" ;
16
15
const SERVICE_NAME = "authController" ;
17
16
18
17
class AuthController {
19
- constructor ( db , settingsService , emailService , jobQueue ) {
18
+ constructor ( db , settingsService , emailService , jobQueue , stringService ) {
20
19
this . db = db ;
21
20
this . settingsService = settingsService ;
22
21
this . emailService = emailService ;
23
22
this . jobQueue = jobQueue ;
23
+ this . stringService = stringService ;
24
24
}
25
25
26
26
/**
@@ -85,7 +85,7 @@ class AuthController {
85
85
86
86
const newUser = await this . db . insertUser ( { ...req . body } , req . file ) ;
87
87
logger . info ( {
88
- message : successMessages . AUTH_CREATE_USER ,
88
+ message : this . stringService . authCreateUser ,
89
89
service : SERVICE_NAME ,
90
90
details : newUser . _id ,
91
91
} ) ;
@@ -116,7 +116,7 @@ class AuthController {
116
116
} ) ;
117
117
118
118
res . success ( {
119
- msg : successMessages . AUTH_CREATE_USER ,
119
+ msg : this . stringService . authCreateUser ,
120
120
data : { user : newUser , token : token , refreshToken : refreshToken } ,
121
121
} ) ;
122
122
} catch ( error ) {
@@ -153,7 +153,7 @@ class AuthController {
153
153
// Compare password
154
154
const match = await user . comparePassword ( password ) ;
155
155
if ( match !== true ) {
156
- const error = new Error ( errorMessages . AUTH_INCORRECT_PASSWORD ) ;
156
+ const error = new Error ( this . stringService . authIncorrectPassword ) ;
157
157
error . status = 401 ;
158
158
next ( error ) ;
159
159
return ;
@@ -176,7 +176,7 @@ class AuthController {
176
176
userWithoutPassword . avatarImage = user . avatarImage ;
177
177
178
178
return res . success ( {
179
- msg : successMessages . AUTH_LOGIN_USER ,
179
+ msg : this . stringService . authLoginUser ,
180
180
data : {
181
181
user : userWithoutPassword ,
182
182
token : token ,
@@ -200,13 +200,14 @@ class AuthController {
200
200
* @throws {Error } If there is an error during the process such as any of the token is not received
201
201
*/
202
202
refreshAuthToken = async ( req , res , next ) => {
203
+
203
204
try {
204
205
// check for refreshToken
205
206
const refreshToken = req . headers [ "x-refresh-token" ] ;
206
207
207
208
if ( ! refreshToken ) {
208
209
// No refresh token provided
209
- const error = new Error ( errorMessages . NO_REFRESH_TOKEN ) ;
210
+ const error = new Error ( this . stringService . noRefreshToken ) ;
210
211
error . status = 401 ;
211
212
error . service = SERVICE_NAME ;
212
213
error . method = "refreshAuthToken" ;
@@ -221,8 +222,8 @@ class AuthController {
221
222
// Invalid or expired refresh token, trigger logout
222
223
const errorMessage =
223
224
refreshErr . name === "TokenExpiredError"
224
- ? errorMessages . EXPIRED_REFRESH_TOKEN
225
- : errorMessages . INVALID_REFRESH_TOKEN ;
225
+ ? this . stringService . expiredAuthToken
226
+ : this . stringService . invalidAuthToken ;
226
227
const error = new Error ( errorMessage ) ;
227
228
error . status = 401 ;
228
229
error . service = SERVICE_NAME ;
@@ -243,7 +244,7 @@ class AuthController {
243
244
) ;
244
245
245
246
return res . success ( {
246
- msg : successMessages . AUTH_TOKEN_REFRESHED ,
247
+ msg : this . stringService . authTokenRefreshed ,
247
248
data : { user : payloadData , token : newAuthToken , refreshToken : refreshToken } ,
248
249
} ) ;
249
250
} catch ( error ) {
@@ -265,6 +266,7 @@ class AuthController {
265
266
* @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).
266
267
*/
267
268
editUser = async ( req , res , next ) => {
269
+
268
270
try {
269
271
await editUserParamValidation . validateAsync ( req . params ) ;
270
272
await editUserBodyValidation . validateAsync ( req . body ) ;
@@ -276,7 +278,7 @@ class AuthController {
276
278
277
279
// TODO is this neccessary any longer? Verify ownership middleware should handle this
278
280
if ( req . params . userId !== req . user . _id . toString ( ) ) {
279
- const error = new Error ( errorMessages . AUTH_UNAUTHORIZED ) ;
281
+ const error = new Error ( this . stringService . unauthorized ) ;
280
282
error . status = 401 ;
281
283
error . service = SERVICE_NAME ;
282
284
next ( error ) ;
@@ -300,7 +302,7 @@ class AuthController {
300
302
// If not a match, throw a 403
301
303
// 403 instead of 401 to avoid triggering axios interceptor
302
304
if ( ! match ) {
303
- const error = new Error ( errorMessages . AUTH_INCORRECT_PASSWORD ) ;
305
+ const error = new Error ( this . stringService . authIncorrectPassword ) ;
304
306
error . status = 403 ;
305
307
next ( error ) ;
306
308
return ;
@@ -311,7 +313,7 @@ class AuthController {
311
313
312
314
const updatedUser = await this . db . updateUser ( req , res ) ;
313
315
res . success ( {
314
- msg : successMessages . AUTH_UPDATE_USER ,
316
+ msg : this . stringService . authUpdateUser ,
315
317
data : updatedUser ,
316
318
} ) ;
317
319
} catch ( error ) {
@@ -333,7 +335,7 @@ class AuthController {
333
335
const superAdminExists = await this . db . checkSuperadmin ( req , res ) ;
334
336
335
337
return res . success ( {
336
- msg : successMessages . AUTH_ADMIN_EXISTS ,
338
+ msg : this . stringService . authAdminExists ,
337
339
data : superAdminExists ,
338
340
} ) ;
339
341
} catch ( error ) {
@@ -379,7 +381,7 @@ class AuthController {
379
381
) ;
380
382
381
383
return res . success ( {
382
- msg : successMessages . AUTH_CREATE_RECOVERY_TOKEN ,
384
+ msg : this . stringService . authCreateRecoveryToken ,
383
385
data : msgId ,
384
386
} ) ;
385
387
} catch ( error ) {
@@ -410,7 +412,7 @@ class AuthController {
410
412
await this . db . validateRecoveryToken ( req , res ) ;
411
413
412
414
return res . success ( {
413
- msg : successMessages . AUTH_VERIFY_RECOVERY_TOKEN ,
415
+ msg : this . stringService . authVerifyRecoveryToken ,
414
416
} ) ;
415
417
} catch ( error ) {
416
418
next ( handleError ( error , SERVICE_NAME , "validateRecoveryTokenController" ) ) ;
@@ -443,7 +445,7 @@ class AuthController {
443
445
const token = this . issueToken ( user . _doc , tokenType . ACCESS_TOKEN , appSettings ) ;
444
446
445
447
return res . success ( {
446
- msg : successMessages . AUTH_RESET_PASSWORD ,
448
+ msg : this . stringService . authResetPassword ,
447
449
data : { user, token } ,
448
450
} ) ;
449
451
} catch ( error ) {
@@ -497,7 +499,7 @@ class AuthController {
497
499
await this . db . deleteUser ( user . _id ) ;
498
500
499
501
return res . success ( {
500
- msg : successMessages . AUTH_DELETE_USER ,
502
+ msg : this . stringService . authDeleteUser ,
501
503
} ) ;
502
504
} catch ( error ) {
503
505
next ( handleError ( error , SERVICE_NAME , "deleteUserController" ) ) ;
@@ -509,7 +511,7 @@ class AuthController {
509
511
const allUsers = await this . db . getAllUsers ( req , res ) ;
510
512
511
513
return res . success ( {
512
- msg : successMessages . AUTH_GET_ALL_USERS ,
514
+ msg : this . stringService . authGetAllUsers ,
513
515
data : allUsers ,
514
516
} ) ;
515
517
} catch ( error ) {
0 commit comments