I have sent confirmation e-mails to users by using nodemailer for my project. It is working fine.
Now I am looking to send verification codes to mobile numbers from node.js, but I don’t know how to do that.
Is there any module to send verification codes to mobile numbers, like nodemailer does with e-mail addresses? Or if not, how can I do this myself?
I developing my project using node.js and mongodb, JavaScript and jQuery.
To send a message, use the nexmo. sms. sendSms function and pass your virtual number you are sending the message from, a recipient number, and the message to be sent. Also, you can pass optional params, and a callback.
We are going to use nodemailer to send email's. To verify e-mail we will generate one random key in our script, store it, and when user click on verification link in e-mail we will fetch the key from that link and compare it to the store one.
Most carriers provide a SMS gateway to which you can send an email and have it arrive as SMS. If you want a free way of sending SMS that works with your current nodemailer implementation, this is probably your best option. Otherwise, you might want to search for paid SMS services that you can integrate with.
Here is a list of SMS gateways: http://en.wikipedia.org/wiki/List_of_SMS_gateways
From the linked Wikipedia page: For instance, to send to a number typically expressed in the USA as 987-555-0100, one would email 9875550100@SMS-gateway.
NodeJS package https://www.npmjs.com/package/springedge will be easy to send sms. You can install as
npm install springedge
Code example of sending sms:
// send sms
var springedge = require('springedge');
var params = {
'apikey': '', // API Key
'sender': 'SEDEMO', // Sender Name
'to': [
'919019xxxxxxxx' //Moblie Number
],
'message': 'test+message'
};
springedge.messages.send(params, 5000, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});
To ensure you can reach a users mobile no matter their location and network it's likely you're going to have to look at a paid service such as Nexmo (who I work for) or Twilio.
With these services you can either build your own verification (2FA - Two Factor Authentication) workflow:
Or you can use their 2FA authentication products (Nexmo - Verify or Twilio - Authy that should help simplify this workflow.
Using Nexmo verify the code would be:
var Nexmo = require('nexmo');
var nexmo = new Nexmo({apiKey: API_KEY, apiSecret: API_SECRET});
var verifyRequestId = null; // use in the check process
nexmo.verify.request({number: TO_NUMBER, brand: APP_NAME}, function(err, result) {
if(err) { console.error(err); }
else {
verifyRequestId = result. request_id;
}
});
nexmo.verify.control({request_id: verifyRequestId, cmd: 'cancel'}, function(err, result) {
if(err) { console.error(err); }
else {
console.log(result);
}
});
You can use MSG 91 service. It provides OTP verification service as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With