Open In App

Email Verification using OTP in NodeJS

This Article speaks about setting up your node.js server for verifying emails via OTP.

Project setup: The name of the package is two-step-auth



Installation: 

npm i --save two-step-auth

Default Usage:



Folder Structure:

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("verificationEmail@anyDomain.com");

Example:




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 = "yourmailId@anydomain.com"
  
// 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("verificationEmail@anyDomain.com"); 

We have created an OTP verification service.

Output

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

Image as seen on mail


Article Tags :