Implementing OTP Verification with Fast2SMS using Node.js and Express
In today’s digital age, security is a crucial aspect of any application that deals with user data. One of the most common security features used in modern applications is two-factor authentication (2FA), where a user needs to provide two forms of identification to access their account. One of the most popular methods of 2FA is one-time password (OTP) verification, which involves generating a unique password that can only be used once.
In this blog, we will discuss how to implement OTP verification using Fast2SMS with Node.js and Express.
What is Fast2SMS?
Fast2SMS is a popular SMS gateway that allows you to send SMS messages to mobile devices across India. It has a simple API that you can use to send messages to any mobile number. Fast2SMS provides an affordable and reliable way to send OTPs to users, making it an excellent choice for OTP verification.
Implementation
Step 1: Create a Fast2SMS account
To use Fast2SMS, you need to create an account on their website. Once you have created an account, you will be given an API key that you can use to send messages.
- Go to the Fast2SMS website at https://www.fast2sms.com/.
- Click on the “Signup” button on the top right corner of the page.
- Fill in the required details in the registration form, including your name, email address, mobile number, and a password.
- Click on the “Signup” button to create your account.
- Once you have created your account, you will receive an email with a verification link. Click on the link to verify your email address.
- After verifying your email address, log in to your Fast2SMS account using your email address and password.
- On the dashboard, you will see your API key, which you can use to send SMS messages using the Fast2SMS API.
Step 2: Set up your Node.js environment
Before you can start working with Fast2SMS, you need to set up your Node.js environment. To do this, you need to install Node.js and NPM (Node Package Manager) on your computer. You can download Node.js from the official Node.js website.
npm init
then install the required modules
npm i express
npm i fast-two-sms // for sending otp
npm i otplib // for generating random otp
Step 4: Create an OTP generator functionStep and Create an endpoint to send the OTP
we are using otplib module to generate random otp because this module also come with some additional features like we can set a time to expire otp like that.
In this step, we will create an endpoint that sends the OTP to the user’s mobile number. The endpoint will take the user’s mobile number as input, generate an OTP, and send the OTP to the user’s mobile number using Fast2SMS.
const fast2sms = require("fast-two-sms");
const otplib = require('otplib');
const secret = otplib.authenticator.generateSecret();
// Generate an OTP
const token = otplib.authenticator.generate(secret);
const sendMessage = function (mobile, res, next) {
var options = {
authorization : YOUR_API_KEY, //fill this with your api
message: `your OTP verification code is ${token}`,
numbers: [mobile],
};
//send this message
fast2sms
.sendMessage(options)
.then((response) => {
console.log("otp sent successfully");
})
.catch((error) => {
console.log(error);
});
return token;
};
Step 5 : Verifying the otp
const express = require("express")
const app = express()
let newOtp; //for saving the sms generated
app.get('/',(req,res)=>{
newOtp = sendMessage(mobile,res ) // pass the mobile
res.render("otp") //for rendering otp page
})
app.post('/',(req,res)=>{
const otp = req.body.otp //otp that you entered in the page
if(newOtp == otp){ //checking the otp's
res.send("Verified") // if the otp matches sending data
}else{
res.render("otp",{message:"you have enterd the wrong otp"}) //if the otp doesnt match sending message
}
})