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:
- 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.
Folder Structure:
Code Template:
const { Auth } = require( "two-step-auth" );
async function login(emailId) {
const res = await Auth(emailId);
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" );
|
- 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:
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);
}
}
LoginCredentials.mailID = "yourmailId@anydomain.com" ;
LoginCredentials.password = "Your password" ;
LoginCredentials.use = true ;
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
Last Updated :
19 Feb, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...