@@ -5,6 +5,8 @@ const { sanitizeBody } = require("express-validator");
5
5
const apiResponse = require ( "../helpers/apiResponse" ) ;
6
6
const bcrypt = require ( "bcrypt" ) ;
7
7
const jwt = require ( "jsonwebtoken" ) ;
8
+ const mailer = require ( "../helpers/mailer" ) ;
9
+ const { constants } = require ( "../helpers/constants" ) ;
8
10
9
11
/**
10
12
* User registration.
@@ -47,27 +49,42 @@ exports.register = [
47
49
} else {
48
50
//hash input password
49
51
bcrypt . hash ( req . body . password , 10 , function ( err , hash ) {
52
+ // generate OTP for confirmation
53
+ let otp = randomNumber ( 4 ) ;
50
54
// Create User object with escaped and trimmed data
51
55
var user = new UserModel (
52
56
{
53
57
firstName : req . body . firstName ,
54
58
lastName : req . body . lastName ,
55
59
email : req . body . email ,
56
- password : hash
60
+ password : hash ,
61
+ confirmOTP : otp
57
62
}
58
63
) ;
59
-
60
- // Save user.
61
- user . save ( function ( err ) {
62
- if ( err ) { return apiResponse . ErrorResponse ( res , err ) ; }
63
- let userData = {
64
- _id : user . _id ,
65
- firstName : user . firstName ,
66
- lastName : user . lastName ,
67
- email : user . email
68
- }
69
- return apiResponse . successResponseWithData ( res , "Registration Success." , userData ) ;
70
- } ) ;
64
+ // Html email body
65
+ let html = '<p>Please Confirm your Account.</p><p>OTP: ' + otp + '</p>' ;
66
+ // Send confirmation email
67
+ mailer . send (
68
+ constants . confirmEmails . from ,
69
+ req . body . email ,
70
+ 'Confirm Account' ,
71
+ html
72
+ ) . then ( function ( response ) {
73
+ // Save user.
74
+ user . save ( function ( err ) {
75
+ if ( err ) { return apiResponse . ErrorResponse ( res , err ) ; }
76
+ let userData = {
77
+ _id : user . _id ,
78
+ firstName : user . firstName ,
79
+ lastName : user . lastName ,
80
+ email : user . email
81
+ }
82
+ return apiResponse . successResponseWithData ( res , "Registration Success." , userData ) ;
83
+ } ) ;
84
+ } ) . catch ( err => {
85
+ console . log ( err )
86
+ return apiResponse . ErrorResponse ( res , err ) ;
87
+ } ) ;
71
88
} ) ;
72
89
}
73
90
} catch ( err ) {
@@ -101,21 +118,31 @@ exports.login = [
101
118
//Compare given password with db's hash.
102
119
bcrypt . compare ( req . body . password , user . password , function ( err , same ) {
103
120
if ( same ) {
104
- let userData = {
105
- _id : user . _id ,
106
- firstName : user . firstName ,
107
- lastName : user . lastName ,
108
- email : user . email ,
121
+ //Check account confirmation.
122
+ if ( user . isConfirmed ) {
123
+ // Check User's account active or not.
124
+ if ( user . status ) {
125
+ let userData = {
126
+ _id : user . _id ,
127
+ firstName : user . firstName ,
128
+ lastName : user . lastName ,
129
+ email : user . email ,
130
+ }
131
+ //Prepare JWT token for authentication
132
+ const jwtPayload = userData ;
133
+ const jwtData = {
134
+ expiresIn : process . env . JWT_TIMEOUT_DURATION ,
135
+ } ;
136
+ const secret = process . env . JWT_SECRET ;
137
+ //Generated JWT token with Payload and secret.
138
+ userData . token = jwt . sign ( jwtPayload , secret , jwtData ) ;
139
+ return apiResponse . successResponseWithData ( res , "Login Success." , userData ) ;
140
+ } else {
141
+ return apiResponse . unauthorizedResponse ( res , "Account is not active. Please contact admin." ) ;
142
+ }
143
+ } else {
144
+ return apiResponse . unauthorizedResponse ( res , "Account is not confirmed. Please confirm your account." ) ;
109
145
}
110
- //Prepare JWT token for authentication
111
- const jwtPayload = userData ;
112
- const jwtData = {
113
- expiresIn : process . env . JWT_TIMEOUT_DURATION ,
114
- } ;
115
- const secret = process . env . JWT_SECRET ;
116
- //Generated JWT token with Payload and secret.
117
- userData . token = jwt . sign ( jwtPayload , secret , jwtData ) ;
118
- return apiResponse . successResponseWithData ( res , "Login Success." , userData ) ;
119
146
} else {
120
147
return apiResponse . unauthorizedResponse ( res , "Email or Password wrong." ) ;
121
148
}
@@ -128,4 +155,68 @@ exports.login = [
128
155
} catch ( err ) {
129
156
return apiResponse . ErrorResponse ( res , err ) ;
130
157
}
131
- } ] ;
158
+ } ] ;
159
+
160
+ /**
161
+ * OTP generator.
162
+ *
163
+ * @param {intiger } length
164
+ *
165
+ * @returns {Interger }
166
+ */
167
+ function randomNumber ( length ) {
168
+ var text = "" ;
169
+ var possible = "123456789" ;
170
+ for ( var i = 0 ; i < length ; i ++ ) {
171
+ var sup = Math . floor ( Math . random ( ) * possible . length ) ;
172
+ text += i > 0 && sup == i ? "0" : possible . charAt ( sup ) ;
173
+ }
174
+ return Number ( text ) ;
175
+ }
176
+
177
+ /**
178
+ * Verify Confirm otp.
179
+ *
180
+ * @param {string } email
181
+ * @param {string } otp
182
+ *
183
+ * @returns {Object }
184
+ */
185
+ exports . verifyConfirm = [
186
+ body ( "email" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( "Email must be specified." )
187
+ . isEmail ( ) . withMessage ( "Email must be a valid email address." ) ,
188
+ body ( "otp" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( "OTP must be specified." ) ,
189
+ sanitizeBody ( "email" ) . escape ( ) ,
190
+ sanitizeBody ( "otp" ) . escape ( ) ,
191
+ ( req , res , next ) => {
192
+ try {
193
+ const errors = validationResult ( req ) ;
194
+ if ( ! errors . isEmpty ( ) ) {
195
+ return apiResponse . validationErrorWithData ( res , "Validation Error." , errors . array ( ) ) ;
196
+ } else {
197
+ var query = { email : req . body . email } ;
198
+ UserModel . findOne ( query ) . then ( user => {
199
+ if ( user ) {
200
+ //Compare given password with db's hash.
201
+ if ( user . isConfirmed ) {
202
+ //Check account confirmation.
203
+ if ( user . confirmOTP == req . body . otp ) {
204
+ UserModel . findOneAndUpdate ( query , {
205
+ name : 'jason bourne'
206
+ } , options , callback )
207
+ return apiResponse . successResponseWithData ( res , "Login Success." , userData ) ;
208
+ } else {
209
+ return apiResponse . unauthorizedResponse ( res , "Otp does not match" ) ;
210
+ }
211
+ } else {
212
+ return apiResponse . unauthorizedResponse ( res , "Account already confirmed." ) ;
213
+ }
214
+ } else {
215
+ return apiResponse . unauthorizedResponse ( res , "Specified email not found." ) ;
216
+ }
217
+ } ) ;
218
+ }
219
+ } catch ( err ) {
220
+ return apiResponse . ErrorResponse ( res , err ) ;
221
+ }
222
+ } ] ;
0 commit comments