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
1var userPhone = '+972545999999'; 2// Request for sms phone verification -- please note before receiving SMS you should Follow the SMS Integration tutorial below 3Accounts.requestPhoneVerification(userPhone, function(){}); 4//Debug: Verify the user phone isn't confirmed it. 5console.log('Phone verification status is :', Accounts.isPhoneVerified()); 6 7// After receiving SMS let user enter his code and verify account by sending it to the server 8var verificationCode = 'CodeRecivedBySMS'; 9 10Accounts.verifyPhone(userPhone, verificationCode, function(){}); 11//Debug: Verify the user phone is confirmed. 12console.log('Phone verification status is :', Accounts.isPhoneVerified());
SMS Integration
If you are using twilio : you can just put your twilio credentials on server.
1SMS.twilio = {FROM: 'XXXXXXXXXXXX', ACCOUNT_SID: 'XXXXXXXXXXXXXXXXXXXXX', AUTH_TOKEN: 'XXXXXXXXXXXXXXXXXXXX'};
otherwise you can just override the function
1 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
1 SMS.phoneTemplates = { 2 from: '+9729999999', 3 text: function (user, code) { 4 return 'Welcome your invitation code is: ' + code; 5 } 6 };
- Note: it can only be done on server
Simple API
1 2 /** 3 * @summary Request a new verification code. create user if not exist 4 * @locus Client 5 * @param {String} phone - The phone we send the verification code to. 6 * @param {Function} [callback] Optional callback. Called with no arguments on success, or with a single `Error` argument on failure. 7 */ 8 Accounts.requestPhoneVerification = function (phone, callback) { }; 9 10 /** 11 * @summary Marks the user's phone as verified. Optional change passwords, Logs the user in afterwards.. 12 * @locus Client 13 * @param {String} phone - The phone number we want to verify. 14 * @param {String} code - The code retrieved in the SMS. 15 * @param {Function} [callback] Optional callback. Called with no arguments on success, or with a single `Error` argument on failure. 16 */ 17 Accounts.verifyPhone = function (phone, code, callback) {...}; 18 19 20 /** 21 * Returns whether the current user phone is verified 22 * @returns {boolean} Whether the user phone is verified 23 */ 24 Accounts.isPhoneVerified = function () { }; 25
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:
1Accounts._options.verificationMaxRetries = 3;
More code samples
Creating new user
1 // Create a user. 2 3 var options = {phone:'+972545999999'}; 4 // You can also create user with password 5 options.password = 'VeryHardPassword'; 6 7 8 Accounts.createUserWithPhone(options, function (){}); 9 // Debug: Verify the user phone isn't confirmed it. 10 console.log('Phone verification status is :', Accounts.isPhoneVerified());
1 var userPhone = '+972545999999'; 2 // Request for sms phone verification -- please note before receiving SMS you should Follow the SMS Integration tutorial below 3 Accounts.requestPhoneVerification(userPhone, function(){}); 4 //Debug: Verify the user phone isn't confirmed it. 5 console.log('Phone verification status is :', Accounts.isPhoneVerified()); 6 7 // After receiving SMS let user enter his code and verify account by sending it to the server 8 var verificationCode = 'CodeRecivedBySMS'; 9 var newPassword = null; 10 // You can keep your old password by sending null in the password field 11 Accounts.verifyPhone(userPhone, verificationCode, function(){}); 12 //Debug: Verify the user phone is confirmed. 13 console.log('Phone verification status is :', Accounts.isPhoneVerified());
Login existing user - Requires creating user with password
1 2 var userPhone = '+972545999999'; 3 var password = 'VerySecure'; 4 var callback = function() {}; 5 Accounts.createUserWithPhone({phone:userPhone, password:password}, function (){}); 6 7 Meteor.loginWithPhoneAndPassword({phone:userPhone}, password, callback);
Full API
1 2 /** 3 * @summary Log the user in with a password. 4 * @locus Client 5 * @param {Object | String} user Either a string interpreted as a phone; or an object with a single key: `phone` or `id`. 6 * @param {String} password The user's password. 7 * @param {Function} [callback] Optional callback. Called with no arguments on success, or with a single `Error` argument on failure. 8 */ 9 Meteor.loginWithPhoneAndPassword = function (selector, password, callback) { }; 10 11 /** 12 * @summary Create a new user. 13 * @locus Anywhere 14 * @param {Object} options 15 * @param {String} options.phone The user's full phone number. 16 * @param {String} options.password The user's password. This is __not__ sent in plain text over the wire. 17 * @param {Object} options.profile The user's profile, typically including the `name` field. 18 * @param {Function} [callback] Client only, optional callback. Called with no arguments on success, or with a single `Error` argument on failure. 19 */ 20 Accounts.createUserWithPhone = function (options, callback) { }; 21 22 /** 23 * @summary Request a new verification code. 24 * @locus Client 25 * @param {String} phone - The phone we send the verification code to. 26 * @param {Function} [callback] Optional callback. Called with no arguments on success, or with a single `Error` argument on failure. 27 */ 28 Accounts.requestPhoneVerification = function (phone, callback) { }; 29 30 /** 31 * @summary Marks the user's phone as verified. Optional change passwords, Logs the user in afterwards.. 32 * @locus Client 33 * @param {String} phone - The phone number we want to verify. 34 * @param {String} code - The code retrieved in the SMS. 35 * @param {String} newPassword, Optional, A new password for the user. This is __not__ sent in plain text over the wire. 36 * @param {Function} [callback] Optional callback. Called with no arguments on success, or with a single `Error` argument on failure. 37 */ 38 Accounts.verifyPhone = function (phone, code, newPassword, callback) {...}; 39 40 41 /** 42 * Returns whether the current user phone is verified 43 * @returns {boolean} Whether the user phone is verified 44 */ 45 Accounts.isPhoneVerified = function () { }; 46 47 48/** 49 * @summary Register a callback to be called after a phone verification attempt succeeds. 50 * @locus Server 51 * @param {Function} func The callback to be called when phone verification is successful. 52 * Function gets the userId of the new verified user as first argument 53 */ 54Accounts.onPhoneVerification = function (func) { };