In today's tutorial, I am going to show you how implement OTP (one-time-password) verification authentication using nodemailer module in node.js and express.
Prerequisites
Node.js
To Continue with this tutorial, you should have installed Node.js in your pc. If you are still not installed Node.js in your machine you can download it from here.
Initialize project
So, to begin with we need initialize the project by running below command
1
npm init -y
On passing this your app will be initialized with package.json file.
Install required dependencies
Now we need install all the dependencies and packages which need to development of this project.
app.get('/',function(req,res){ res.send("Node.js 2 way authnetication"); });
//defining port const PORT=process.env.PORT||3000; app.listen(PORT,()=>{ console.log(`app is live at ${PORT}`); })
Now we have finished with the coding part of basic server. We can run our application by running below command in your terminal
1
nodemon app.js
Construct static and templating files
Now our app is working so before developing the routes for sending otp, we need to construct our static and templating files, so first we will code the contact.handlebars which will only contain a basic html form which is to be filled by the user.
Create a folder called views in your main directory and create a file called contact.handlebars in it. Then just paste the below code in it.
In this transporter function, it contains info about the host and port no. of your email services. I am using gmail here. If you use any other mail sender service you can specify it’s configuration here.
you need to allow permission for non-secure apps in your gmail setting for nodemailer to use your gmail account. you can give permission from here.
app.post('/send', function (req, res) { email = req.body.email;
// send mail with defined transport object var mailOptions = { to: req.body.email, subject: "Otp for registration is: ", html: "<h3>OTP for account verification is </h3>" + "<h1 style='font-weight:bold;'>" + otp + "</h1>" // html body };
Next route is for resend otp. Paste below code under the /send route:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
app.post('/resend', function (req, res) { var mailOptions = { to: email, subject: "Otp for registration is: ", html: "<h3>OTP for account verification is </h3>" + "<h1 style='font-weight:bold;'>" + otp + "</h1>" // html body };
transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info)); res.render('otp', { msg: "otp has been sent" }); });
});
Now we need to define our last route /verify. Paste below code in between /send and /resend routes:
1 2 3 4 5 6 7 8 9
app.post('/verify', function (req, res) {
if (req.body.otp == otp) { res.send("You has been successfully registered"); } else { res.render('otp', { msg: 'otp is incorrect' }); } });
Now out app.js file is completed and it’s looked like this.
app.post('/send', function (req, res) { email = req.body.email;
// send mail with defined transport object var mailOptions = { to: req.body.email, subject: "Otp for registration is: ", html: "<h3>OTP for account verification is </h3>" + "<h1 style='font-weight:bold;'>" + otp + "</h1>" // html body };
if (req.body.otp == otp) { res.send("You has been successfully registered"); } else { res.render('otp', { msg: 'otp is incorrect' }); } });
app.post('/resend', function (req, res) { var mailOptions = { to: email, subject: "Otp for registration is: ", html: "<h3>OTP for account verification is </h3>" + "<h1 style='font-weight:bold;'>" + otp + "</h1>" // html body };
transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info)); res.render('otp', { msg: "otp has been sent" }); });
});
//defining port const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`app is live at ${PORT}`); })
Conclusion
In this tutorial, we obtain how can we implement OTP (one-time-password) verification authentication using nodemailer module in node.js and express.. You can obtain complete source code for this tutorial from this GitHub repository.