1
1
"use strict" ;
2
2
3
- const User = require ( "../models/user" ) ;
3
+ const User = require ( "../models/user" ) ,
4
+ Token = require ( "../models/token" ) ,
5
+ crypto = require ( "crypto" ) ,
6
+ passport = require ( "passport" ) ,
7
+ nodemailer = require ( "nodemailer" ) ;
8
+
9
+
4
10
const getUserParams = body => {
5
11
return {
6
12
name : {
@@ -37,64 +43,125 @@ module.exports = {
37
43
req . flash (
38
44
"Success" ,
39
45
`${ user . name . first } 's account created successfully!`
40
-
41
46
) ;
42
47
console . log ( user ) ;
43
- res . locals . redirect = `/user/${ user . name . first } ` ;
48
+ // Create a verification token for this user
49
+ var token = new Token ( {
50
+ _userId : user . _id ,
51
+ token : crypto . randomBytes ( 16 ) . toString ( "hex" ) ,
52
+ } ) ;
53
+
54
+ // Save the verification token
55
+ token . save ( function ( err ) {
56
+ if ( err ) {
57
+ return res . status ( 500 ) . send ( { msg : err . message } ) ;
58
+ }
59
+
60
+ // Send the email
61
+ var transporter = nodemailer . createTransport ( {
62
+ service : "SendGrid" ,
63
+ auth : {
64
+ user : "apikey" ,
65
+ pass :
66
+ "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" ,
67
+ } ,
68
+ } ) ;
69
+ var mailOptions = {
70
+ from : "priyankrastogi14@gmail.com" ,
71
+ to : user . email ,
72
+ subject : "Account Verification Token" ,
73
+ // text:
74
+ // "Hello,\n\n" +
75
+ // "Please verify your account by clicking the link \n",
76
+ html : `<p>Please verify your Smartier account by clicking the link below</p><a href=http:\/\/${ req . headers . host } \/confirmation\/${ token . token } >Link</a>` ,
77
+ } ;
78
+ transporter . sendMail ( mailOptions , function ( err ) {
79
+ if ( err ) {
80
+ console . log ( err . stack ) ;
81
+ res . locals . redirect = "/" ;
82
+ next ( ) ;
83
+ }
84
+ // res.status(200).send('A verification email has been sent to Your email address.');
85
+ } ) ;
86
+ } ) ;
87
+ res . locals . redirect = `/feed` ;
44
88
next ( ) ;
45
89
} else {
46
90
req . flash (
47
91
"error" ,
48
92
`Failed to create user account because: ${ error . message } .`
49
93
) ;
50
94
res . locals . redirect = "/" ;
51
- next ( )
95
+ next ( ) ;
52
96
}
53
97
} ) ;
54
98
} ,
55
99
userFeed : ( req , res ) => {
56
- res . render ( "user" , { userName :req . params . user } ) ;
57
-
100
+ res . render ( "feed" ) ;
58
101
} ,
59
102
redirectView : ( req , res , next ) => {
60
103
let redirectPath = res . locals . redirect ;
61
104
if ( redirectPath ) res . redirect ( redirectPath ) ;
62
105
} ,
63
- login : ( req , res ) => {
64
- res . render ( "users/login" ) ;
65
- } ,
66
- authenticate : ( req , res , next ) => {
67
- User . findOne ( { email : req . body . email } )
68
- . then ( ( user ) => {
69
- if ( user ) {
70
- user . passwordComparison ( req . body . password ) . then ( ( passwordsMatch ) => {
71
- if ( passwordsMatch ) {
72
- res . locals . redirect = `/user/${ user . _id } ` ;
73
- req . flash ( "success" , `${ user . first } 's logged in successfully!` ) ;
74
- res . locals . user = user ;
75
- } else {
76
- req . flash (
77
- "error" ,
78
- "Failed to log in user account: Incorrect Password."
79
- ) ;
80
- res . locals . redirect = "/login" ;
81
- }
82
- next ( ) ;
83
- } ) ;
84
- } else {
85
- req . flash (
86
- "error" ,
87
- "Failed to log in user account: User account not found."
88
- ) ;
89
- res . locals . redirect = "/login" ;
90
- next ( ) ;
91
- }
106
+ resendToken : function ( req , res , next ) {
107
+ req
108
+ . sanitizeBody ( "email" )
109
+ . normalizeEmail ( {
110
+ all_lowercase : true ,
92
111
} )
93
- . catch ( ( error ) => {
94
- console . log ( `Error logging in user: ${ error . message } ` ) ;
95
- next ( error ) ;
96
- } ) ;
112
+ . trim ( ) ;
113
+ req . check ( "email" , "Email is invalid" ) . isEmail ( ) ;
114
+ // Check for validation errors
115
+
116
+ var errors = req . validationErrors ( ) ;
117
+ if ( errors ) return res . status ( 400 ) . send ( errors ) ;
118
+
119
+ User . findOne ( { email : req . body . email } , function ( err , user ) {
120
+ if ( ! user ) return res . status ( 400 ) . send ( { msg : 'We were unable to find a user with that email.' } ) ;
121
+ if ( user . isVerified ) return res . status ( 400 ) . send ( { msg : 'This account has already been verified. Please log in.' } ) ;
122
+
123
+ // Create a verification token, save it, and send email
124
+ var token = new Token ( { _userId : user . _id , token : crypto . randomBytes ( 16 ) . toString ( 'hex' ) } ) ;
125
+
126
+ // Save the token
127
+ token . save ( function ( err ) {
128
+ if ( err ) { return res . status ( 500 ) . send ( { msg : err . message } ) ; }
129
+
130
+ // Send the email
131
+ var transporter = nodemailer . createTransport ( {
132
+ service : "SendGrid" ,
133
+ auth : {
134
+ user : "apikey" ,
135
+ pass :
136
+ "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" ,
137
+ } ,
138
+ } ) ;
139
+ var mailOptions = {
140
+ from : "priyankrastogi14@gmail.com" ,
141
+ to : user . email ,
142
+ subject : "Account Verification Token" ,
143
+ // text:
144
+ // "Hello,\n\n" +
145
+ // "Please verify your account by clicking the link \n",
146
+ html : `<p>Please verify your Smartier account by clicking the link below</p><a href=http:\/\/${ req . headers . host } \/confirmation\/${ token . token } >Link</a>` ,
147
+ } ;
148
+ transporter . sendMail ( mailOptions , function ( err ) {
149
+ if ( err ) { return res . status ( 500 ) . send ( { msg : err . message } ) ; }
150
+ return res . status ( 200 ) . send ( 'A verification email has been sent to ' + user . email + '.' ) ;
151
+ } ) ;
152
+ } ) ;
153
+
154
+ } ) ;
155
+ } ,
156
+ login : ( req , res ) => {
157
+ res . render ( "user/login" ) ;
97
158
} ,
159
+ authenticate : passport . authenticate ( "local" , {
160
+ failureRedirect : "/login" ,
161
+ failureFlash : "Failed to login." ,
162
+ successRedirect : "/feed" ,
163
+ successFlash : "Logged in!" ,
164
+ } ) ,
98
165
validate : ( req , res , next ) => {
99
166
req
100
167
. sanitizeBody ( "email" )
@@ -107,7 +174,6 @@ module.exports = {
107
174
req . check ( "first" , "this cannot be empty" ) . notEmpty ( ) ;
108
175
req . check ( "last" , "this cannot be empty" ) . notEmpty ( ) ;
109
176
110
-
111
177
req . getValidationResult ( ) . then ( ( error ) => {
112
178
if ( ! error . isEmpty ( ) ) {
113
179
let messages = error . array ( ) . map ( ( e ) => e . msg ) ;
@@ -120,4 +186,44 @@ module.exports = {
120
186
}
121
187
} ) ;
122
188
} ,
189
+ /**
190
+ * POST /confirmation
191
+ */
192
+ confirmationPost : function ( req , res , next ) {
193
+ console . log ( req . params . token ) ;
194
+ Token . findOne ( { token : req . params . token } , function ( err , token ) {
195
+ if ( ! token )
196
+ return res
197
+ . status ( 400 )
198
+ . send ( {
199
+ type : "not-verified" ,
200
+ msg :
201
+ "We were unable to find a valid token. Your token may have expired." ,
202
+ } ) ;
203
+
204
+ // If we found a token, find a matching user
205
+ User . findOne ( { _id : token . _userId } , function ( err , user ) {
206
+ if ( ! user )
207
+ return res
208
+ . status ( 400 )
209
+ . send ( { msg : "We were unable to find a user for this token." } ) ;
210
+ if ( user . isVerified )
211
+ return res
212
+ . status ( 400 )
213
+ . send ( {
214
+ type : "already-verified" ,
215
+ msg : "This user has already been verified." ,
216
+ } ) ;
217
+
218
+ // Verify and save the user
219
+ user . isVerified = true ;
220
+ user . save ( function ( err ) {
221
+ if ( err ) {
222
+ return res . status ( 500 ) . send ( { msg : err . message } ) ;
223
+ }
224
+ res . status ( 200 ) . send ( "The account has been verified. Please log in." ) ;
225
+ } ) ;
226
+ } ) ;
227
+ } ) ;
228
+ } ,
123
229
} ;
0 commit comments