@@ -53,12 +53,23 @@ const validateUser = async (
5353 next : NextFunction ,
5454) : Promise < MiddlewareUser > => {
5555 try {
56- const { username, password } = req . body as { username : string ; password : string } ;
57- const [ sanitizedUsername , sanitizedPassword ] = [ username . toString ( ) , password . toString ( ) ] ;
56+ const { username, password } = req . body as {
57+ username : string ;
58+ password : string ;
59+ } ;
60+ const [ sanitizedUsername , sanitizedPassword ] = [
61+ username . toString ( ) ,
62+ password . toString ( ) ,
63+ ] ;
5864
59- const user = await User . findOne ( { $expr : { $eq : [ '$username' , sanitizedUsername ] } } ) ;
65+ const user = await User . findOne ( {
66+ $expr : { $eq : [ '$username' , sanitizedUsername ] } ,
67+ } ) ;
6068
61- if ( user ?. password && ( await bcrypt . compare ( sanitizedPassword , user . password ) ) ) {
69+ if (
70+ user ?. password &&
71+ ( await bcrypt . compare ( sanitizedPassword , user . password ) )
72+ ) {
6273 return next ( ) ;
6374 }
6475
@@ -82,7 +93,9 @@ const isAdmin = async (
8293 const sanitizedUsername = username . toString ( ) ;
8394
8495 try {
85- const user = await User . findOne ( { $expr : { $eq : [ '$username' , sanitizedUsername ] } } ) ;
96+ const user = await User . findOne ( {
97+ $expr : { $eq : [ '$username' , sanitizedUsername ] } ,
98+ } ) ;
8699
87100 if ( user ?. isAdmin ) {
88101 return next ( ) ;
@@ -108,10 +121,10 @@ const validateToken = async (
108121 try {
109122 const { authorization } = req . headers ;
110123 const token = authorization ?. replace ( 'Bearer ' , '' ) ;
124+ if ( ! token ) return res . status ( 401 ) . json ( boom . unauthorized ( ) ) ;
111125 if ( secret ) {
112- if ( ! token ) return res . json ( boom . unauthorized ( ) ) ;
113126 const decoded = jwt . verify ( token , secret ) ;
114- return decoded ? next ( ) : res . json ( boom . unauthorized ( ) ) ;
127+ return decoded ? next ( ) : res . status ( 401 ) . json ( boom . unauthorized ( ) ) ;
115128 }
116129 } catch ( error ) {
117130 return res . status ( 401 ) . json ( boom . unauthorized ( ) ) ;
0 commit comments