Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
1.0 kB
3
Indexable
Never
const msg91 = require("msg91").default;
const { response } = require("./response");

const Msg91 = async (res, next, phoneNumber, type, verifyOtp = null) => {
  try {
    msg91.initialize({ authKey: process.env.MSG91_AUTH_KEY });
    const otp = msg91.getOTP(process.env.MSG91_TEMPLATE_ID, { length:  6 });

    switch (type) {
      case "send":
        const sendResult = await otp.send(phoneNumber);
        return response(res,  200, true, sendResult, "OTP Sent Successfully");
      
      case "retry":
        const retryResult = await otp.retry(phoneNumber);
        return response(res,  200, true, retryResult, "New OTP sent successfully");
      
      case "verify":
        const verifyResult = await otp.verify(phoneNumber, verifyOtp);
        return response(res,  200, true, verifyResult, "OTP Verified successfully");
      
      default:
        throw new Error("Invalid Action. Only send, retry or verify otp is allowed");
    }
  } catch (error) {
    next(error);
  }
};

module.exports = Msg91;
Leave a Comment