Email verification is a technique in node.js which is used to ensure whether the provided email corresponds to an existing user or not because otherwise the database will become filled up with unnecessary users.
The process goes like that we send some kind of token to email and the user sends get request along with that token, now if the get request consists of the exact token we earlier sent from the server then email is considered as verified.

Prerequisite
Steps to Implement Email Verification
Step 1: Create a node project & Initialize code with installation
npm init // To initialize the package
npm install jsonwebtoken // To generate token
npm install express // To create server
npm install nodemailer // To send email
This would be our folder structure after installation and creating two empty files namely app.js and tokenSender.js

The Updated Dependencies in the package.json file.
"dependencies": {
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^6.9.13",
"razorpay": "^2.9.4"
}Step 2: Create a route to generate tokens and send an email
After installation, we can proceed to generate a token with jwt and send an email with nodemailer here we are using Gmail as a service for simplification but the fundamental concepts remain the same.
Explanation:
In the first line we have imported the nodemailer package which will later be used for sending mail, and then we have created a transporter object which is used to send mail and it contains some configurations about the email service and sender. And then we have created another object which will contain information about mail like the receiver, sender, text of the email, etc.
In the end, the sendMail method of the transporter object simply sends the mail to the given address.
Node
// Filename - tokenSender.js
const nodemailer = require('nodemailer');
const jwt = require('jsonwebtoken');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: secure_configuration.EMAIL_USERNAME,
pass: secure_configuration.PASSWORD
}
});
const token = jwt.sign({
data: 'Token Data' .
}, 'ourSecretKey', { expiresIn: '10m' }
);
const mailConfigurations = {
// It should be a string of sender/server email
from: 'mrtwinklesharma@gmail.com',
to: 'smtwinkle451@gmail.com',
// Subject of Email
subject: 'Email Verification',
// This would be the text of email body
text: `Hi! There, You have recently visited
our website and entered your email.
Please follow the given link to verify your email
http://localhost:3000/verify/${token}
Thanks`
};
transporter.sendMail(mailConfigurations, function(error, info){
if (error) throw Error(error);
console.log('Email Sent Successfully');
console.log(info);
});
Output:We are executing the file with node command and as expected our email is being sent successfully.

Step 3: Create a route to receive get request along with the token and verify email
After we have sent the token to the user the next step is to set up a route on the server, and the user will send the get request with the token in the parameter because the browser can send only data with URL.
Explanation:
Here we have created a simple express server and there exists a get route. Inside that, we are extracting the information i.e. token from URL, and then we are verifying it with jwt like whether the token being sent is exactly the one which was generated from our server or not.
Finally, the route is sending the appropriate response according to the verification of the token.
Node
// Filename app.js
const express = require('express');
const jwt = require('jsonwebtoken');
const app = express();
const PORT = 3000;
app.get('/verify/:token', (req, res)=>{
const {token} = req.params;
// Verifying the JWT token
jwt.verify(token, 'ourSecretKey', function(err, decoded) {
if (err) {
console.log(err);
res.send("Email verification failed,
possibly the link is invalid or expired");
}
else {
res.send("Email verifified successfully");
}
});
});
app.listen(PORT, (error) =>{
if(!error)
console.log("Server is Successfully Running,
and App is listening on port "+ PORT)
else
console.log("Error occurred, server can't start", error);
}
);
Output: Run the server with node command.

In the inbox, we will have our email sent by the server. After this, we are making a get request on the server by clicking on the link given and as expected the server will respond as email verified. Later we are changing the URL a little bit to make the URL invalid and correspondingly server is responding that email verification failed because of the invalid token.
Conclusion
This was the entire code and workflow of verifying email in nodejs. You can check out linked geeksforgeeks articles in the prerequisite section, to know more about these libraries. In some other examples the terms, libraries, techniques, design patterns may change a little bit but the general concept will remain the same.
Similar Reads
Spring Security â Resend Verification Email
When building web applications, ensuring that users verify their email addresses is important to confirm their identity and secure access. In scenarios where the user does not receive the initial verification email or the verification link has expired. It becomes necessary to provide the option to r
15+ min read
E-Mails Notification Bot With Python
Email continues to be a widely used communication method, making it an effective platform for receiving updates and staying connected. However, manual email notifications for recurring events or tasks can be inefficient and time-consuming. Python offers a robust toolset to develop automated email no
3 min read
Email Protocols
Email protocols are a collection of protocols that are used to send and receive emails properly. The email protocols provide the ability for the client to transmit the mail to or from the intended mail server. Email protocols are a set of commands for sharing mails between two computers. Email proto
3 min read
Email OTP Verification using PHP in Live Server
The task is to create and design a sign-up and login form. In the sign-up form, the user will sign-up with a custom username and password and a valid email then the user will receive an OTP through the email, and after successful verification of OTP user account will be created and data will be stor
7 min read
What is Email Virus?
Virus stands for Vital Information Resources under Siege. Virus is defined as a malicious program that spreads over the system. The virus needs an initiating host such as a pendrive. Once the host is connected to the system it gets spread over. It affects the system and gets all the confidential inf
4 min read
What is an Email?
Email stands for Electronic Mail. It is a method to send messages from one computer to another computer through the Internet. It is mostly used in business, education, technical communication, and document interactions. It allows communicating with people all over the world without bothering them. I
6 min read
Working of Email
The email refers to the electronic means of communication of sending and receiving messages over the Internet. Email is the most common form of communication nowadays. An email has significantly evolved over the past couple of years. There are now stronger sync and messaging features along with stro
4 min read
What is Secure Email?
E-Mail is one of the most efficient methods of communication in the modern world, taking into account modern trends and the increase in the use of the internet. However, the globalization of e-mail or electronic mail as is commonly referred to, comes with its merits, but also have numerous security
8 min read
SMTP Enumeration
SMTP (Simple Mail Transfer Protocol) is a set of communication guidelines that allow web applications to perform communication tasks over the internet, including emails. It is a part of the TCP/IP protocol and works on moving emails across the network. SMTP enumeration allows us to identify valid us
2 min read
Email Account Merging
Given a 2D array arr[][], where each element arr[i] is an array of strings representing an account, where the first element arr[i][0] is a name, and the rest of the elements are emails associated with this name. Also, two accounts belong to the same person if there is a common email associated with
15+ min read