Simple Email verification in javscript and nodejs.
learn to build simple email verification module for you awesome web application in javascript using nodejs and nodemailer.
so you want to integrate email authentication in your awesome website or application. well, you've come to right place.
Tools that we are going to use today
-Nodejs
-nodemailer
if you are not using nodejs then this is right time to use it.
-it is highly efficient and highly scalable.
let's start by installing nodemailer in our node modules.
npm intstall nodemailer
Nodemailer is a module for Node.js applications to allow easy as cake email sending.
read more about nodemailer here👇: nodemailer.com/about
it is a excellent tool that i have been using for quite some time in my production level applications.
let's write some code
first of all lets import previously installed node mailer in our javascript file.
var nodemailer = require('nodemailer');
i am using email_verification.js as file name you can use whatever you like.
lets write a email verification function first which accepts email as argument.
async function email_verification(email){
}
we are using async function because we need to return promise this is necessary as when we will use this as middleware in our main app it needs to return something.
if you don't know async function, check out this article👇:
if you want article about async function in javascript comment down.
getting back to our function now we need provide internal ingredients for this function to work.
first let's create a transport which is nothing more but a document containing all credentials required for sending a email from a email account like email id, password.
it is very important as for production level application we need to provide oauth tokens and secret tokens of our email providing service it may be Gmail, yahoo, or self hosted.
but we are going with simple email and password for sake of simplicity.
so lets create our transport method. we are using gmail as our transport service you any service you want.
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "your email",
pass:"your email password"
}});
Note:never user email and password or any credential directly into code use .env file or config file instead
if you are wondering how actual production level transporter looks like. here it is.
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: "",
pass:"",
clientId: "",
clientSecret: "",
refreshToken: ""
}});
now its time to provide mail option which is simply the mail you want to send.
var mailOptions = {
from: 'your or your website email',
to: email,
subject: 'OTP for email authentication',
text: "some text"
};
since we want to send OTP(one time password) to our user lets generate simple 4 digit otp first.
var email_auth_otp = Math.floor(1000 + Math.random() * 9000);
i am using simple random function to generate email verification otp you can use whatever complex methods cooking in your brilliant mind.
so when user request to verify email we will email this otp to our requested user so they can verify that their email exist by entering the otp.
now add the generated otp to email.
var mailOptions = {
from: 'navdeepdhkr@gmail.com',
to: email,
subject: 'Sending Email using Node.js',
text: email_auth_otp.toString(),
};
our verification email is ready now we need to send it to user.
we send email by using sendMail() method.
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
return email_auth_otp
}
we have also added some console logs so we will know if something went wrong.
congratulation🎊🎉, you completed the hard part now just export it.
module.exports = email_verification;
COMPLETE CODE:
var nodemailer = require('nodemailer');
async function email_verification(email){
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "your email",
pass:"email password",
}});
var email_auth_otp = Math.floor(1000 + Math.random() * 9000);
console.log(email_auth_otp);
var mailOptions = {
from: 'your email',
to: email,
subject: 'Sending Email using Node.js',
text: email_auth_otp.toString(),
};
console.log("completed up to here");
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
return email_auth_otp
}
module.exports = email_verification;
🎉🎉you have successfully created your first email verification module, now your email verification module is ready to use.
"happy coding"
if you want production level email verification article comment down👇:
comment down any question.
reach me out on twitter: navdeep dhakar