Skip to content

Commit 9bd5224

Browse files
committed
feat(user): add instance methods for authData login
- AV.User#loginWithAuthData - AV.User#loginWithAuthDataAndUnionId - AV.User#loginWithWeapp see https://github.com/leancloud/paas/issues/991
1 parent 41cb566 commit 9bd5224

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

src/user.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,41 @@ module.exports = function(AV) {
423423
});
424424
},
425425

426+
/**
427+
* The same with {@link AV.User.loginWithAuthData}, except that you can set attributes before login.
428+
* @since 3.7.0
429+
*/
430+
loginWithAuthData(authData, platform, options) {
431+
return this._linkWith(platform, authData, options);
432+
},
433+
434+
/**
435+
* The same with {@link AV.User.loginWithAuthDataAndUnionId}, except that you can set attributes before login.
436+
* @since 3.7.0
437+
*/
438+
loginWithAuthDataAndUnionId(
439+
authData,
440+
platform,
441+
unionId,
442+
unionLoginOptions
443+
) {
444+
return this.loginWithAuthData(
445+
mergeUnionDataIntoAuthData(authData, unionId, unionLoginOptions),
446+
platform,
447+
unionLoginOptions
448+
);
449+
},
450+
451+
/**
452+
* The same with {@link AV.User.loginWithWeapp}, except that you can set attributes before login.
453+
* @since 3.7.0
454+
*/
455+
loginWithWeapp(options) {
456+
return getWeappLoginCode().then(code =>
457+
this.loginWithAuthData({ code }, 'lc_weapp', options)
458+
);
459+
},
460+
426461
/**
427462
* Logs in a AV.User. On success, this saves the session to localStorage,
428463
* so you can retrieve the currently logged in user using

test/user.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ describe('User', function() {
203203

204204
describe('authData and unionId', () => {
205205
const now = Date.now();
206+
const username = now.toString(36);
206207
it('failOnNotExist', () =>
207208
AV.User.signUpOrlogInWithAuthData(
208209
{
@@ -216,30 +217,34 @@ describe('User', function() {
216217
}
217218
).should.be.rejectedWith(/Could not find user/));
218219
it('should login as the same user', () => {
219-
return AV.User.signUpOrlogInWithAuthDataAndUnionId(
220-
{
221-
uid: 'openid1' + now,
222-
access_token: 'access_token',
223-
expires_in: 1382686496,
224-
},
225-
'weixin_1',
226-
'unionid' + now,
227-
{
228-
asMainAccount: true,
229-
}
230-
).then(user1 => {
231-
return AV.User.signUpOrlogInWithAuthDataAndUnionId(
220+
return new AV.User()
221+
.setUsername(username)
222+
.loginWithAuthDataAndUnionId(
232223
{
233-
uid: 'openid2' + now,
224+
uid: 'openid1' + now,
234225
access_token: 'access_token',
235226
expires_in: 1382686496,
236227
},
237-
'weixin_2',
238-
'unionid' + now
239-
).then(user2 => {
240-
user2.id.should.be.eql(user1.id);
228+
'weixin_1',
229+
'unionid' + now,
230+
{
231+
asMainAccount: true,
232+
}
233+
)
234+
.then(user1 => {
235+
return AV.User.signUpOrlogInWithAuthDataAndUnionId(
236+
{
237+
uid: 'openid2' + now,
238+
access_token: 'access_token',
239+
expires_in: 1382686496,
240+
},
241+
'weixin_2',
242+
'unionid' + now
243+
).then(user2 => {
244+
user2.id.should.be.eql(user1.id);
245+
user2.getUsername().should.be.eql(username);
246+
});
241247
});
242-
});
243248
});
244249
});
245250

0 commit comments

Comments
 (0)