Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit fb56bed

Browse files
committed
Allow expiresIn to accept a date object
Closes #38
1 parent 355bd77 commit fb56bed

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/client-oauth2.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ ClientOAuth2.prototype.createToken = function (access, refresh, type, data) {
243243
typeof type === 'string' ? { token_type: type } : type
244244
)
245245

246-
return new ClientOAuth2Token(this, options)
246+
return new ClientOAuth2.Token(this, options)
247247
}
248248

249249
/**
@@ -296,21 +296,23 @@ function ClientOAuth2Token (client, data) {
296296
this.accessToken = data.access_token
297297
this.refreshToken = data.refresh_token
298298

299-
this.expiresIn(data.expires_in)
299+
this.expiresIn(Number(data.expires_in))
300300
}
301301

302302
/**
303-
* Expire after some seconds.
303+
* Expire the token after some time.
304304
*
305-
* @param {Number} duration
305+
* @param {Number|Date} duration Seconds from now to expire, or a date to expire on.
306306
* @return {Date}
307307
*/
308308
ClientOAuth2Token.prototype.expiresIn = function (duration) {
309-
if (!isNaN(duration)) {
309+
if (typeof duration === 'number') {
310310
this.expires = new Date()
311-
this.expires.setSeconds(this.expires.getSeconds() + Number(duration))
311+
this.expires.setSeconds(this.expires.getSeconds() + duration)
312+
} else if (duration instanceof Date) {
313+
this.expires = new Date(duration.getTime())
312314
} else {
313-
this.expires = undefined
315+
throw new TypeError('Unknown duration: ' + duration)
314316
}
315317

316318
return this.expires
@@ -360,7 +362,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
360362
options = extend(this.client.options, options)
361363

362364
if (!this.refreshToken) {
363-
return Promise.reject(new Error('No refresh token set'))
365+
return Promise.reject(new Error('No refresh token'))
364366
}
365367

366368
return this.client._request(requestOptions({
@@ -385,11 +387,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
385387
* @return {Boolean}
386388
*/
387389
ClientOAuth2Token.prototype.expired = function () {
388-
if (this.expires) {
389-
return Date.now() > this.expires.getTime()
390-
}
391-
392-
return false
390+
return Date.now() > this.expires.getTime()
393391
}
394392

395393
/**

0 commit comments

Comments
 (0)