Open In App

Email Verification using OTP in NodeJS

Last Updated : 24 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Implementing email verification using OTP in Node.js enhances security by ensuring users provide valid email addresses. It involves generating and sending OTPs to users, verifying them, and confirming their email authenticity.

Approach

To enable Email Verification using OTP in NodeJS we will be using two-step-auth package. It involves installing and configuring the two-step-auth package, generating and sending OTPs to user emails, and verifying the OTPs for secure authentication.

two-step-auth is a Node.js package for email-based two-factor authentication. It generates and verifies OTPs, enhancing security for user authentication in applications.

Default Usage:

  • Kindly Provide a Company Name so the mail will be treated as important. (This is optional)
  • Import the Auth object from the package and use them as mentioned below.

Steps to Enable Email Verification using OTP

Step 1: Initialize the Node Project

npm init

Step 2: Instal required modules

npm i --save two-step-auth

Folder Structure:

Updated dependencies in package.json file

"dependencies": {
"two-step-auth": "^1.1.2"
}

Code Template:

const { Auth } = require("two-step-auth");

async function login(emailId) {
const res = await Auth(emailId);
// You can follow this approach,
// but the second approach is suggested,
// as the mails will be treated as important
const res = await Auth(emailId, "Company Name");
console.log(res);
console.log(res.mail);
console.log(res.OTP);
console.log(res.success);
}

login("[email protected]");
  • Once if the operation is success, we will have the OTP in hand, and an Email will be sent to the particular user’s mail ID
  • Custom Email ID usage :
    • Pull the LoginCredentials object from the package and use them as mentioned below
    • Prerequisites for using custom Email ID : 
      • Make sure you have enabled the allow less secure apps for that particular account before executing the function.
      • Turn them Off when not in use.

Example:

Node
// index.js

const { Auth, LoginCredentials } = require("two-step-auth");

async function login(emailId) {
  try {
    const res = await Auth(emailId, "Company Name");
    console.log(res);
    console.log(res.mail);
    console.log(res.OTP);
    console.log(res.success);
  } catch (error) {
    console.log(error);
  }
}

// This should have less secure apps enabled
LoginCredentials.mailID = "[email protected]"; 

// You can store them in your env variables and
// access them, it will work fine
LoginCredentials.password = "Your password"; 
LoginCredentials.use = true;

// Pass in the mail ID you need to verify
login("[email protected]"); 

We have created an OTP verification service.

Steps to Run: Use the command given below to run the application.

npm start

Output:

Email Sample: This will received in your given mail Id.

Image as seen on mail



Next Article

Similar Reads