|
13 | 13 | */ |
14 | 14 | (function(root) { |
15 | 15 | root.AV = root.AV || {}; |
16 | | - root.AV.VERSION = "js0.4.6"; |
| 16 | + root.AV.VERSION = "js0.4.7"; |
17 | 17 | }(this)); |
18 | 18 | // Underscore.js 1.4.4 |
19 | 19 | // http://underscorejs.org |
|
1409 | 1409 | *Use china avoscloud API service:https://cn.avoscloud.com |
1410 | 1410 | */ |
1411 | 1411 | AV.useAVCloudCN = function(){ |
1412 | | - AV.serverURL = "https://cn.avoscloud.com"; |
| 1412 | + AV.serverURL = "https://leancloud.cn"; |
1413 | 1413 | }; |
1414 | 1414 |
|
1415 | 1415 | /** |
1416 | 1416 | *Use USA avoscloud API service:https://us.avoscloud.com |
1417 | 1417 | */ |
1418 | 1418 | AV.useAVCloudUS = function(){ |
1419 | | - AV.serverURL = "https://us.avoscloud.com"; |
| 1419 | + AV.serverURL = "https://avoscloud.us"; |
1420 | 1420 | }; |
1421 | 1421 |
|
1422 | 1422 | /** |
|
1509 | 1509 | } |
1510 | 1510 | }; |
1511 | 1511 | xdr.onerror = xdr.ontimeout = function() { |
| 1512 | + // Let's fake a real error message. |
| 1513 | + var fakeResponse = { |
| 1514 | + responseText: JSON.stringify({ |
| 1515 | + code: AV.Error.X_DOMAIN_REQUEST, |
| 1516 | + error: "IE's XDomainRequest does not supply error info." |
| 1517 | + }) |
| 1518 | + }; |
1512 | 1519 | promise.reject(xdr); |
1513 | 1520 | }; |
1514 | 1521 | xdr.onprogress = function() {}; |
|
1517 | 1524 | return promise; |
1518 | 1525 | }; |
1519 | 1526 |
|
| 1527 | + AV._useXDomainRequest = function() { |
| 1528 | + if (typeof(XDomainRequest) !== "undefined") { |
| 1529 | + // We're in IE 8+. |
| 1530 | + if ('withCredentials' in new XMLHttpRequest()) { |
| 1531 | + // We're in IE 10+. |
| 1532 | + return false; |
| 1533 | + } |
| 1534 | + return true; |
| 1535 | + } |
| 1536 | + return false; |
| 1537 | + }; |
1520 | 1538 |
|
1521 | 1539 | AV._ajax = function(method, url, data, success, error) { |
1522 | 1540 | var options = { |
1523 | 1541 | success: success, |
1524 | 1542 | error: error |
1525 | 1543 | }; |
1526 | 1544 |
|
1527 | | - if (typeof(XDomainRequest) !== "undefined") { |
| 1545 | + if (AV._useXDomainRequest()) { |
1528 | 1546 | return AV._ajaxIE8(method, url, data)._thenRunCallbacks(options); |
1529 | 1547 | } |
1530 | 1548 |
|
|
1607 | 1625 | route !== "requestSmsCode" && |
1608 | 1626 | route !== "verifySmsCode" && |
1609 | 1627 | route !== "users" && |
| 1628 | + route !== "usersByMobilePhone" && |
1610 | 1629 | route !== "cloudQuery" && |
1611 | 1630 | route !== "qiniu" && |
1612 | 1631 | route !== "statuses" && |
1613 | 1632 | route !== 'subscribe/statuses/count' && |
1614 | 1633 | route !== 'subscribe/statuses' && |
| 1634 | + !(/users\/[^\/]+\/updatePassword/.test(route)) && |
1615 | 1635 | !(/users\/[^\/]+\/friendship\/[^\/]+/.test(route))) { |
1616 | 1636 | throw "Bad route: '" + route + "'."; |
1617 | 1637 | } |
|
2237 | 2257 | * Twitter) is unsupported. |
2238 | 2258 | * @constant |
2239 | 2259 | */ |
2240 | | - UNSUPPORTED_SERVICE: 252 |
| 2260 | + UNSUPPORTED_SERVICE: 252, |
| 2261 | + /** |
| 2262 | + * Error code indicating a real error code is unavailable because |
| 2263 | + * we had to use an XDomainRequest object to allow CORS requests in |
| 2264 | + * Internet Explorer, which strips the body from HTTP responses that have |
| 2265 | + * a non-2XX status code. |
| 2266 | + * @constant |
| 2267 | + */ |
| 2268 | + X_DOMAIN_REQUEST: 602 |
2241 | 2269 | }); |
2242 | 2270 |
|
2243 | 2271 | }(this)); |
|
4528 | 4556 | this._fetchWhenSave = enable; |
4529 | 4557 | }, |
4530 | 4558 |
|
| 4559 | + /** |
| 4560 | + * Returns the object's objectId. |
| 4561 | + * @return {String} the objectId. |
| 4562 | + */ |
| 4563 | + getObjectId: function() { |
| 4564 | + return this.id; |
| 4565 | + }, |
| 4566 | + |
| 4567 | + /** |
| 4568 | + * Returns the object's createdAt attribute. |
| 4569 | + * @return {Date} |
| 4570 | + */ |
| 4571 | + getCreatedAt: function() { |
| 4572 | + return this.createdAt || this.get('createdAt'); |
| 4573 | + }, |
| 4574 | + |
| 4575 | + /** |
| 4576 | + * Returns the object's updatedAt attribute. |
| 4577 | + * @return {Date} |
| 4578 | + */ |
| 4579 | + getUpdatedAt: function() { |
| 4580 | + return this.updatedAt || this.get('updatedAt'); |
| 4581 | + }, |
| 4582 | + |
4531 | 4583 | /** |
4532 | 4584 | * Returns a JSON version of the object suitable for saving to AV. |
4533 | 4585 | * @return {Object} |
|
5277 | 5329 | route = "users"; |
5278 | 5330 | className = null; |
5279 | 5331 | } |
5280 | | - 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); |
5281 | 5335 |
|
5282 | 5336 | request = request.then(function(resp, status, xhr) { |
5283 | 5337 | var serverAttrs = model.parse(resp, status, xhr); |
|
6894 | 6948 | return this.save(attrs, newOptions); |
6895 | 6949 | }, |
6896 | 6950 |
|
| 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.signUpOrlogInWithMobilePhone |
| 6967 | + * @see AV.Cloud.requestSmsCode |
| 6968 | + */ |
| 6969 | + signUpOrlogInWithMobilePhone: function(attrs, options) { |
| 6970 | + var error; |
| 6971 | + options = options || {}; |
| 6972 | + |
| 6973 | + var mobilePhoneNumber = (attrs && attrs.mobilePhoneNumber) || |
| 6974 | + this.get("mobilePhoneNumber"); |
| 6975 | + if (!mobilePhoneNumber || (mobilePhoneNumber === "")) { |
| 6976 | + error = new AV.Error( |
| 6977 | + AV.Error.OTHER_CAUSE, |
| 6978 | + "Cannot sign up or login user by mobilePhoneNumber " + |
| 6979 | + "with an empty mobilePhoneNumber."); |
| 6980 | + if (options && options.error) { |
| 6981 | + options.error(this, error); |
| 6982 | + } |
| 6983 | + return AV.Promise.error(error); |
| 6984 | + } |
| 6985 | + |
| 6986 | + var smsCode = (attrs && attrs.smsCode) || this.get("smsCode"); |
| 6987 | + if (!smsCode || (smsCode === "")) { |
| 6988 | + error = new AV.Error( |
| 6989 | + AV.Error.OTHER_CAUSE, |
| 6990 | + "Cannot sign up or login user by mobilePhoneNumber " + |
| 6991 | + "with an empty smsCode."); |
| 6992 | + if (options && options.error) { |
| 6993 | + options.error(this, error); |
| 6994 | + } |
| 6995 | + return AV.Promise.error(error); |
| 6996 | + } |
| 6997 | + |
| 6998 | + // Overridden so that the user can be made the current user. |
| 6999 | + var newOptions = _.clone(options); |
| 7000 | + newOptions._makeRequest = function(route, className, id, method, json) { |
| 7001 | + return AV._request('usersByMobilePhone', null, null, "POST", json); |
| 7002 | + }; |
| 7003 | + newOptions.success = function(model) { |
| 7004 | + model._handleSaveResult(true); |
| 7005 | + delete model.attributes.smsCode; |
| 7006 | + delete model._serverData.smsCode; |
| 7007 | + if (options.success) { |
| 7008 | + options.success.apply(this, arguments); |
| 7009 | + } |
| 7010 | + }; |
| 7011 | + return this.save(attrs, newOptions); |
| 7012 | + }, |
| 7013 | + |
6897 | 7014 | /** |
6898 | 7015 | * Logs in a AV.User. On success, this saves the session to localStorage, |
6899 | 7016 | * so you can retrieve the currently logged in user using |
|
7026 | 7143 | return AV.Object.prototype.fetch.call(this, newOptions); |
7027 | 7144 | }, |
7028 | 7145 |
|
| 7146 | + /** |
| 7147 | + * Update user's new password safely based on old password. |
| 7148 | + * @param {String} oldPassword, the old password. |
| 7149 | + * @param {String} newPassword, the new password. |
| 7150 | + * @param {Object} An optional Backbone-like options object with |
| 7151 | + * success and error callbacks that will be invoked once the iteration |
| 7152 | + * has finished. |
| 7153 | + */ |
| 7154 | + updatePassword: function(oldPassword, newPassword, options) { |
| 7155 | + var route = 'users/' + this.id + '/updatePassword'; |
| 7156 | + var params = { |
| 7157 | + old_password: oldPassword, |
| 7158 | + new_password: newPassword |
| 7159 | + }; |
| 7160 | + var request = AV._request(route, null, null, 'PUT', params); |
| 7161 | + return request._thenRunCallbacks(options, this); |
| 7162 | + }, |
| 7163 | + |
7029 | 7164 | /** |
7030 | 7165 | * Returns true if <code>current</code> would return this user. |
7031 | 7166 | * @see AV.User#current |
|
7199 | 7334 | return user.logIn(options); |
7200 | 7335 | }, |
7201 | 7336 |
|
| 7337 | + /** |
| 7338 | + * Sign up or logs in a user with a mobilePhoneNumber and smsCode. |
| 7339 | + * On success, this saves the session to disk, so you can retrieve the currently |
| 7340 | + * logged in user using <code>current</code>. |
| 7341 | + * |
| 7342 | + * <p>Calls options.success or options.error on completion.</p> |
| 7343 | + * |
| 7344 | + * @param {String} mobilePhoneNumber The user's mobilePhoneNumber. |
| 7345 | + * @param {String} smsCode The sms code sent by AV.Cloud.requestSmsCode |
| 7346 | + * @param {Object} attributes The user's other attributes such as username etc. |
| 7347 | + * @param {Object} options A Backbone-style options object. |
| 7348 | + * @return {AV.Promise} A promise that is fulfilled with the user when |
| 7349 | + * the login completes. |
| 7350 | + * @see AV.User#signUpOrlogInWithMobilePhone |
| 7351 | + * @see AV.Cloud.requestSmsCode |
| 7352 | + */ |
| 7353 | + signUpOrlogInWithMobilePhone: function(mobilePhoneNumber, smsCode, attrs, options) { |
| 7354 | + attrs = attrs || {}; |
| 7355 | + attrs.mobilePhoneNumber = mobilePhoneNumber; |
| 7356 | + attrs.smsCode = smsCode; |
| 7357 | + var user = AV.Object._create("_User"); |
| 7358 | + return user.signUpOrlogInWithMobilePhone(attrs, options); |
| 7359 | + }, |
| 7360 | + |
| 7361 | + |
7202 | 7362 | /** |
7203 | 7363 | * Logs in a user with a mobile phone number and password. On success, this |
7204 | 7364 | * saves the session to disk, so you can retrieve the currently logged in |
|
7428 | 7588 | delete json._id; |
7429 | 7589 | AV.User._currentUser._sessionToken = json._sessionToken; |
7430 | 7590 | delete json._sessionToken; |
7431 | | - AV.User._currentUser.set(json); |
| 7591 | + AV.User._currentUser._finishFetch(json); |
| 7592 | + //AV.User._currentUser.set(json); |
7432 | 7593 |
|
7433 | 7594 | AV.User._currentUser._synchronizeAllAuthData(); |
7434 | 7595 | AV.User._currentUser._refreshCache(); |
|
0 commit comments