Untitled
unknown
plain_text
2 years ago
1.1 kB
10
Indexable
const twilio = require('twilio');
async function sendOrVerifyOTP(phoneNumber, type, otpCode = null) {
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);
try {
switch (type) {
case 'send':
const verification = await client.verify.services(process.env.TWILIO_SERVICE_ID)
.verifications
.create({ to: phoneNumber, channel: 'sms' });
return verification;
case 'verify':
if (!otpCode) {
throw new Error('OTP Code is required for verification');
}
const verificationCheck = await client.verify.services(process.env.TWILIO_SERVICE_ID)
.verificationChecks
.create({ to: phoneNumber, code: otpCode });
return verificationCheck;
default:
throw new Error('Invalid action. Only "send" or "verify" are allowed');
}
} catch (error) {
console.error(`Error in Twilio service: ${error.message}`);
throw error;
}
}
module.exports = sendOrVerifyOTP;Editor is loading...
Leave a Comment