11// External Modules
2- import boom , { Boom } from '@hapi/boom' ;
2+ import boom from '@hapi/boom' ;
33import { NextFunction , Request , Response } from 'express' ;
44import bcrypt from 'bcrypt' ;
55import jwt from 'jsonwebtoken' ;
6+ import { LogArgument } from 'rollbar' ;
67
78// Internal Modules
89import User from './schema/user-schema' ;
9- import { Config } from '../app/config/config' ;
10- import { UsernameType } from './interfaces/user-interface' ;
10+ import { Config } from 'src/app/config/config' ;
11+ import { rollbar } from 'src/app/config/rollbar' ;
12+ import { MiddlewareUser } from './interfaces/user-interface' ;
1113
1214/**
13- * @description Validates if the user exist in the database
15+ * Checks if a user with the given username already exists in the database
16+ * @param {Object } req.body - request body containing the username
1417 * @param {string } req.body.username - the username to check
15- * @returns {Response<Boom | NextFunction> } Response with the next function
18+ * @param {Response } res - response object
19+ * @param {NextFunction } next - next function
20+ * @returns {Promise<MiddlewareUser | NextFunction> }
1621 */
1722const userExists = async (
1823 req : Request ,
1924 res : Response ,
2025 next : NextFunction ,
21- ) : Promise < Boom | NextFunction | Response | unknown > => {
22- const { username } : UsernameType = req . body ;
23- const user : UsernameType | null = await User . findOne ( {
24- username : { $eq : username } ,
25- } ) ;
26- return user ? res . json ( boom . conflict ( 'User already exists' ) ) : next ( ) ;
26+ ) : Promise < MiddlewareUser > => {
27+ const { username } = req . body ;
28+
29+ try {
30+ const user = await User . findOne ( { username : { $eq : username } } ) ;
31+
32+ if ( user ) {
33+ return res . status ( 409 ) . json ( { error : 'User already exists' } ) ;
34+ }
35+
36+ return next ( ) ;
37+ } catch ( error : unknown ) {
38+ rollbar . error ( error as LogArgument ) ;
39+ return res . status ( 500 ) . json ( { error } ) ;
40+ }
2741} ;
2842
2943/**
3044 * @description Validates the user username & password of the request body
3145 * @param {string } req.body.username - the username to validate
3246 * @param {string } req.body.password - the password to validate
33- * @returns {Response<Boom | NextFunction } if the username && password match then next() else return an error
47+ * @returns {Promise<MiddlewareUser | NextFunction> } if the username & password match, then next() else return an error
3448 */
3549const validateUser = async (
3650 req : Request ,
3751 res : Response ,
3852 next : NextFunction ,
39- ) : Promise < Boom | NextFunction | Response | unknown > => {
53+ ) : Promise < MiddlewareUser > => {
4054 try {
4155 const { username, password } = req . body ;
42- const userExists = await User . findOne ( { username : { $eq : username } } ) ;
43- const user = userExists ?. username ;
44- const pass = userExists ?. password ;
45- const isMatch = pass && ( await bcrypt . compare ( password , pass ) ) ; // Compare the password with the hash password
46- const isValid = user && isMatch ;
47- return isValid ? next ( ) : res . json ( boom . badRequest ( 'Invalid credentials' ) ) ;
56+ const user = await User . findOne ( { username : { $eq : username } } ) ;
57+
58+ if ( user ?. password && ( await bcrypt . compare ( password , user . password ) ) ) {
59+ return next ( ) ;
60+ }
61+
62+ return res . status ( 401 ) . json ( boom . unauthorized ( 'Invalid credentials' ) ) ;
4863 } catch ( error ) {
49- return res . status ( 400 ) && res . json ( boom . badRequest ( 'Something went wrong ' ) ) ;
64+ return res . status ( 400 ) . json ( boom . badRequest ( 'User not found ' ) ) ;
5065 }
5166} ;
5267
5368/**
5469 * @description Validate if the user is administrator
5570 * @param {string } req.body.username - the username to validate
56- * @returns {Promise<Boom | NextFunction | Response | unknown > } if the user is admin then next() else return an error
71+ * @returns {Promise<MiddlewareUser | NextFunction> } if the user is admin then next() else return an error
5772 */
5873const isAdmin = async (
5974 req : Request ,
6075 res : Response ,
6176 next : NextFunction ,
62- ) : Promise < Boom | NextFunction | Response | unknown > => {
77+ ) : Promise < MiddlewareUser > => {
78+ const { username } = req . body ;
79+
6380 try {
64- const { username } = req . body ;
6581 const user = await User . findOne ( { username : { $eq : username } } ) ;
66- return user ?. isAdmin ? next ( ) : res . json ( boom . unauthorized ( 'Not admin' ) ) ;
82+
83+ if ( user ?. isAdmin ) {
84+ return next ( ) ;
85+ }
86+
87+ return res . status ( 401 ) . json ( boom . unauthorized ( 'Not admin' ) ) ;
6788 } catch ( error ) {
68- return res . status ( 400 ) && res . json ( boom . badRequest ( 'User not found' ) ) ;
89+ return res . status ( 400 ) . json ( boom . badRequest ( 'User not found' ) ) ;
6990 }
7091} ;
7192
7293/**
73- * @description Validate the jwt
94+ * @description Validate the JWT
7495 * @param {Authorization } req.headers - Authorization header with the token
7596 * @returns {Response<Boom | NextFunction> } Authorization error or next
7697 */
7798const validateToken = async (
7899 req : Request ,
79100 res : Response ,
80101 next : NextFunction ,
81- ) : Promise < Boom | NextFunction | Response | unknown > => {
102+ ) : Promise < MiddlewareUser > => {
82103 const { secret } = Config . jwt ;
83104 try {
84105 const { authorization } = req . headers ;
@@ -88,7 +109,7 @@ const validateToken = async (
88109 return decoded ? next ( ) : res . json ( boom . unauthorized ( ) ) ;
89110 }
90111 } catch ( error ) {
91- return res . status ( 401 ) . json ( boom . unauthorized ( 'Invalid token' ) ) ;
112+ return res . status ( 401 ) . json ( boom . unauthorized ( ) ) ;
92113 }
93114} ;
94115
0 commit comments