@@ -7,7 +7,8 @@ import { UserController, ServerError, UserInfo } from '../../types';
7
7
// const secret = JWT_SECRET;
8
8
9
9
/**
10
- * @description Contains middleware that creates new user in database, gets all users from database for system admin, and verifies user exists before sending back user data to login component
10
+ * @description Contains middleware that creates new user in database, gets all users from database verifies if user exists before sending back user data to login component
11
+ * v12.0 implemented cookies for user sessions and commented out all system admin implementaion since it was nonfunctional
11
12
*/
12
13
13
14
const userController : UserController = {
@@ -16,14 +17,13 @@ const userController: UserController = {
16
17
res : Response ,
17
18
next : NextFunction
18
19
) : Promise < void > => {
19
- console . log ( 'in userController.createUser' ) ;
20
20
21
21
try {
22
22
const {
23
23
username,
24
24
password,
25
25
// role_id,
26
- } : { username : string ; password : string ; } = req . body ;
26
+ } : { username : string ; password : string ; } = req . body ;
27
27
// hash password
28
28
const hashedPassword = await bcrypt . hash ( password , 10 ) ;
29
29
@@ -48,9 +48,6 @@ const userController: UserController = {
48
48
// create an array, userDetails, to hold values from our createUser SQL query placeholders.
49
49
const userDetails : string [ ] = [ username , hashedPassword ] ;
50
50
const createdUser = await db . query ( createUser , userDetails ) ;
51
-
52
- console . log ( 'createdUser: ' , createdUser . rows [ 0 ] ) ;
53
-
54
51
res . locals . user = createdUser . rows [ 0 ] ;
55
52
return next ( ) ;
56
53
} catch ( err : unknown ) {
@@ -107,12 +104,10 @@ const userController: UserController = {
107
104
req . body ;
108
105
// using username we create a query string to grab that user
109
106
const getUser = 'SELECT * FROM users WHERE username=$1;' ;
110
- // using bcrypt we check if client's password input matches the password of that username in the db; we then add to locals accordingly
107
+ // using bcrypt we check if client's password input matches the password of that username in the db; we then add to locals accordingly
111
108
db . query ( getUser , [ username ] )
112
109
. then ( async ( data : any ) => {
113
- console . log ( data . rows [ 0 ] ) ;
114
110
const match = await bcrypt . compare ( password , data . rows [ 0 ] . password ) ;
115
- console . log ( match ) ;
116
111
if ( ! data . rows [ 0 ] || ! match ) {
117
112
return next ( {
118
113
log : 'Error in userController\'s verifyUser method' ,
@@ -124,9 +119,8 @@ const userController: UserController = {
124
119
}
125
120
const verifiedUser = data . rows [ 0 ] ;
126
121
res . locals . user = verifiedUser ;
127
- console . log ( 'verified user' , verifiedUser ) ;
128
122
return next ( ) ;
129
-
123
+
130
124
// const verifiedRole = verifiedUser.role;
131
125
// if (verifiedRole === 'system admin') {
132
126
// await jwt.sign({ verifiedRole }, secret, (err, token) => {
@@ -157,11 +151,8 @@ const userController: UserController = {
157
151
} ) ;
158
152
} ,
159
153
160
-
161
154
162
155
updatePassword : ( req : Request , res : Response , next : NextFunction ) : void => {
163
- // if there is an error property on res.locals, return next(). i.e., incorrect password entered
164
-
165
156
const { newHashedPassword } : { newHashedPassword : string } = res . locals as {
166
157
newHashedPassword : string ;
167
158
} ;
@@ -226,29 +217,29 @@ const userController: UserController = {
226
217
} ) ;
227
218
} ,
228
219
220
+ // adding cookie
229
221
addCookie : ( req : Request , res : Response , next : NextFunction ) : void => {
230
- console . log ( 'we are adding the cookie here right now' ) ;
231
222
res . cookie ( 'loggedIn' , true ) ;
232
223
return next ( ) ;
233
224
} ,
234
225
226
+ // verify cookie on refresh
235
227
checkCookie : ( req : Request , res : Response , next : NextFunction ) : void => {
236
228
if ( req . cookies . loggedIn ) res . locals . signedIn = true ;
237
229
else res . locals . signedIn = false ;
238
230
return next ( ) ;
239
231
} ,
240
232
233
+ // remove cookie on logout
241
234
removeCookie : ( req : Request , res : Response , next : NextFunction ) : void => {
242
- console . log ( 'abt to rmv cookie' ) ;
243
235
res . clearCookie ( 'loggedIn' ) ;
244
- console . log ( 'cookied rmvd' ) ;
245
236
res . locals . loggedOut = true ;
246
237
return next ( ) ;
247
238
} ,
248
239
} ;
249
240
export default userController ;
250
241
251
- // not currently in use.
242
+ // not currently in use (from v12.0)
252
243
253
244
// switches role of user upon designation by system admin
254
245
// switchUserRole: (req: Request, res: Response, next: NextFunction) => {
@@ -303,7 +294,6 @@ export default userController;
303
294
// });
304
295
// }
305
296
// const verifiedUser = data.rows[0];
306
- // console.log('verified user', verifiedUser);
307
297
// res.locals.verifiedUser = verifiedUser;
308
298
// const verifiedRole = verifiedUser.role;
309
299
// if (verifiedRole === 'system admin') {
0 commit comments