@@ -32,7 +32,9 @@ const MAIL_EVENT_FOOTER = '\nYou can check the event details on Web Portal.\nFor
32
32
var MongoClient = require ( 'mongodb' ) . MongoClient ;
33
33
var url = 'mongodb://dock:[email protected] :27017/dock' ;
34
34
35
- MongoClient . connect ( url , { useNewUrlParser : true } , function ( err , db ) {
35
+ MongoClient . connect ( url , {
36
+ useNewUrlParser : true
37
+ } , function ( err , db ) {
36
38
if ( err ) throw err ;
37
39
let dbo = db . db ( 'dock' ) ;
38
40
router . use ( fileUpload ( ) ) ;
@@ -83,17 +85,27 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
83
85
if ( ! req . body ) return res . sendStatus ( 400 ) ;
84
86
const email = req . body . email ;
85
87
var pin = Math . floor ( Math . random ( ) * 1000000 ) ;
86
- sendVerificationMail ( email , pin ) ;
87
- const JWTToken = jwt . sign ( {
88
- email : email ,
89
- pin : pin
90
- } ,
91
- APP_SECRET_KEY , {
92
- expiresIn : '2h'
88
+ sendVerificationMail ( email , pin , function ( error ) {
89
+ if ( error ) res . status ( 400 ) . json ( {
90
+ error : true ,
91
+ mssg : error
92
+ } ) ;
93
+
94
+ const JWTToken = jwt . sign ( {
95
+ email : email ,
96
+ pin : pin
97
+ } ,
98
+ APP_SECRET_KEY , {
99
+ expiresIn : '2h'
100
+ } ) ;
101
+ return res . status ( 200 ) . json ( {
102
+ error : false ,
103
+ token : JWTToken
93
104
} ) ;
94
- return res . status ( 200 ) . json ( {
95
- error : false ,
96
- token : JWTToken
105
+ } ) ;
106
+ res . status ( 400 ) . json ( {
107
+ error : true ,
108
+ mssg : 'Something went wrong :('
97
109
} ) ;
98
110
} ) ;
99
111
@@ -112,8 +124,15 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
112
124
} ) ;
113
125
console . log ( decoded ) ;
114
126
if ( decoded . pin == req . body . pin && decoded . email == req . body . email ) {
127
+ const JWTToken = jwt . sign ( {
128
+ email : email
129
+ } ,
130
+ APP_SECRET_KEY , {
131
+ expiresIn : '7d'
132
+ } ) ;
115
133
return res . status ( 200 ) . json ( {
116
- error : false
134
+ error : false ,
135
+ token : JWTToken
117
136
} ) ;
118
137
} else {
119
138
return res . status ( 400 ) . json ( {
@@ -137,7 +156,7 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
137
156
auth : false ,
138
157
message : 'Failed to authenticate token.'
139
158
} ) ;
140
- email = decoded . email ;
159
+ email = decoded . email ;
141
160
} ) ;
142
161
143
162
if ( ! req . body ) return res . status ( 400 ) . send ( {
@@ -185,7 +204,7 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
185
204
} ) ;
186
205
} ) ;
187
206
188
- function sendMail ( reciever , subject , text ) {
207
+ function sendMail ( reciever , subject , text , callback ) {
189
208
var mailOptions = {
190
209
from :
'"Campus Dock" <[email protected] >' ,
191
210
to : reciever ,
@@ -195,12 +214,22 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
195
214
smtpTransport . sendMail ( mailOptions , function ( error , response ) {
196
215
if ( error ) {
197
216
console . log ( error ) ;
217
+ return callback ( error ) ;
198
218
} else {
199
219
console . log ( 'Message sent to: ' + req . session . email ) ;
220
+ return callback ( null ) ;
200
221
}
201
222
} ) ;
202
223
}
203
224
225
+ function sendVerificationMail ( reciever , pin , callback ) {
226
+ var text = 'This is your verification PIN : ' + pin + '.\nThis PIN is valid for 2 hours only.\nNever share your PIN with anyone. If you didn\'t requested PIN, please ignore!' ;
227
+ var subject = 'Verify your E-mail | CampusDock' ;
228
+ sendMail ( reciever , subject , text , function ( error ) {
229
+ return callback ( error ) ;
230
+ } ) ;
231
+ }
232
+
204
233
function sendToScope ( scopeArray , payload , callback ) {
205
234
for ( i = 0 ; i < scopeArray . length ; i ++ ) {
206
235
admin . messaging ( ) . sendToTopic ( scopeArray [ i ] , payload )
@@ -215,22 +244,6 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
215
244
return callback ( null ) ;
216
245
}
217
246
218
- function sendVerificationMail ( email , pin ) {
219
- var mailOptions = {
220
- from :
'"Campus Dock" <[email protected] >' ,
221
- to : email ,
222
- text : 'This is your verification PIN : ' + pin + '.\nThis PIN is valid for 2 hours only.\nNever share your PIN with anyone. If you didn\'t requested PIN, please ignore!' ,
223
- subject : 'Verify your E-mail | CampusDock'
224
- } ;
225
- smtpTransport . sendMail ( mailOptions , function ( error , response ) {
226
- if ( error ) {
227
- console . log ( error ) ;
228
- } else {
229
- console . log ( 'Message sent to: ' + req . session . email ) ;
230
- }
231
- } ) ;
232
- }
233
-
234
247
function saveEventToDB ( email , event_name , event_description , event_start , event_end , event_tags , event_audience , media , callback ) {
235
248
var creator = email ;
236
249
var event_id = creator + '-' + UID ( 6 ) ;
@@ -248,8 +261,9 @@ MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
248
261
} ;
249
262
dbo . collection ( TABLE_EVENTS ) . insertOne ( params , function ( err , data ) {
250
263
if ( err ) callback ( err ) ;
251
- sendMail ( creator , MAIL_EVENT_TITLE , MAIL_EVENT_TEXT + MAIL_EVENT_DEATILS_TITLE + event_name + MAIL_EVENT_FOOTER ) ;
252
- callback ( null ) ;
264
+ sendMail ( creator , MAIL_EVENT_TITLE , MAIL_EVENT_TEXT + MAIL_EVENT_DEATILS_TITLE + event_name + MAIL_EVENT_FOOTER , function ( error ) {
265
+ callback ( error ) ;
266
+ } ) ;
253
267
} ) ;
254
268
}
255
269
0 commit comments