Skip to content

Commit 33aaf22

Browse files
committed
Added signUpOrlogInWithMobilePhone for AV.User
1 parent c50a440 commit 33aaf22

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

lib/av.js

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,7 @@
16251625
route !== "requestSmsCode" &&
16261626
route !== "verifySmsCode" &&
16271627
route !== "users" &&
1628+
route !== "usersByMobilePhone" &&
16281629
route !== "cloudQuery" &&
16291630
route !== "qiniu" &&
16301631
route !== "statuses" &&
@@ -5328,7 +5329,9 @@
53285329
route = "users";
53295330
className = null;
53305331
}
5331-
var request = AV._request(route, className, model.id, method, json);
5332+
//hook makeRequest in options.
5333+
var makeRequest = options._makeRequest || AV._request;
5334+
var request = makeRequest(route, className, model.id, method, json);
53325335

53335336
request = request.then(function(resp, status, xhr) {
53345337
var serverAttrs = model.parse(resp, status, xhr);
@@ -6945,6 +6948,68 @@
69456948
return this.save(attrs, newOptions);
69466949
},
69476950

6951+
/**
6952+
* Signs up a new user with mobile phone and sms code.
6953+
* You should call this instead of save for
6954+
* new AV.Users. This will create a new AV.User on the server, and
6955+
* also persist the session on disk so that you can access the user using
6956+
* <code>current</code>.
6957+
*
6958+
* <p>A username and password must be set before calling signUp.</p>
6959+
*
6960+
* <p>Calls options.success or options.error on completion.</p>
6961+
*
6962+
* @param {Object} attrs Extra fields to set on the new user, or null.
6963+
* @param {Object} options A Backbone-style options object.
6964+
* @return {AV.Promise} A promise that is fulfilled when the signup
6965+
* finishes.
6966+
* @see AV.User.signUp
6967+
*/
6968+
signUpOrlogInWithMobilePhone: function(attrs, options) {
6969+
var error;
6970+
options = options || {};
6971+
6972+
var mobilePhoneNumber = (attrs && attrs.mobilePhoneNumber) ||
6973+
this.get("mobilePhoneNumber");
6974+
if (!mobilePhoneNumber || (mobilePhoneNumber === "")) {
6975+
error = new AV.Error(
6976+
AV.Error.OTHER_CAUSE,
6977+
"Cannot sign up or login user by mobilePhoneNumber " +
6978+
"with an empty mobilePhoneNumber.");
6979+
if (options && options.error) {
6980+
options.error(this, error);
6981+
}
6982+
return AV.Promise.error(error);
6983+
}
6984+
6985+
var smsCode = (attrs && attrs.smsCode) || this.get("smsCode");
6986+
if (!smsCode || (smsCode === "")) {
6987+
error = new AV.Error(
6988+
AV.Error.OTHER_CAUSE,
6989+
"Cannot sign up or login user by mobilePhoneNumber " +
6990+
"with an empty smsCode.");
6991+
if (options && options.error) {
6992+
options.error(this, error);
6993+
}
6994+
return AV.Promise.error(error);
6995+
}
6996+
6997+
// Overridden so that the user can be made the current user.
6998+
var newOptions = _.clone(options);
6999+
newOptions._makeRequest = function(route, className, id, method, json) {
7000+
return AV._request('usersByMobilePhone', null, null, "POST", json);
7001+
};
7002+
newOptions.success = function(model) {
7003+
model._handleSaveResult(true);
7004+
delete model.attributes.smsCode;
7005+
delete model._serverData.smsCode;
7006+
if (options.success) {
7007+
options.success.apply(this, arguments);
7008+
}
7009+
};
7010+
return this.save(attrs, newOptions);
7011+
},
7012+
69487013
/**
69497014
* Logs in a AV.User. On success, this saves the session to localStorage,
69507015
* so you can retrieve the currently logged in user using
@@ -7268,6 +7333,31 @@
72687333
return user.logIn(options);
72697334
},
72707335

7336+
/**
7337+
* Sign up or logs in a user with a mobilePhoneNumber and smsCode.
7338+
* On success, this saves the session to disk, so you can retrieve the currently
7339+
* logged in user using <code>current</code>.
7340+
*
7341+
* <p>Calls options.success or options.error on completion.</p>
7342+
*
7343+
* @param {String} mobilePhoneNumber The user's mobilePhoneNumber.
7344+
* @param {String} smsCode The sms code sent by AV.Cloud.requestSmsCode
7345+
* @param {Object} attributes The user's other attributes such as username etc.
7346+
* @param {Object} options A Backbone-style options object.
7347+
* @return {AV.Promise} A promise that is fulfilled with the user when
7348+
* the login completes.
7349+
* @see AV.User#signUpOrlogInWithMobilePhone
7350+
* @see AV.Cloud#requestSmsCode
7351+
*/
7352+
signUpOrlogInWithMobilePhone: function(mobilePhoneNumber, smsCode, attrs, options) {
7353+
attrs = attrs || {};
7354+
attrs.mobilePhoneNumber = mobilePhoneNumber;
7355+
attrs.smsCode = smsCode;
7356+
var user = AV.Object._create("_User");
7357+
return user.signUpOrlogInWithMobilePhone(attrs, options);
7358+
},
7359+
7360+
72717361
/**
72727362
* Logs in a user with a mobile phone number and password. On success, this
72737363
* saves the session to disk, so you can retrieve the currently logged in

tests/user.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ describe("Associations",function(){
156156
})
157157
})
158158

159-
160159
describe("Follow/unfollow users",function(){
161160
it("should follow/unfollow",function(done){
162161
var user = AV.User.current();

0 commit comments

Comments
 (0)