Skip to content

Commit 130d344

Browse files
wangxiaoleeyeh
authored andcommitted
Add associateWithAuthData() and fix signUpOrlogInWithAuthData bug (#318)
* fix signUpOrlogInWithAuthData() can not use by promise style * Add associateWithAuthData() * codereview fix
1 parent 8d35139 commit 130d344

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

src/user.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -757,38 +757,42 @@ module.exports = function(AV) {
757757
* On success, this saves the session to disk, so you can retrieve the currently
758758
* logged in user using <code>current</code>.
759759
*
760-
* <p>Calls options.success or options.error on completion.</p>
761-
*
762-
* @param {Object} data The response json data returned from third party token.
760+
* @param {Object} authData The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
763761
* @param {string} platform Available platform for sign up.
764762
* @param {Object} [callback] An object that has an optional success function, that takes no arguments and will be called on a successful puSH. and an error function that takes a AVError and will be called if the push failed.
765763
* @return {AV.Promise} A promise that is fulfilled with the user when
766764
* the login completes.
767-
* @example AV.User.signUpOrlogInWithAuthData(data, platform, {
768-
* success: function(user) {
769-
* //Access user here
770-
* },
771-
* error: function(error) {
772-
* //console.log("error: ", error);
773-
* }
774-
* });
765+
* @example AV.User.signUpOrlogInWithAuthData(authData, platform).then(function(user) {
766+
* //Access user here
767+
* }).catch(function(error) {
768+
* //console.error("error: ", error);
769+
* });
775770
* @see {@link https://leancloud.cn/docs/js_guide.html#绑定第三方平台账户}
776771
*/
777-
signUpOrlogInWithAuthData: function (data, platform, callback) {
778-
/**
779-
* Construct accessToken
780-
*/
781-
return AV.User._logInWith(platform, {
782-
"authData": data,
783-
success: function (user) {
784-
callback.success(user);
785-
},
786-
error: function (error) {
787-
callback.error(error);
788-
}
789-
});
772+
signUpOrlogInWithAuthData(authData, platform, callback) {
773+
return AV.User._logInWith(platform, { authData })._thenRunCallbacks(callback);
790774
},
791775

776+
/**
777+
* Associate a user with a third party auth data(AccessToken).
778+
*
779+
* @param {AV.User} userObj A user which you want to associate.
780+
* @param {string} platform Available platform for sign up.
781+
* @param {Object} authData The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
782+
* @return {AV.Promise} A promise that is fulfilled with the user when completed.
783+
* @example AV.User.associateWithAuthData(loginUser, 'weixin', {
784+
* openid: 'abc123',
785+
* access_token: '123abc',
786+
* expires_in: 1382686496
787+
* }).then(function(user) {
788+
* //Access user here
789+
* }).catch(function(error) {
790+
* //console.error("error: ", error);
791+
* });
792+
*/
793+
associateWithAuthData(userObj, platform, authData) {
794+
return userObj._linkWith(platform, { authData });
795+
},
792796
/**
793797
* Logs out the currently logged in user session. This will remove the
794798
* session from disk, log out of linked services, and future calls to

test/user.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,30 @@ describe("User", function() {
270270
id: getFixedId()
271271
}
272272

273-
AV.User.signUpOrlogInWithAuthData(data, "anonymous", {
274-
success: function(user) {
275-
expect(user.id).to.be.ok();
276-
done();
277-
},
278-
error: function(error) {
279-
throw error.message;
280-
}
273+
AV.User.signUpOrlogInWithAuthData(data, 'anonymous').then(function(user) {
274+
expect(user.id).to.be.ok();
275+
done();
276+
}).catch(function(error) {
277+
throw error;
278+
});
279+
});
280+
});
281+
282+
describe('associate with authData', function() {
283+
it('logIn an user, and associate with authData', function() {
284+
var username = Date.now().toString(36);
285+
var password = '123456';
286+
var user = new AV.User();
287+
user.set('username', username);
288+
user.set('password', password);
289+
return user.save().then(function() {
290+
return AV.User.logIn(username, password);
291+
}).then(function (loginedUser) {
292+
return AV.User.associateWithAuthData(loginedUser, 'weixin', {
293+
openid: 'aaabbbccc123123',
294+
access_token: 'a123123aaabbbbcccc',
295+
expires_in: 1382686496,
296+
});
281297
});
282298
});
283299
});

0 commit comments

Comments
 (0)