Accounts-Phone
Accounts-Phone is a Meteor package that let you authenticate by phone number.
The package use SMS code verification to verify the user account.
The package is based and inspired by Meteor Accounts-Password package.
Installation
In a Meteor app directory, enter:
$ meteor add okland:accounts-phone
Via Bower:
$ bower install accounts-phone
Add to your index.html
<script src="bower_components/accounts-base-client-side/dist/accounts-base-client-side.bundle.min.js"></script>
<script src="bower_components/accounts-phone/dist/accounts-phone.bundle.min.js"></script>
Examples
Let's say you want to register new user and verify him using his phone number
Verify phone number - Create user if not exists
var userPhone = '+972545999999';
Accounts.requestPhoneVerification(userPhone, function(){});
console.log('Phone verification status is :', Accounts.isPhoneVerified());
var verificationCode = 'CodeRecivedBySMS';
Accounts.verifyPhone(userPhone, verificationCode, function(){});
console.log('Phone verification status is :', Accounts.isPhoneVerified());
SMS Integration
If you are using twilio :
you can just put your twilio credentials on server.
SMS.twilio = {FROM: 'XXXXXXXXXXXX', ACCOUNT_SID: 'XXXXXXXXXXXXXXXXXXXXX', AUTH_TOKEN: 'XXXXXXXXXXXXXXXXXXXX'};
otherwise you can just override the function
SMS.send = function (options) {};
Where the parameter options is an object containing :
* @param options
* @param options.from {String} - The sending SMS number
* @param options.to {String} - The receiver SMS number
* @param options.body {String} - The content of the SMS
Moreover to control the Sending number and the message content you can override the phone Template
SMS.phoneTemplates = {
from: '+9729999999',
text: function (user, code) {
return 'Welcome your invitation code is: ' + code;
}
};
- Note: it can only be done on server
Simple API
Accounts.requestPhoneVerification = function (phone, callback) { };
Accounts.verifyPhone = function (phone, code, callback) {...};
Accounts.isPhoneVerified = function () { };
Settings - you can control
- verificationCodeLength : The length of the verification code
- verificationMaxRetries : The number of SMS verification tries before verification temporary lock
- verificationRetriesWaitTime : The verification lock time after max retries
- verificationWaitTime : The verification lock time if between two retries
- sendPhoneVerificationCodeOnCreation : Whether to send phone number verification on user creation
- forbidClientAccountCreation: Don't let client create user on server
- phoneVerificationMasterCode: Optional master code if exists let user verify account by entering this code for example '1234'
- adminPhoneNumbers: Optional array of admin phone numbers - don't need to be valid phone numbers for example ['+972123456789', '+972987654321']
In order to change those settings just override the property under :
Accounts._options
For example to change the verificationMaxRetries to 3 all we need to do is:
Accounts._options.verificationMaxRetries = 3;
More code samples
Creating new user
var options = {phone:'+972545999999'};
options.password = 'VeryHardPassword';
Accounts.createUserWithPhone(options, function (){});
console.log('Phone verification status is :', Accounts.isPhoneVerified());
var userPhone = '+972545999999';
Accounts.requestPhoneVerification(userPhone, function(){});
console.log('Phone verification status is :', Accounts.isPhoneVerified());
var verificationCode = 'CodeRecivedBySMS';
var newPassword = null;
Accounts.verifyPhone(userPhone, verificationCode, function(){});
console.log('Phone verification status is :', Accounts.isPhoneVerified());
Login existing user - Requires creating user with password
var userPhone = '+972545999999';
var password = 'VerySecure';
var callback = function() {};
Accounts.createUserWithPhone({phone:userPhone, password:password}, function (){});
Meteor.loginWithPhoneAndPassword({phone:userPhone}, password, callback);
Full API
Meteor.loginWithPhoneAndPassword = function (selector, password, callback) { };
Accounts.createUserWithPhone = function (options, callback) { };
Accounts.requestPhoneVerification = function (phone, callback) { };
Accounts.verifyPhone = function (phone, code, newPassword, callback) {...};
Accounts.isPhoneVerified = function () { };
Accounts.onPhoneVerification = function (func) { };