|
1 | 1 | const _ = require('underscore'); |
| 2 | +const uuid = require('uuid/v4'); |
2 | 3 | const AVError = require('./error'); |
3 | 4 | const { _request: AVRequest, request } = require('./request'); |
4 | 5 | const Promise = require('./promise'); |
5 | 6 |
|
| 7 | +const PLATFORM_ANONYMOUS = 'anonymous'; |
| 8 | +const PLATFORM_WEAPP = 'lc_weapp'; |
| 9 | + |
6 | 10 | const getWeappLoginCode = () => { |
7 | 11 | if (typeof wx === 'undefined' || typeof wx.login !== 'function') { |
8 | 12 | throw new Error('Weapp Login is only available in Weapp'); |
@@ -249,7 +253,7 @@ module.exports = function(AV) { |
249 | 253 | */ |
250 | 254 | linkWithWeapp() { |
251 | 255 | return getWeappLoginCode().then(code => |
252 | | - this._linkWith('lc_weapp', { code }) |
| 256 | + this._linkWith(PLATFORM_WEAPP, { code }) |
253 | 257 | ); |
254 | 258 | }, |
255 | 259 |
|
@@ -292,6 +296,10 @@ module.exports = function(AV) { |
292 | 296 | return !!authData[authType]; |
293 | 297 | }, |
294 | 298 |
|
| 299 | + isAnonymous() { |
| 300 | + return this._isLinked(PLATFORM_ANONYMOUS); |
| 301 | + }, |
| 302 | + |
295 | 303 | logOut: function() { |
296 | 304 | this._logOutWithAll(); |
297 | 305 | this._isCurrentUser = false; |
@@ -365,6 +373,10 @@ module.exports = function(AV) { |
365 | 373 | } |
366 | 374 |
|
367 | 375 | return this.save(attrs, options).then(function(model) { |
| 376 | + if (model.isAnonymous()) { |
| 377 | + model.unset(`authData.${PLATFORM_ANONYMOUS}`); |
| 378 | + model._opSetQueue = [{}]; |
| 379 | + } |
368 | 380 | return model._handleSaveResult(true).then(function() { |
369 | 381 | return model; |
370 | 382 | }); |
@@ -454,7 +466,7 @@ module.exports = function(AV) { |
454 | 466 | */ |
455 | 467 | loginWithWeapp(options) { |
456 | 468 | return getWeappLoginCode().then(code => |
457 | | - this.loginWithAuthData({ code }, 'lc_weapp', options) |
| 469 | + this.loginWithAuthData({ code }, PLATFORM_WEAPP, options) |
458 | 470 | ); |
459 | 471 | }, |
460 | 472 |
|
@@ -1018,7 +1030,22 @@ module.exports = function(AV) { |
1018 | 1030 | */ |
1019 | 1031 | loginWithWeapp(options) { |
1020 | 1032 | return getWeappLoginCode().then(code => |
1021 | | - this.loginWithAuthData({ code }, 'lc_weapp', options) |
| 1033 | + this.loginWithAuthData({ code }, PLATFORM_WEAPP, options) |
| 1034 | + ); |
| 1035 | + }, |
| 1036 | + |
| 1037 | + /** |
| 1038 | + * Creates an anonymous user. |
| 1039 | + * |
| 1040 | + * @since 3.9.0 |
| 1041 | + * @return {Promise.<AV.User>} |
| 1042 | + */ |
| 1043 | + loginAnonymously() { |
| 1044 | + return this.loginWithAuthData( |
| 1045 | + { |
| 1046 | + id: uuid(), |
| 1047 | + }, |
| 1048 | + 'anonymous' |
1022 | 1049 | ); |
1023 | 1050 | }, |
1024 | 1051 |
|
|
0 commit comments