Skip to content

Instantly share code, notes, and snippets.

@bibhuticoder
Last active May 30, 2021
Embed
What would you like to do?
app.post('/api/request-token', (req, res) => {
let { phoneNo } = req.body;
if (!phoneNo) return res.status(400).send({ message: "Phone no. is required" });
phoneNo = phoneNo.replace(/\s/g, "");
console.log("Sending code to ", phoneNo);
requestCode(phoneNo, (result) => {
if (result) {
// success at 1st attempt
if (result.status == '0') onOtpSuccess(res, result);
// concurrent OTP error
else if (result.status == '10') {
// cancel & try again once
cancelOtp(result.request_id, (cancelData) => {
// cancel success
if (cancelData && cancelData.status == '0') requestCode(phoneNo, (result2) => {
// success at 2nd attempt
if (result2 && result2.status == '0') await onOtpSuccess(res, result2);
// fail again at 2nd attempt
else onOtpError(res);
});
// fail at cancel
else onOtpError(res);
})
}
}
// fail at 1st attempt
else onOtpError(res);
})
} catch (e) {
console.log(e);
// fail server issue
onOtpError(res);
}
};
function onOtpSuccess(res, result) {
// ...
// store result.request_id on DB oe sent it to user
res.send({ message: "Success", request_id: result.request_id });
}
function onOtpError(res) {
//...
res.status(500).send({ message: 'Error sending OTP. Please try again.' });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment