Skip to content

Commit 41c979f

Browse files
committed
feat(user): add failOnNotExist option for .signUpOrlogInWithAuthData[AndUnionId]
see #991
1 parent 4fc4486 commit 41c979f

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/object.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,10 @@ module.exports = function(AV) {
10301030
if (model._fetchWhenSave || options.fetchWhenSave) {
10311031
query['new'] = 'true';
10321032
}
1033+
// user login option
1034+
if (options._failOnNotExist) {
1035+
query.failOnNotExist = 'true';
1036+
}
10331037

10341038
if (options.query) {
10351039
var queryJSON;

src/user.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ module.exports = function(AV) {
159159
* call linkWith on the user (even if it doesn't exist yet on the server).
160160
* @private
161161
*/
162-
_linkWith: function(provider, data) {
162+
_linkWith: function(provider, data, { failOnNotExist = false } = {}) {
163163
var authType;
164164
if (_.isString(provider)) {
165165
authType = provider;
@@ -170,7 +170,10 @@ module.exports = function(AV) {
170170
if (data) {
171171
return this.save(
172172
{ authData: { [authType]: data } },
173-
{ fetchWhenSave: !!this.get('authData') }
173+
{
174+
fetchWhenSave: !!this.get('authData'),
175+
_failOnNotExist: failOnNotExist,
176+
}
174177
).then(function(model) {
175178
return model._handleSaveResult(true).then(function() {
176179
return model;
@@ -211,7 +214,7 @@ module.exports = function(AV) {
211214
* @param {string} unionId
212215
* @param {Object} [unionLoginOptions]
213216
* @param {string} [unionLoginOptions.unionIdPlatform = 'weixin'] unionId platform
214-
* @param {boolean} [unionLoginOptions.asMainAccount = false]
217+
* @param {boolean} [unionLoginOptions.asMainAccount = false] If true, the unionId will be associated with the user.
215218
* @return {Promise<AV.User>} A promise that is fulfilled with the user when completed.
216219
* @example user.associateWithAuthDataAndUnionId({
217220
* openid: 'abc123',
@@ -897,6 +900,8 @@ module.exports = function(AV) {
897900
*
898901
* @param {Object} authData The response json data returned from third party token, maybe like { openid: 'abc123', access_token: '123abc', expires_in: 1382686496 }
899902
* @param {string} platform Available platform for sign up.
903+
* @param {Object} [options]
904+
* @param {boolean} [options.failOnNotExist] If true, the login request will fail when no user match this authData exists.
900905
* @return {Promise} A promise that is fulfilled with the user when
901906
* the login completes.
902907
* @example AV.User.signUpOrlogInWithAuthData({
@@ -910,8 +915,8 @@ module.exports = function(AV) {
910915
* });
911916
* @see {@link https://leancloud.cn/docs/js_guide.html#绑定第三方平台账户}
912917
*/
913-
signUpOrlogInWithAuthData(authData, platform) {
914-
return AV.User._logInWith(platform, authData);
918+
signUpOrlogInWithAuthData(authData, platform, options) {
919+
return AV.User._logInWith(platform, authData, options);
915920
},
916921

917922
/**
@@ -922,15 +927,16 @@ module.exports = function(AV) {
922927
* @param {string} unionId
923928
* @param {Object} [unionLoginOptions]
924929
* @param {string} [unionLoginOptions.unionIdPlatform = 'weixin'] unionId platform
925-
* @param {boolean} [unionLoginOptions.asMainAccount = false]
930+
* @param {boolean} [unionLoginOptions.asMainAccount = false] If true, the unionId will be associated with the user.
931+
* @param {boolean} [options.failOnNotExist] If true, the login request will fail when no user match this authData exists.
926932
* @return {Promise<AV.User>} A promise that is fulfilled with the user when completed.
927933
* @example AV.User.signUpOrlogInWithAuthDataAndUnionId({
928934
* openid: 'abc123',
929935
* access_token: '123abc',
930936
* expires_in: 1382686496
931937
* }, 'weixin', 'union123', {
932938
* unionIdPlatform: 'weixin',
933-
* asMainAccount: false,
939+
* asMainAccount: true,
934940
* }).then(function(user) {
935941
* //Access user here
936942
* }).catch(function(error) {
@@ -945,7 +951,8 @@ module.exports = function(AV) {
945951
) {
946952
return this.signUpOrlogInWithAuthData(
947953
mergeUnionDataIntoAuthData(authData, unionId, unionLoginOptions),
948-
platform
954+
platform,
955+
unionLoginOptions
949956
);
950957
},
951958

@@ -1320,9 +1327,9 @@ module.exports = function(AV) {
13201327
}
13211328
},
13221329

1323-
_logInWith: function(provider, options) {
1330+
_logInWith: function(provider, authData, options) {
13241331
var user = AV.Object._create('_User');
1325-
return user._linkWith(provider, options);
1332+
return user._linkWith(provider, authData, options);
13261333
},
13271334
}
13281335
);

test/user.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,20 @@ describe('User', function() {
202202
});
203203

204204
describe('authData and unionId', () => {
205+
const now = Date.now();
206+
it('failOnNotExist', () =>
207+
AV.User.signUpOrlogInWithAuthData(
208+
{
209+
uid: 'openid1' + now,
210+
access_token: 'access_token',
211+
expires_in: 1382686496,
212+
},
213+
'weixin_1',
214+
{
215+
failOnNotExist: true,
216+
}
217+
).should.be.rejectedWith(/Could not find user/));
205218
it('should login as the same user', () => {
206-
const now = Date.now();
207219
return AV.User.signUpOrlogInWithAuthDataAndUnionId(
208220
{
209221
uid: 'openid1' + now,

0 commit comments

Comments
 (0)