Introduction
Before starting the code you need an account in send grid which will send the notification to your mail. Generally we use email notifications for forget passwords in your applications. You can follow the below steps to get the task done and if you have any queries please leave a comment below.
Step 1
We require a few modules from npm to send the notification through the mail.
We require a few modules from npm to send the notification through the mail.
- npm install formidable
- npm install crypto
- npm install async
- npm install nodemailer
Step 2
In my router.js file the following code will be present
In my router.js file the following code will be present
- app.route('/forgotpasswordResponse')
- .post(userCtrl.forgotpasswordResponse);
Step 3
In forgotpasswordResponse my code is somthing like this,
In forgotpasswordResponse my code is somthing like this,
- exports.forgotpasswordResponse = function(req, res, next) {
- var input=req.body;
- //console.log(input);
- async.waterfall([
- function(done) {
- crypto.randomBytes(20, function(err, buf) {
- var token = buf.toString('hex');
- done(err, token);
- });
- },
- function(token, done) {
- MongoClient.connect(url, function(err, db){
- var dbo = db.db("Here is your DB Name");
- //console.log(req.body.Email);
- var query = { Email : req.body.Email };
- dbo.collection('CLC_User').find(query).toArray(function(err,result){
- if(result.length == 0){
- req.flash('error', 'No account with that email address exists.');
- }
- var myquery = { Email: result[0].Email };
- var newvalues = { $set: {resetPasswordToken: token, resetPasswordExpires: Date.now() + 3600000 }};
- dbo.collection("CLC_User").updateOne(myquery, newvalues, function(err, res) {
- if (err) throw err;
- console.log("1 document updated");
- });
- // console.log(result[0].Email);
- done(err, token, result);
- });
- });
- },
- function(token, result, done,Username,password) {
- var emailVal = result[0].Email;
- console.log(emailVal);
- var Username="";
- var password="";
- MongoClient.connect(url, function(err, db){
- var dbo = db.db("Here willbe your db name");
- dbo.collection('Accountsettings').find().toArray(function(err,result){
- if (err) throw err;
- Username=result[0].UserName;
- password=result[0].Password;
- // console.log(Username);
- // console.log(password);
- // res.json({status : 'success', message : 'Records found', result : result});
- // console.log(Username);
- var smtpTransport = nodemailer.createTransport({
- service: 'SendGrid',
- auth: {
- user: Username,
- pass: password
- }
- });
- const mailOptions = {
- to: emailVal,
- from: '[email protected]',
- subject: 'Node.js Password Reset',
- text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
- 'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
- 'http://' + req.headers.host + '/reset/' + token + '\n\n' +
- 'If you did not request this, please ignore this email and your password will remain unchanged.\n'
- };
- smtpTransport.sendMail(mailOptions, function(err) {
- console.log("HI:"+emailVal);
- res.json({status : 'success', message : 'An e-mail has been sent to ' + emailVal + ' with further instructions.'});
- done(err, 'done');
- });
- })
- });
- }
- ], function(err) {
- if (err) return next(err);
- });
- }
In my case I am using waterfall methologie for this method with will execute acyn in method,
In the above code initially I am updating the collection with resetPasswordToken and resetPasswordExpires using email id and getting my send grid credentials from db form Accountsettings collections. If you can observe in mailOptions text "req.headers.host" will be the link which will get in you mail with token.
In the above code initially I am updating the collection with resetPasswordToken and resetPasswordExpires using email id and getting my send grid credentials from db form Accountsettings collections. If you can observe in mailOptions text "req.headers.host" will be the link which will get in you mail with token.
Step 4
When you click on Url which you got in the email it will redirect you to another page to set the password.
Again we need to go to route.js and the code will be some thing like this. It will take to html page which we can reset the password,
Again we need to go to route.js and the code will be some thing like this. It will take to html page which we can reset the password,
- app.route('/reset/:token')
- .get(Resetpassword.resetpasswordResponse);

ashok kumarPosted Sep 11, 2018, 2:02 AM
What does it mean using req.headers.host instead of localhost:4000?? and please can u give a clue that how to verify jwt token using link that we send on email with token to redirect on reset-password UI like context: { url: 'http://192.168.1.141:3000/reset_password?token=' +token, here I'm facing some issues with unauthorized access when I click on link please help to solve this problem