sinch-verification
Version:
Verify mobile numbers using Sinch Verification
56 lines (45 loc) • 1.67 kB
JavaScript
var sinchRestApi = require('sinch-rest-api')
var assert = require('assert-plus');
var Q = require('q');
var verificationManager = function(credentials, environment) {
assert.object(credentials, 'credentials');
assert.string(credentials.key, 'credentials.key');
assert.string(credentials.secret, 'credentials.secret');
this._sinchRestApi = require('sinch-rest-api')(credentials, environment);
}
verificationManager.prototype.createVerification = function(number, custom) {
var newVerification = new verificationInstance(number, custom);
newVerification._sinchRestApi = this._sinchRestApi;
return newVerification;
}
var verificationInstance = function(number, custom) {
this._number = number;
this._number = this._number.replace(/[-\s\(\)]/gi, '') // Remove possible irrelevant characters
this._custom = custom;
this.flagVerified = false;
}
verificationInstance.prototype.initiate = function() {
var verificationObj = {
identity: { type: 'number', endpoint: this._number},
custom: this._custom,
method: 'sms',
metadata: {
os: 'js',
platform: 'NodeJS'
}
};
return this._sinchRestApi.verification.verifyUserSMS(verificationObj);
}
verificationInstance.prototype.verify = function(secretCode) {
assert.string(secretCode);
var confirmationObj = {
number: this._number,
source: 'manual', //Always manual input of verification code in JS SDK. Hard-coded, so direct API usage does not pollute stats.
sms: { code: secretCode },
method: 'sms'
};
return this._sinchRestApi.verification.confirmUserSMS(confirmationObj);
}
module.exports = function(config, environment) {
return new verificationManager(config, environment)
};