1
1
const UserModel = require ( "../models/UserModel" ) ;
2
- const { body, validationResult } = require ( ' express-validator' ) ;
3
- const { sanitizeBody } = require ( ' express-validator' ) ;
2
+ const { body, validationResult } = require ( " express-validator" ) ;
3
+ const { sanitizeBody } = require ( " express-validator" ) ;
4
4
//helper file to prepare responses.
5
- const apiResponse = require ( ' ../helpers/apiResponse' ) ;
6
- const bcrypt = require ( ' bcrypt' ) ;
7
- const jwt = require ( ' jsonwebtoken' ) ;
5
+ const apiResponse = require ( " ../helpers/apiResponse" ) ;
6
+ const bcrypt = require ( " bcrypt" ) ;
7
+ const jwt = require ( " jsonwebtoken" ) ;
8
8
9
9
/**
10
10
* User registration.
11
- *
12
- * @param {string } firstName
11
+ *
12
+ * @param {string } firstName
13
13
* @param {string } lastName
14
14
* @param {string } email
15
15
* @param {string } password
16
- *
16
+ *
17
17
* @returns {Object }
18
18
*/
19
19
exports . register = [
20
20
// Validate fields.
21
- body ( ' firstName' ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( ' First name must be specified.' )
22
- . isAlphanumeric ( ) . withMessage ( ' First name has non-alphanumeric characters.' ) ,
23
- body ( ' lastName' ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( ' Last name must be specified.' )
24
- . isAlphanumeric ( ) . withMessage ( ' Last name has non-alphanumeric characters.' ) ,
25
- body ( ' email' ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( ' Email must be specified.' )
26
- . isEmail ( ) . withMessage ( ' Email must be a valid email address.' ) . custom ( value => {
27
- return UserModel . findOne ( { email : value } ) . then ( user => {
21
+ body ( " firstName" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( " First name must be specified." )
22
+ . isAlphanumeric ( ) . withMessage ( " First name has non-alphanumeric characters." ) ,
23
+ body ( " lastName" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( " Last name must be specified." )
24
+ . isAlphanumeric ( ) . withMessage ( " Last name has non-alphanumeric characters." ) ,
25
+ body ( " email" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( " Email must be specified." )
26
+ . isEmail ( ) . withMessage ( " Email must be a valid email address." ) . custom ( ( value ) => {
27
+ return UserModel . findOne ( { email : value } ) . then ( ( user ) => {
28
28
if ( user ) {
29
- return Promise . reject ( ' E-mail already in use' ) ;
29
+ return Promise . reject ( " E-mail already in use" ) ;
30
30
}
31
31
} ) ;
32
32
} ) ,
33
- body ( ' password' ) . isLength ( { min : 6 } ) . trim ( ) . withMessage ( ' Password must be 6 characters or greater.' ) ,
33
+ body ( " password" ) . isLength ( { min : 6 } ) . trim ( ) . withMessage ( " Password must be 6 characters or greater." ) ,
34
34
// Sanitize fields.
35
- sanitizeBody ( ' firstName' ) . escape ( ) ,
36
- sanitizeBody ( ' lastName' ) . escape ( ) ,
37
- sanitizeBody ( ' email' ) . escape ( ) ,
38
- sanitizeBody ( ' password' ) . escape ( ) ,
35
+ sanitizeBody ( " firstName" ) . escape ( ) ,
36
+ sanitizeBody ( " lastName" ) . escape ( ) ,
37
+ sanitizeBody ( " email" ) . escape ( ) ,
38
+ sanitizeBody ( " password" ) . escape ( ) ,
39
39
// Process request after validation and sanitization.
40
40
( req , res , next ) => {
41
41
try {
42
42
// Extract the validation errors from a request.
43
43
const errors = validationResult ( req ) ;
44
44
if ( ! errors . isEmpty ( ) ) {
45
45
// Display sanitized values/errors messages.
46
- return apiResponse . validationErrorWithData ( res , ' Validation Error.' , errors . array ( ) ) ;
46
+ return apiResponse . validationErrorWithData ( res , " Validation Error." , errors . array ( ) ) ;
47
47
} else {
48
48
//hash input password
49
49
bcrypt . hash ( req . body . password , 10 , function ( err , hash ) {
@@ -53,7 +53,7 @@ exports.register = [
53
53
firstName : req . body . firstName ,
54
54
lastName : req . body . lastName ,
55
55
email : req . body . email ,
56
- password : hash ,
56
+ password : hash
57
57
}
58
58
) ;
59
59
@@ -64,38 +64,37 @@ exports.register = [
64
64
_id : user . _id ,
65
65
firstName : user . firstName ,
66
66
lastName : user . lastName ,
67
- email : user . email ,
67
+ email : user . email
68
68
}
69
- return apiResponse . successResponseWithData ( res , 'Registration Success.' , userData ) ;
70
- } ) ;
71
- return ;
69
+ return apiResponse . successResponseWithData ( res , "Registration Success." , userData ) ;
70
+ } ) ;
72
71
} ) ;
73
72
}
74
73
} catch ( err ) {
75
- //throw error in json response with status 500.
74
+ //throw error in json response with status 500.
76
75
return apiResponse . ErrorResponse ( res , err ) ;
77
- }
76
+ }
78
77
} ] ;
79
78
80
79
/**
81
80
* User login.
82
- *
81
+ *
83
82
* @param {string } email
84
83
* @param {string } password
85
- *
84
+ *
86
85
* @returns {Object }
87
86
*/
88
87
exports . login = [
89
- body ( ' email' ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( ' Email name must be specified.' )
90
- . isEmail ( ) . withMessage ( ' Email must be a valid email address.' ) ,
91
- body ( ' password' ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( ' Password must be specified.' ) ,
92
- sanitizeBody ( ' email' ) . escape ( ) ,
93
- sanitizeBody ( ' password' ) . escape ( ) ,
88
+ body ( " email" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( " Email name must be specified." )
89
+ . isEmail ( ) . withMessage ( " Email must be a valid email address." ) ,
90
+ body ( " password" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( " Password must be specified." ) ,
91
+ sanitizeBody ( " email" ) . escape ( ) ,
92
+ sanitizeBody ( " password" ) . escape ( ) ,
94
93
( req , res , next ) => {
95
94
try {
96
95
const errors = validationResult ( req ) ;
97
96
if ( ! errors . isEmpty ( ) ) {
98
- return apiResponse . validationErrorWithData ( res , ' Validation Error.' , errors . array ( ) ) ;
97
+ return apiResponse . validationErrorWithData ( res , " Validation Error." , errors . array ( ) ) ;
99
98
} else {
100
99
UserModel . findOne ( { email : req . body . email } ) . then ( user => {
101
100
if ( user ) {
@@ -116,17 +115,17 @@ exports.login = [
116
115
const secret = process . env . JWT_SECRET ;
117
116
//Generated JWT token with Payload and secret.
118
117
userData . token = jwt . sign ( jwtPayload , secret , jwtData ) ;
119
- return apiResponse . successResponseWithData ( res , ' Login Success.' , userData ) ;
118
+ return apiResponse . successResponseWithData ( res , " Login Success." , userData ) ;
120
119
} else {
121
- return apiResponse . unauthorizedResponse ( res , ' Email or Password wrong.' ) ;
120
+ return apiResponse . unauthorizedResponse ( res , " Email or Password wrong." ) ;
122
121
}
123
122
} ) ;
124
123
} else {
125
- return apiResponse . unauthorizedResponse ( res , ' Email or Password wrong.' ) ;
124
+ return apiResponse . unauthorizedResponse ( res , " Email or Password wrong." ) ;
126
125
}
127
126
} ) ;
128
127
}
129
128
} catch ( err ) {
130
129
return apiResponse . ErrorResponse ( res , err ) ;
131
- }
130
+ }
132
131
} ] ;
0 commit comments