I want to send otp via SMS and email to the user who is signing-up to the web app to verify their email and phone no. Is there any NPM package for sending and verifying OTP or should I write my own code?
I want to send otp via SMS and email to the user who is signing-up to the web app to verify their email and phone no. Is there any NPM package for sending and verifying OTP or should I write my own code?
Yes, there is a package called sendotp. This is used for sending OTP over SMS only.
In npmjs you can find different examples to achieve your task.
But my suggestion is to implement your own methodology for sending and verifying OTP is better. Because by using packages you might get extra methods that you don't want at all or there is no functionality implemented that you want.
You can use Nodemailer for email verification and twiolio for sms verification. Create a random string and send it to user via sms or email and if using mongodb you can check when otp is created and from that you can check how much time is passed after its sent but by that you can set expirey.
You can use the msg91 for sending the otp to users.
Login to msg91 and get your template_id and api_key.
using msg91 api with the axios.
const axios = require('axios');
async function sendOtpTOUser(phone) {
const template = "template _id";
const apiKey = "api_key";
const sendotp = "https://api.msg91.com/api/v5/otp?template_id="+template+"&mobile="+phone+"&authkey="+apiKey;
let request_options1 = {
method: 'get',
url: sendotp
};
let otpResponse = await axios(request_options1);
console.log(otpResponse.data)
return otpResponse.data;
}
it will return the object as
{ request_id: '3166686e7867313634383535', type: 'success' }