11const 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" ) ;
44//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" ) ;
88
99/**
1010 * User registration.
11- *
12- * @param {string } firstName
11+ *
12+ * @param {string } firstName
1313 * @param {string } lastName
1414 * @param {string } email
1515 * @param {string } password
16- *
16+ *
1717 * @returns {Object }
1818 */
1919exports . register = [
2020 // 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 ) => {
2828 if ( user ) {
29- return Promise . reject ( ' E-mail already in use' ) ;
29+ return Promise . reject ( " E-mail already in use" ) ;
3030 }
3131 } ) ;
3232 } ) ,
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." ) ,
3434 // 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 ( ) ,
3939 // Process request after validation and sanitization.
4040 ( req , res , next ) => {
4141 try {
4242 // Extract the validation errors from a request.
4343 const errors = validationResult ( req ) ;
4444 if ( ! errors . isEmpty ( ) ) {
4545 // Display sanitized values/errors messages.
46- return apiResponse . validationErrorWithData ( res , ' Validation Error.' , errors . array ( ) ) ;
46+ return apiResponse . validationErrorWithData ( res , " Validation Error." , errors . array ( ) ) ;
4747 } else {
4848 //hash input password
4949 bcrypt . hash ( req . body . password , 10 , function ( err , hash ) {
@@ -53,7 +53,7 @@ exports.register = [
5353 firstName : req . body . firstName ,
5454 lastName : req . body . lastName ,
5555 email : req . body . email ,
56- password : hash ,
56+ password : hash
5757 }
5858 ) ;
5959
@@ -64,38 +64,37 @@ exports.register = [
6464 _id : user . _id ,
6565 firstName : user . firstName ,
6666 lastName : user . lastName ,
67- email : user . email ,
67+ email : user . email
6868 }
69- return apiResponse . successResponseWithData ( res , 'Registration Success.' , userData ) ;
70- } ) ;
71- return ;
69+ return apiResponse . successResponseWithData ( res , "Registration Success." , userData ) ;
70+ } ) ;
7271 } ) ;
7372 }
7473 } catch ( err ) {
75- //throw error in json response with status 500.
74+ //throw error in json response with status 500.
7675 return apiResponse . ErrorResponse ( res , err ) ;
77- }
76+ }
7877} ] ;
7978
8079/**
8180 * User login.
82- *
81+ *
8382 * @param {string } email
8483 * @param {string } password
85- *
84+ *
8685 * @returns {Object }
8786 */
8887exports . 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 ( ) ,
9493 ( req , res , next ) => {
9594 try {
9695 const errors = validationResult ( req ) ;
9796 if ( ! errors . isEmpty ( ) ) {
98- return apiResponse . validationErrorWithData ( res , ' Validation Error.' , errors . array ( ) ) ;
97+ return apiResponse . validationErrorWithData ( res , " Validation Error." , errors . array ( ) ) ;
9998 } else {
10099 UserModel . findOne ( { email : req . body . email } ) . then ( user => {
101100 if ( user ) {
@@ -116,17 +115,17 @@ exports.login = [
116115 const secret = process . env . JWT_SECRET ;
117116 //Generated JWT token with Payload and secret.
118117 userData . token = jwt . sign ( jwtPayload , secret , jwtData ) ;
119- return apiResponse . successResponseWithData ( res , ' Login Success.' , userData ) ;
118+ return apiResponse . successResponseWithData ( res , " Login Success." , userData ) ;
120119 } else {
121- return apiResponse . unauthorizedResponse ( res , ' Email or Password wrong.' ) ;
120+ return apiResponse . unauthorizedResponse ( res , " Email or Password wrong." ) ;
122121 }
123122 } ) ;
124123 } else {
125- return apiResponse . unauthorizedResponse ( res , ' Email or Password wrong.' ) ;
124+ return apiResponse . unauthorizedResponse ( res , " Email or Password wrong." ) ;
126125 }
127126 } ) ;
128127 }
129128 } catch ( err ) {
130129 return apiResponse . ErrorResponse ( res , err ) ;
131- }
130+ }
132131} ] ;
0 commit comments