diff options
| -rw-r--r-- | routes/user.js | 142 | ||||
| -rw-r--r-- | views/password_edit.hbs | 20 | ||||
| -rw-r--r-- | views/reset_password.hbs | 18 |
3 files changed, 114 insertions, 66 deletions
diff --git a/routes/user.js b/routes/user.js index 9b5f0fb..849ebb6 100644 --- a/routes/user.js +++ b/routes/user.js @@ -222,55 +222,65 @@ router.get('/resetPassword', loggedOut, function(req, res, next) { router.post('/sendReset', loggedOut, function(req, res, next){ - dbFile.find_user(req.body.email, function(found){ - if (!found) { - res.render('reset_password', {errors: "This email does not exist in our database."}); - } else { - var token = crypto.randomBytes(32).toString('hex'); - var userToken = crypto.randomBytes(8).toString('hex'); + req.check('email', 'Enter a valid Email address').notEmpty().withMessage('Email required').isEmail(); - dbFile.addToken(req.body.email, userToken, token, function(error, message){ - if (error) { - res.render('reset_password', {errors: "An error has occurred. Please try again."}); - } else { - var transporter = nodemailer.createTransport({ - service: "Gmail", - auth: { - user: 'solutionsrepo0@gmail.com', - pass: 'lamptable' - } - }); + var errors = req.validationErrors(); + if (errors) { + req.session.errors = errors; + req.session.success = false; + res.redirect('/user/resetPassword'); + } else { + dbFile.find_user(req.body.email, function(found){ + if (!found) { + res.render('reset_password', {error: "This email does not exist in our database."}); + } else { + var token = crypto.randomBytes(32).toString('hex'); + var userToken = crypto.randomBytes(8).toString('hex'); + + dbFile.addToken(req.body.email, userToken, token, function(error, message){ + if (error) { + res.render('reset_password', {error: "An error has occurred. Please try again."}); + } else { + var transporter = nodemailer.createTransport({ + service: "Gmail", + auth: { + user: 'solutionsrepo0@gmail.com', + pass: 'lamptable' + } + }); + + var mailOptions = { + from: "Solutions.Repo < solutionsrepo0@gmail.com >", + to: req.body.email, + subject: 'Password reset on Solutions.Repo', + text: "Hello!\n" + + "You are receiving this email because you requested a password reset for your account at Solutions.Repo.\n" + + "In order to reset your password, please visit the following link:\n" + + resetLink + userToken + '/' + token + '\n' + + 'Thanks for using Solutions.Repo! The Solutions.Repo Team.', + html: "<p>Hello!</p>" + + "<p>You are receiving this email because you requested a password reset for your account at Solutions.Repo.</p>" + + "<p>In order to reset your password, please visit the following link:</p>" + + "<a href='"+ resetLink + userToken + '/' + token + "'>Click here!</a>" + + "<p>Thanks for using Solutions.Repo! The Solutions.Repo Team.</p>" + }; - var mailOptions = { - from: "Solutions.Repo < solutionsrepo0@gmail.com >", - to: req.body.email, - subject: 'Password reset on Solutions.Repo', - text: "Hello!\n" + - "You are receiving this email because you requested a password reset for your account at Solutions.Repo.\n" + - "In order to reset your password, please visit the following link:\n" + - resetLink + userToken + '/' + token + '\n' + - 'Thanks for using Solutions.Repo! The Solutions.Repo Team.', - html: "<p>Hello!</p>" + - "<p>You are receiving this email because you requested a password reset for your account at Solutions.Repo.</p>" + - "<p>In order to reset your password, please visit the following link:</p>" + - "<a href='"+ resetLink + userToken + '/' + token + "'>Click here!</a>" + - "<p>Thanks for using Solutions.Repo! The Solutions.Repo Team.</p>" - }; - - transporter.sendMail(mailOptions, function(error, info) { - if (error) { - console.log(error); - res.render('reset_password', {errors: "An error has occurred. Please try again."}); - } else { - console.log("Message Sent"); - res.render('reset_password', {success: "We've e-mailed you instructions for resetting your password." + - " You should be receiving the email shortly. Please check your inbox."}); - } - }); - } - }); - } - }); + transporter.sendMail(mailOptions, function(error, info) { + if (error) { + console.log(error); + res.render('reset_password', {error: "An error has occurred. Please try again."}); + } else { + console.log("Message Sent"); + res.render('reset_password', {success: "We've e-mailed you instructions for resetting your password." + + " You should be receiving the email shortly. Please check your inbox."}); + } + }); + } + }); + } + }); + + } }); @@ -283,13 +293,13 @@ router.get('/password/reset/:emailHash/:token', loggedOut, function(req, res, ne res.redirect('/'); } else { if (data[0].userToken === emailHash) { - dbFile.removeToken(token, function(err, message) { + /*dbFile.removeToken(token, function(err, message) { if (err) console.log('could not remove token from db'); else { console.log('token removed'); } - }); + });*/ res.render('password_edit', {email: data[0].email, csrfToken: req.csrfToken()}); } else { @@ -300,16 +310,30 @@ router.get('/password/reset/:emailHash/:token', loggedOut, function(req, res, ne }); router.post('/password/change', loggedOut, function(req, res, next) { - var email = req.body._email.valueOf(); - var password = req.body.password; - var hashed = pass.encryptPassword(password); - dbFile.updatePassword(email, hashed, function(err, message) { - if (err) { - res.render('password_edit', {errors: "Could not change password!"}); - } else { - res.render('password_edit', {success: "Password changed."}); - } - }); + req.check('password', "Password should be between 6 and 12 characters.") + .notEmpty().withMessage('Password required.').isLength({min: 6, max: 12}); + req.check('password', "The confirmation password doesn't match.").equals(req.body.confirmPassword); + + var errors = req.validationErrors(); + if (errors) { + req.session.errors = errors; + req.session.success = false; + /*res.redirect('/user/resetPassword');*/ + res.render('password_edit', { + errors : errors + }); + } else { + var email = req.body._email.valueOf(); + var password = req.body.password; + var hashed = pass.encryptPassword(password); + dbFile.updatePassword(email, hashed, function(err, message) { + if (err) { + res.render('password_edit', {error: "Could not change password!"}); + } else { + res.render('password_edit', {success: "Password changed."}); + } + }); + } }); /** Render/GET signin page. */ diff --git a/views/password_edit.hbs b/views/password_edit.hbs index 903388a..f5e87ab 100644 --- a/views/password_edit.hbs +++ b/views/password_edit.hbs @@ -5,12 +5,24 @@ {{# if success }} <section class="alert alert-success"> <!-- Change those to banners later --> - <h4>{{success}}</h4> + <p>{{success}}</p> </section> {{ else }} {{# if errors }} + {{# if errors.length}} + <section class="alert alert-danger"> + <ul> + {{# each errors }} + <li>{{ this.msg }}</li> + {{/each}} + </ul> + </section> + {{/if}} + + {{/if}} + {{# if error}} <section class="alert alert-danger"> - <h4>{{errors}}</h4> + <p>{{error}}</p> </section> {{/if}} {{/if}} @@ -27,13 +39,13 @@ <div class="input-group-addon addon-username"> <i class="fa fa-lock" aria-hidden="true"></i> </div> - <input type="text" class="form-control input-username" placeholder="password" name="password" required> + <input type="password" class="form-control input-username" placeholder="password" name="password"> </div> <div class="input-group input-group-lg"> <div class="input-group-addon addon-password"> <i class="fa fa-unlock-alt" aria-hidden="true"></i> </div> - <input type="password" class="form-control input-lg input-password" placeholder="password" name="passwordConfirm" required> + <input type="password" class="form-control input-lg input-password" placeholder="confirm password" name="confirmPassword"> </div> </div> <input type="hidden" name="_email" value="{{ email}}"> diff --git a/views/reset_password.hbs b/views/reset_password.hbs index 5526123..9403b04 100644 --- a/views/reset_password.hbs +++ b/views/reset_password.hbs @@ -5,12 +5,24 @@ {{# if success }} <section class="alert alert-success"> <!-- Change those to banners later --> - <h4>{{success}}</h4> + <p>{{success}}</p> </section> {{ else }} {{# if errors }} + {{# if errors.length}} + <section class="alert alert-danger"> + <ul> + {{# each errors }} + <li>{{ this.msg }}</li> + {{/each}} + </ul> + </section> + {{/if}} + + {{/if}} + {{# if error}} <section class="alert alert-danger"> - <h4>{{errors}}</h4> + <p>{{error}}</p> </section> {{/if}} {{/if}} @@ -27,7 +39,7 @@ <div class="input-group-addon addon-email-reset"> <i class="fa fa-at" aria-hidden="true"></i> </div> - <input type="text" class="form-control input-email-reset" placeholder="email" name="email" required> + <input type="text" class="form-control input-email-reset" placeholder="email" name="email"> </div> </div> <input type="hidden" name="_csrf" value="{{ csrfToken }}"> |
