Untitled
unknown
plain_text
8 months ago
1.1 kB
3
Indexable
Never
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;
Leave a Comment