Skip to content

Commit f3d8c7c

Browse files
authored
feat(User): add User#associateWithAuthData & #disassociateAuthData (#519)
1 parent 9e87b04 commit f3d8c7c

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"gulp-babel": "^6.1.1",
4949
"gulp-clean": "^0.3.1",
5050
"gulp-shell": "^0.5.2",
51-
"jsdoc": "~3.4.0",
51+
"jsdoc": "^3.5.5",
5252
"mocha": "^3.0.0",
5353
"nyc": "^8.1.0",
5454
"qiniu": "^6.1.11",

src/user.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ module.exports = function(AV) {
108108
}
109109
var success = provider.restoreAuthentication(authData[authType]);
110110
if (!success) {
111-
this._unlinkFrom(provider);
111+
this.dissociateAuthData(provider);
112112
}
113113
},
114114

@@ -160,21 +160,43 @@ module.exports = function(AV) {
160160
}
161161
},
162162

163+
/**
164+
* Associate the user with a third party authData.
165+
* @since 3.3.0
166+
* @param {Object} authData The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
167+
* @param {string} platform Available platform for sign up.
168+
* @return {Promise<AV.User>} A promise that is fulfilled with the user when completed.
169+
* @example user.associateWithAuthData({
170+
* openid: 'abc123',
171+
* access_token: '123abc',
172+
* expires_in: 1382686496
173+
* }, 'weixin').then(function(user) {
174+
* //Access user here
175+
* }).catch(function(error) {
176+
* //console.error("error: ", error);
177+
* });
178+
*/
179+
associateWithAuthData(authData, platform) {
180+
return this._linkWith(platform, authData);
181+
},
182+
163183
/**
164184
* 将用户与小程序用户进行关联。适用于为已经在用户系统中存在的用户关联当前使用小程序的微信帐号。
165185
* 仅在小程序中可用。
166186
*
167-
* @return {AV.User}
187+
* @return {Promise<AV.User>}
168188
*/
169189
linkWithWeapp() {
170190
return getWeappLoginCode().then(code => this._linkWith('lc_weapp', { code }));
171191
},
172192

173193
/**
174194
* Unlinks a user from a service.
175-
* @private
195+
* @param {string} platform
196+
* @return {Promise<AV.User>}
197+
* @since 3.3.0
176198
*/
177-
_unlinkFrom: function(provider) {
199+
dissociateAuthData(provider) {
178200
if (_.isString(provider)) {
179201
provider = AV.User._authProviders[provider];
180202
}
@@ -184,6 +206,15 @@ module.exports = function(AV) {
184206
});
185207
},
186208

209+
/**
210+
* @private
211+
* @deprecated
212+
*/
213+
_unlinkFrom(provider) {
214+
console.warn('DEPRECATED: User#_unlinkFrom 已废弃,请使用 User#dissociateAuthData 代替');
215+
return this.dissociateAuthData(provider);
216+
},
217+
187218
/**
188219
* Checks whether a user is linked to a service.
189220
* @private
@@ -800,24 +831,8 @@ module.exports = function(AV) {
800831
return getWeappLoginCode().then(code => this.signUpOrlogInWithAuthData({ code }, 'lc_weapp'));
801832
},
802833

803-
/**
804-
* Associate a user with a third party auth data(AccessToken).
805-
*
806-
* @param {AV.User} userObj A user which you want to associate.
807-
* @param {string} platform Available platform for sign up.
808-
* @param {Object} authData The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
809-
* @return {Promise} A promise that is fulfilled with the user when completed.
810-
* @example AV.User.associateWithAuthData(loginUser, 'weixin', {
811-
* openid: 'abc123',
812-
* access_token: '123abc',
813-
* expires_in: 1382686496
814-
* }).then(function(user) {
815-
* //Access user here
816-
* }).catch(function(error) {
817-
* //console.error("error: ", error);
818-
* });
819-
*/
820834
associateWithAuthData(userObj, platform, authData) {
835+
console.warn('DEPRECATED: User.associateWithAuthData 已废弃,请使用 User#dissociateAuthData 代替');
821836
return userObj._linkWith(platform, authData);
822837
},
823838
/**

storage.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ declare namespace AV {
576576
isAuthenticated(): Promise<boolean>;
577577
isCurrent(): boolean;
578578

579+
associateWithAuthData(authData: Object, platform?: string): Promise<User>;
580+
dissociateAuthData(platform?: string): Promise<User>;
579581

580582
getEmail(): string;
581583
setEmail(email: string, options?: AuthOptions): boolean;

0 commit comments

Comments
 (0)