Skip to content

Commit 528c15d

Browse files
committed
feat(user): add anonymously login API
1 parent a2cce49 commit 528c15d

File tree

5 files changed

+67
-27
lines changed

5 files changed

+67
-27
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"localstorage-memory": "^1.0.1",
3636
"md5": "^2.0.0",
3737
"superagent": "^3.3.1",
38-
"underscore": "^1.8.3"
38+
"underscore": "^1.8.3",
39+
"uuid": "^3.3.2"
3940
},
4041
"devDependencies": {
4142
"babel-core": "^6.4.0",

src/av.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require('underscore');
2+
const uuid = require('uuid/v4');
23
const userAgent = require('./ua');
34
const { inherits, parseDate } = require('./utils');
45
const Promise = require('./promise');
@@ -48,13 +49,6 @@ AV._getAVPath = function(path) {
4849
return 'AV/' + AV.applicationId + '/' + path;
4950
};
5051

51-
const hexOctet = () =>
52-
Math.floor((1 + Math.random()) * 0x10000)
53-
.toString(16)
54-
.substring(1);
55-
const uuid = () =>
56-
`${hexOctet()}${hexOctet()}-${hexOctet()}-${hexOctet()}-${hexOctet()}-${hexOctet()}${hexOctet()}${hexOctet()}`;
57-
5852
/**
5953
* Returns the unique string for this app on this machine.
6054
* Gets reset when localStorage is cleared.

src/user.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const _ = require('underscore');
2+
const uuid = require('uuid/v4');
23
const AVError = require('./error');
34
const { _request: AVRequest, request } = require('./request');
45
const Promise = require('./promise');
56

7+
const PLATFORM_ANONYMOUS = 'anonymous';
8+
const PLATFORM_WEAPP = 'lc_weapp';
9+
610
const getWeappLoginCode = () => {
711
if (typeof wx === 'undefined' || typeof wx.login !== 'function') {
812
throw new Error('Weapp Login is only available in Weapp');
@@ -249,7 +253,7 @@ module.exports = function(AV) {
249253
*/
250254
linkWithWeapp() {
251255
return getWeappLoginCode().then(code =>
252-
this._linkWith('lc_weapp', { code })
256+
this._linkWith(PLATFORM_WEAPP, { code })
253257
);
254258
},
255259

@@ -292,6 +296,10 @@ module.exports = function(AV) {
292296
return !!authData[authType];
293297
},
294298

299+
isAnonymous() {
300+
return this._isLinked(PLATFORM_ANONYMOUS);
301+
},
302+
295303
logOut: function() {
296304
this._logOutWithAll();
297305
this._isCurrentUser = false;
@@ -365,6 +373,10 @@ module.exports = function(AV) {
365373
}
366374

367375
return this.save(attrs, options).then(function(model) {
376+
if (model.isAnonymous()) {
377+
model.unset(`authData.${PLATFORM_ANONYMOUS}`);
378+
model._opSetQueue = [{}];
379+
}
368380
return model._handleSaveResult(true).then(function() {
369381
return model;
370382
});
@@ -454,7 +466,7 @@ module.exports = function(AV) {
454466
*/
455467
loginWithWeapp(options) {
456468
return getWeappLoginCode().then(code =>
457-
this.loginWithAuthData({ code }, 'lc_weapp', options)
469+
this.loginWithAuthData({ code }, PLATFORM_WEAPP, options)
458470
);
459471
},
460472

@@ -1018,7 +1030,22 @@ module.exports = function(AV) {
10181030
*/
10191031
loginWithWeapp(options) {
10201032
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'
10221049
);
10231050
},
10241051

storage.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ export class User extends Object {
636636
static logOut(): Promise<User>;
637637
static become(sessionToken: string): Promise<User>;
638638

639+
static loginAnonymously(): Promise<User>;
639640
static loginWithWeapp(options?: OAuthLoginOptions): Promise<User>;
640641
static logInWithMobilePhone(
641642
mobilePhone: string,
@@ -720,6 +721,7 @@ export class User extends Object {
720721
logIn(): Promise<User>;
721722
linkWithWeapp(): Promise<User>;
722723
isAuthenticated(): Promise<boolean>;
724+
isAnonymous(): boolean;
723725
isCurrent(): boolean;
724726

725727
associateWithAuthData(authData: Object, platform: string): Promise<User>;

test/user.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,38 @@ describe('User', function() {
182182
});
183183
});
184184

185-
describe('User logInAnonymously', function() {
186-
it('should create anonymous user, and login with AV.User.signUpOrlogInWithAuthData()', function() {
187-
var getFixedId = function() {
188-
var rawId = 13334230101333423010;
189-
var result = rawId.toString(16);
190-
return result;
191-
};
192-
var data = {
193-
id: getFixedId(),
194-
};
195-
196-
return AV.User.signUpOrlogInWithAuthData(data, 'anonymous').then(function(
197-
user
198-
) {
199-
expect(user.id).to.be.ok();
200-
});
185+
describe('User loginAnonymously', function() {
186+
it('create an anonymous user, and then associateWithAuthData', function() {
187+
return AV.User.loginAnonymously()
188+
.then(function(user) {
189+
expect(user.id).to.be.ok();
190+
expect(user.isAnonymous()).to.be.ok();
191+
return user.associateWithAuthData(
192+
{
193+
uid: Date.now().toString(36),
194+
access_token: 'access_token',
195+
},
196+
'github'
197+
);
198+
})
199+
.then(user => {
200+
expect(user.isAnonymous()).to.be.equal(false);
201+
expect(user.dirty()).to.be.equal(false);
202+
});
203+
});
204+
it('create an anonymous user, and then signup', function() {
205+
return AV.User.loginAnonymously()
206+
.then(function(user) {
207+
expect(user.id).to.be.ok();
208+
expect(user.isAnonymous()).to.be.ok();
209+
const name = Date.now().toString(36);
210+
user.setUsername(name).setPassword(name);
211+
return user.signUp();
212+
})
213+
.then(user => {
214+
expect(user.isAnonymous()).to.be.equal(false);
215+
expect(user.dirty()).to.be.equal(false);
216+
});
201217
});
202218
});
203219

0 commit comments

Comments
 (0)