@@ -3,6 +3,7 @@ const { body,validationResult } = require("express-validator");
3
3
const { sanitizeBody } = require ( "express-validator" ) ;
4
4
//helper file to prepare responses.
5
5
const apiResponse = require ( "../helpers/apiResponse" ) ;
6
+ const utility = require ( "../helpers/utility" ) ;
6
7
const bcrypt = require ( "bcrypt" ) ;
7
8
const jwt = require ( "jsonwebtoken" ) ;
8
9
const mailer = require ( "../helpers/mailer" ) ;
@@ -50,7 +51,7 @@ exports.register = [
50
51
//hash input password
51
52
bcrypt . hash ( req . body . password , 10 , function ( err , hash ) {
52
53
// generate OTP for confirmation
53
- let otp = randomNumber ( 4 ) ;
54
+ let otp = utility . randomNumber ( 4 ) ;
54
55
// Create User object with escaped and trimmed data
55
56
var user = new UserModel (
56
57
{
@@ -157,23 +158,6 @@ exports.login = [
157
158
}
158
159
} ] ;
159
160
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
161
/**
178
162
* Verify Confirm otp.
179
163
*
@@ -197,14 +181,18 @@ exports.verifyConfirm = [
197
181
var query = { email : req . body . email } ;
198
182
UserModel . findOne ( query ) . then ( user => {
199
183
if ( user ) {
200
- //Compare given password with db's hash .
201
- if ( user . isConfirmed ) {
184
+ //Check already confirm or not .
185
+ if ( ! user . isConfirmed ) {
202
186
//Check account confirmation.
203
187
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 ) ;
188
+ //Update user as confirmed
189
+ UserModel . findOneAndUpdate ( query , {
190
+ isConfirmed : 1 ,
191
+ confirmOTP : null
192
+ } ) . catch ( err => {
193
+ return apiResponse . ErrorResponse ( res , err ) ;
194
+ } ) ;
195
+ return apiResponse . successResponse ( res , "Account confirmed success." ) ;
208
196
} else {
209
197
return apiResponse . unauthorizedResponse ( res , "Otp does not match" ) ;
210
198
}
@@ -220,3 +208,57 @@ exports.verifyConfirm = [
220
208
return apiResponse . ErrorResponse ( res , err ) ;
221
209
}
222
210
} ] ;
211
+
212
+ /**
213
+ * Resend Confirm otp.
214
+ *
215
+ * @param {string } email
216
+ *
217
+ * @returns {Object }
218
+ */
219
+ exports . resendConfirmOtp = [
220
+ body ( "email" ) . isLength ( { min : 1 } ) . trim ( ) . withMessage ( "Email must be specified." )
221
+ . isEmail ( ) . withMessage ( "Email must be a valid email address." ) ,
222
+ sanitizeBody ( "email" ) . escape ( ) ,
223
+ ( req , res , next ) => {
224
+ try {
225
+ const errors = validationResult ( req ) ;
226
+ if ( ! errors . isEmpty ( ) ) {
227
+ return apiResponse . validationErrorWithData ( res , "Validation Error." , errors . array ( ) ) ;
228
+ } else {
229
+ var query = { email : req . body . email } ;
230
+ UserModel . findOne ( query ) . then ( user => {
231
+ if ( user ) {
232
+ //Check already confirm or not.
233
+ if ( ! user . isConfirmed ) {
234
+ // Generate otp
235
+ let otp = utility . randomNumber ( 4 ) ;
236
+ // Html email body
237
+ let html = '<p>Please Confirm your Account.</p><p>OTP: ' + otp + '</p>' ;
238
+ // Send confirmation email
239
+ mailer . send (
240
+ constants . confirmEmails . from ,
241
+ req . body . email ,
242
+ 'Confirm Account' ,
243
+ html
244
+ ) . then ( function ( response ) {
245
+ user . isConfirmed = 0 ;
246
+ user . confirmOTP = otp ;
247
+ // Save user.
248
+ user . save ( function ( err ) {
249
+ if ( err ) { return apiResponse . ErrorResponse ( res , err ) ; }
250
+ return apiResponse . successResponse ( res , "Confirm otp sent." ) ;
251
+ } ) ;
252
+ } ) ;
253
+ } else {
254
+ return apiResponse . unauthorizedResponse ( res , "Account already confirmed." ) ;
255
+ }
256
+ } else {
257
+ return apiResponse . unauthorizedResponse ( res , "Specified email not found." ) ;
258
+ }
259
+ } ) ;
260
+ }
261
+ } catch ( err ) {
262
+ return apiResponse . ErrorResponse ( res , err ) ;
263
+ }
264
+ } ] ;
0 commit comments