Skip to content

Commit a238b6d

Browse files
author
ayaya
committed
remember created_at and expires_in
1 parent 124bfe7 commit a238b6d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

dist/lib/AccessToken.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var AccessToken = (function () {
2222
function AccessToken(client, _ref) {
2323
var access_token = _ref.access_token;
2424
var refresh_token = _ref.refresh_token;
25+
var created_at = _ref.created_at;
26+
var expires_in = _ref.expires_in;
2527
var scope = _ref.scope;
2628

2729
_classCallCheck(this, AccessToken);
@@ -30,6 +32,8 @@ var AccessToken = (function () {
3032
this.client = client;
3133
this.accessToken = access_token; // eslint-disable-line camelcase
3234
this.refreshToken = refresh_token; // eslint-disable-line camelcase
35+
this.createdAt = created_at; // eslint-disable-line camelcase
36+
this.expiresIn = expires_in; // eslint-disable-line camelcase
3337
this.scope = scope;
3438
this.http = this.client.http.set('Authorization', 'Bearer ' + this.accessToken);
3539
_lodash2['default'].assign(this, this.client.accessTokenMixin);
@@ -41,9 +45,18 @@ var AccessToken = (function () {
4145
return {
4246
access_token: this.accessToken, // eslint-disable-line camelcase
4347
refresh_token: this.refreshToken, // eslint-disable-line camelcase
48+
created_at: createdAt, // eslint-disable-line camelcase
49+
expires_in: expiresIn, // eslint-disable-line camelcase
4450
scope: this.scope
4551
};
4652
}
53+
}, {
54+
key: 'isExpired',
55+
value: function isExpired() {
56+
var expiresAt = (this.createdAt + this.expiresIn) * 1000;
57+
var now = new Date().getTime();
58+
return expiresAt < now;
59+
}
4760
}, {
4861
key: 'refresh',
4962
value: function refresh() {

lib/AccessToken.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import _ from 'lodash';
22
import Promise from 'promise';
33

44
class AccessToken {
5-
constructor(client, { access_token, refresh_token, scope }) { // eslint-disable-line camelcase
5+
constructor(client, { access_token, refresh_token, created_at, expires_in, scope }) { // eslint-disable-line camelcase
66
this.client = client;
77
this.accessToken = access_token; // eslint-disable-line camelcase
88
this.refreshToken = refresh_token; // eslint-disable-line camelcase
9+
this.createdAt = created_at; // eslint-disable-line camelcase
10+
this.expiresIn = expires_in; // eslint-disable-line camelcase
911
this.scope = scope;
1012
this.http = this.client.http.set('Authorization', `Bearer ${this.accessToken}`);
1113
_.assign(this, this.client.accessTokenMixin);
@@ -15,10 +17,18 @@ class AccessToken {
1517
return {
1618
access_token: this.accessToken, // eslint-disable-line camelcase
1719
refresh_token: this.refreshToken, // eslint-disable-line camelcase
20+
created_at: createdAt, // eslint-disable-line camelcase
21+
expires_in: expiresIn, // eslint-disable-line camelcase
1822
scope: this.scope
1923
};
2024
}
2125

26+
isExpired() {
27+
const expiresAt = (this.createdAt + this.expiresIn) * 1000;
28+
const now = new Date().getTime();
29+
return expiresAt < now;
30+
}
31+
2232
refresh() {
2333
if (this.refreshToken) {
2434
return this.client.refreshToken().getToken(this.refreshToken);

0 commit comments

Comments
 (0)