Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send verfication codes to mobile numbers in node.js? (Like nodemailer, but for SMS)

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.

like image 339
silvesterprabu Avatar asked Feb 09 '13 12:02

silvesterprabu


People also ask

How can I send free SMS using node JS?

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.

How does Nodemailer verify email?

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.


4 Answers

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.

like image 165
JWK Avatar answered Oct 24 '22 19:10

JWK


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);
});
like image 26
BSB Avatar answered Oct 24 '22 18:10

BSB


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:

  1. User enters their phone number in a form and submits to your app
  2. Your app receives the phone number
  3. You send the user an SMS with an auth code
  4. User receives the auth code
  5. User enters auth code into a form and submits to your app
  6. Your app receives the auth code and checks that auth code against the phone number (maybe using the current session phone number)
  7. If the auth code is the one you are expected then the user is validated

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:

Send Verification Request

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;
  }
});

Check Authentication Code

nexmo.verify.control({request_id: verifyRequestId, cmd: 'cancel'}, function(err, result) {
  if(err) { console.error(err); }
  else {
    console.log(result);
  }
});
like image 23
leggetter Avatar answered Oct 24 '22 19:10

leggetter


You can use MSG 91 service. It provides OTP verification service as well.

like image 35
Abhishek Khedekar Avatar answered Oct 24 '22 19:10

Abhishek Khedekar