2
2
* @module Bcrypt Controller
3
3
* @description Contains middleware that encrypts password before storing in database and compares a user's inputted password to their stored password
4
4
*/
5
- const db = require ( ' ../models/cloudModel' ) ;
6
- const bcrypt = require ( ' bcryptjs' ) ;
5
+ const db = require ( " ../models/cloudModel" ) ;
6
+ const bcrypt = require ( " bcryptjs" ) ;
7
7
8
8
const bcryptController = { } ;
9
9
@@ -12,69 +12,80 @@ bcryptController.hashPassword = (req, res, next) => {
12
12
const { password } = req . body ;
13
13
const saltRounds = 10 ;
14
14
15
- bcrypt . hash ( password , saltRounds )
15
+ bcrypt
16
+ . hash ( password , saltRounds )
16
17
. then ( ( hash ) => {
17
18
res . locals . hash = hash ;
18
19
return next ( ) ;
19
20
} )
20
21
. catch ( ( err ) => {
21
22
return next ( {
22
23
log : `Error in bcryptController hashPassword: ${ err } ` ,
23
- message : { err : 'An error occured creating hash with bcrypt. See bcryptController.hashPassword.' } ,
24
+ message : {
25
+ err : "An error occured creating hash with bcrypt. See bcryptController.hashPassword." ,
26
+ } ,
24
27
} ) ;
25
28
} ) ;
26
29
} ;
27
30
28
31
// Hash new user password with bCrypt - User updated password
29
32
bcryptController . hashNewPassword = async ( req , res , next ) => {
30
-
31
33
// if there is an error property on res.locals, return next(). i.e., incorrect password entered
32
- if ( Object . prototype . hasOwnProperty . call ( res . locals , ' error' ) ) {
34
+ if ( Object . prototype . hasOwnProperty . call ( res . locals , " error" ) ) {
33
35
return next ( ) ;
34
36
}
35
37
// else bCrypt the new password and move to next middleware
36
38
const { newPassword } = req . body ;
37
39
const saltRounds = 10 ;
38
40
39
- await bcrypt . hash ( newPassword , saltRounds )
41
+ await bcrypt
42
+ . hash ( newPassword , saltRounds )
40
43
. then ( ( hash ) => {
41
44
res . locals . hash = hash ;
42
45
return next ( ) ;
43
46
} )
44
47
. catch ( ( err ) => {
45
48
return next ( {
46
49
log : `Error in bcryptController hashNewPassword: ${ err } ` ,
47
- message : { err : 'An error occured creating hash with bcrypt. See bcryptController.hashNewPassword.' } ,
50
+ message : {
51
+ err : "An error occured creating hash with bcrypt. See bcryptController.hashNewPassword." ,
52
+ } ,
48
53
} ) ;
49
54
} ) ;
50
55
} ;
51
56
52
57
/**
53
58
* @description hashes the locals property cookie. Creates a column in the database to store the hashed cookie
54
59
*/
55
- bcryptController . hashCookie = ( req , res , next ) => {
60
+
61
+ bcryptController . hashCookie = ( req , res , next ) => {
56
62
const { role_id, username } = res . locals . user ;
57
63
const saltRounds = 10 ;
58
64
if ( role_id === 1 ) {
59
- bcrypt . hash ( res . locals . cookie , saltRounds )
65
+ bcrypt
66
+ . hash ( res . locals . cookie , saltRounds )
60
67
. then ( ( hash ) => {
61
68
res . locals . user . token = hash ;
62
- db . query ( 'ALTER TABLE users ADD COLUMN IF NOT EXISTS token varchar(250)' ) ;
63
- db . query ( 'UPDATE users SET token=$1 WHERE username=$2' , [ res . locals . user . token , username ] ) ;
69
+ db . query (
70
+ "ALTER TABLE users ADD COLUMN IF NOT EXISTS token varchar(250)"
71
+ ) ;
72
+ db . query ( "UPDATE users SET token=$1 WHERE username=$2" , [
73
+ res . locals . user . token ,
74
+ username ,
75
+ ] ) ;
64
76
return next ( ) ;
65
77
} )
66
78
. catch ( ( err ) => {
67
79
return next ( {
68
80
log : `Error in bcryptController hashCookeis: ${ err } ` ,
69
- message : { err : 'An error occured creating hash with bcrypt. See bcryptController.hashCookies.' } ,
81
+ message : {
82
+ err : "An error occured creating hash with bcrypt. See bcryptController.hashCookies." ,
83
+ } ,
70
84
} ) ;
71
85
} ) ;
72
86
} else {
73
87
return next ( ) ;
74
88
}
75
89
} ;
76
90
77
-
78
-
79
-
80
- module . exports = bcryptController ;
91
+ module . exports = bcryptController ;
0 commit comments