Currently, I am working on a small project that involves receiving and verifying OTPs from MSG91. While I have managed to successfully receive OTPs using MSG91, I am encountering an issue when trying to verify OTPs - specifically, I keep getting an error message stating 'Failed to verify OTP : Invalid Auth key', even though I am certain that I am using the correct Auth key, template ID, and all other necessary details. The setup includes files such as setupProxy.js and App.js.
const { createProxyMiddleware } = require('http-proxy-middleware');
const templateId = 'xxxxxxxxxxxxxxxxx';
const authKey = 'xxxxxxxxxxxxxxxxxx';
module.exports = function(app) {
app.use(
'/api/verifyOTP',
createProxyMiddleware({
target: `https://control.msg91.com/api/v5/otp/verify?authkey=${authKey}`,
changeOrigin: true,
pathRewrite: function (path, req) {
const mobile = req.body ? req.body.mobile : '';
const otp = req.body ? req.body.otp : '';
return `&mobile=91${mobile}&otp=${otp}`;
}
})
);
}
<button onClick={verifyOTP}>Verify OTP</button>
const verifyOTP = async () => {
const mobileNumber = mobile;
try {
const response = await fetch(`/api/verifyOTP?otp=${otp}&mobile=91${mobileNumber}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok) {
const responseData = await response.json();
if (responseData.type === 'success' && responseData.message === 'OTP verified successfully') {
console.log('OTP Verified Successfully');
} else {
console.error('Failed to verify OTP:', responseData.message);
}
} else {
console.error('Error verifying OTP. Status code:', response.status);
}
} catch (error) {
console.error('Error verifying OTP:', error);
}
};