Skip to content

Instantly share code, notes, and snippets.

@bdeanindy
Created March 10, 2016 10:59
Show Gist options
  • Save bdeanindy/b43d9df7d898f5ff8b7b to your computer and use it in GitHub Desktop.
Save bdeanindy/b43d9df7d898f5ff8b7b to your computer and use it in GitHub Desktop.
Example of sending 2FA code via SMS using RingCentral JS SDK
/**
Just for example's sake, this would most-likely receive
429 HTTP responses if used in production.
Should cache the access_token and re-use in a more
modular fashion, but it helps explain the idea better.
**/
// Instantiate the RingCentral SDK
var sdk = new RingCentral.SDK({
server: 'https://platform.devtest.ringcentral.com', // SANDBOX
//server: 'https://platform.ringcentral.com', // PRODUCTION
appKey: 'REPLACE_WITH_YOUR_API_KEY',
appSecret: 'REPLACE_WITH_YOUR_API_SECRET'
});
// Create a reference to the RingCentral Platform Singleton
var platform = sdk.platform();
// Convenience method for example
var sendSMS = function sendSMS(source, dest, message) {
// Validate to suit
return platform
.post('/account/~/extension/~/sms', {
from: {phoneNumber: source},
to: {phoneNumber: dest},
text: message
})
.then(function(response) {
var data = response.json();
});
});
// Create a six (6) digit code. This would need to be cached to verify the user maps to the
// provided code within the allotted time during invalidation attempts once user enters code
// on the appropriate URI or app view of your choosing
var authCode = Math.floor(100000 + Math.random() * 900000);
// Authenticate, then send SMS
platform
.login({
username: 'REPLACE_WITH_YOUR_RC_USERNAME',
password: 'REPLACE_WITH_YOUR_RC_PASSWORD',
extension: 'REPLACE_WITH_YOUR RC_USER_EXTENSION' // OPTIONAL
})
.then(sendSMS('1234567890', '15557890123', 'Your authentication code is' + authCode
.catch(function(e) {
throw e;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment