Skip to content

Commit 9a9cc91

Browse files
committed
don't register expriring event if access token already expired
1 parent 003910d commit 9a9cc91

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎src/AccessTokenEvents.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,30 @@ const DefaultExpiringNotificationTime = 60;
99
export default class AccessTokenEvents {
1010

1111
constructor({
12-
expiringNotificationTime = DefaultExpiringNotificationTime,
12+
expiringNotificationTime = DefaultExpiringNotificationTime,
1313
expiringTimer = new Timer(),
1414
expiredTimer = new Timer()
1515
} = {}) {
1616
this._expiringNotificationTime = expiringNotificationTime;
17-
17+
1818
this._expiring = expiringTimer;
1919
this._expired = expiredTimer;
2020
}
2121

2222
init(container) {
2323
this.cancel();
24-
25-
if (container && container.access_token) {
26-
var duration = container.expires_in;
24+
25+
// only register events if there's an access token where we care about expiration
26+
if (container.access_token) {
27+
let duration = container.expires_in;
28+
29+
let expiring = duration - this._expiringNotificationTime;
30+
if (expiring > 0) {
31+
// only register expiring if we still have time
32+
this._expiring.init(expiring);
33+
}
2734

28-
this._expiring.init(duration - this._expiringNotificationTime);
35+
// always register expired. if it's negative, it will still fire
2936
this._expired.init(duration + 1);
3037
}
3138
}

‎test/unit/AccessTokenEvents.spec.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("AccessTokenEvents", function () {
5050
describe("init", function () {
5151

5252
it("should cancel existing timers", function () {
53-
subject.init();
53+
subject.init({});
5454

5555
expiringTimer.cancelWasCalled.should.be.true;
5656
expiredTimer.cancelWasCalled.should.be.true;
@@ -65,6 +65,33 @@ describe("AccessTokenEvents", function () {
6565
expiringTimer.duration.should.equal(10);
6666
expiredTimer.duration.should.equal(71);
6767
});
68+
69+
it("should not initialize expiring timer if already expired", function () {
70+
subject.init({
71+
access_token:"token",
72+
expires_in : 0
73+
});
74+
75+
assert.isUndefined(expiringTimer.duration);
76+
});
77+
78+
it("should initialize expired timer if already expired", function () {
79+
subject.init({
80+
access_token:"token",
81+
expires_in : 0
82+
});
83+
84+
expiredTimer.duration.should.equal(1);
85+
});
86+
87+
it("should not initialize timers if no access token", function () {
88+
subject.init({
89+
expires_in : 70
90+
});
91+
92+
assert.isUndefined(expiringTimer.duration);
93+
assert.isUndefined(expiredTimer.duration);
94+
});
6895

6996
});
7097

0 commit comments

Comments
 (0)