Skip to content

Commit 88c29a4

Browse files
authored
feat(User): add #isAuthenticated, deprecate #authenticated (#393)
1 parent 95dce0d commit 88c29a4

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/user.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,37 @@ module.exports = function(AV) {
516516

517517
/**
518518
* Checks whether this user is the current user and has been authenticated.
519+
* @deprecated 如果要判断当前用户的登录状态是否有效,请使用 currentUser.isAuthenticated().then(),
520+
* 如果要判断该用户是否是当前登录用户,请使用 user.id === currentUser.id
519521
* @return (Boolean) whether this user is the current user and is logged in.
520522
*/
521523
authenticated: function() {
524+
console.warn('DEPRECATED: 如果要判断当前用户的登录状态是否有效,请使用 currentUser.isAuthenticated().then(),如果要判断该用户是否是当前登录用户,请使用 user.id === currentUser.id。')
522525
return !!this._sessionToken &&
523526
(!AV._config.disableCurrentUser && AV.User.current() && AV.User.current().id === this.id);
524527
},
525528

529+
/**
530+
* 检查该用户的登录状态是否有效,请注意该方法会校验 sessionToken 的有效性,是个异步方法。
531+
*
532+
* @since 2.0.0
533+
* @return Promise.<Boolean>
534+
*/
535+
isAuthenticated() {
536+
return Promise.resolve().then(() =>
537+
!!this._sessionToken &&
538+
AV.User._fetchUserBySessionToken(this._sessionToken).then(
539+
() => true,
540+
(error) => {
541+
if (error.code === 211) {
542+
return false;
543+
}
544+
throw error;
545+
}
546+
)
547+
);
548+
},
549+
526550
/**
527551
* Get sessionToken of current user.
528552
* @return {String} sessionToken

test/user.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe("User", function() {
4444
expect(theUser.get("username")).to.be(username);
4545
});
4646
});
47+
4748

4849
it("should fail with wrong password", function() {
4950
return AV.User.logIn(username, 'wrong password')
@@ -57,12 +58,32 @@ describe("User", function() {
5758

5859

5960
describe("Current User", function() {
60-
it("should return current user", function() {
61-
61+
it("current()", function() {
6262
var currentUser = AV.User.current();
6363
expect(currentUser).to.be.ok();
64-
return AV.User.currentAsync().then(function(user) {
64+
});
65+
it('currentAsync()', () =>
66+
AV.User.currentAsync().then(function(user) {
6567
expect(user).to.be.ok();
68+
})
69+
);
70+
});
71+
72+
describe('authenticated', () => {
73+
it('authenticated()', () => {
74+
AV.User.current().authenticated().should.be.ok();
75+
new AV.User().authenticated().should.not.be.ok();
76+
});
77+
describe('isAuthenticated', () => {
78+
it('currentUser.isAuthenticated()', () =>
79+
AV.User.current().isAuthenticated().should.be.fulfilledWith(true)
80+
);
81+
it('user.isAuthenticated()', () =>
82+
new AV.User().isAuthenticated().should.be.fulfilledWith(false)
83+
);
84+
it('outdated sessionToken', () => {
85+
AV.User.current()._sessionToken = '0';
86+
return new AV.User.current().isAuthenticated().should.be.fulfilledWith(false)
6687
});
6788
});
6889
});

0 commit comments

Comments
 (0)