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

Commit

Permalink
Allow expiresIn to accept a date object
Browse files Browse the repository at this point in the history
Closes #38
  • Loading branch information
blakeembrey committed Nov 7, 2016
1 parent 355bd77 commit fb56bed
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/client-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ ClientOAuth2.prototype.createToken = function (access, refresh, type, data) {
typeof type === 'string' ? { token_type: type } : type
)

return new ClientOAuth2Token(this, options)
return new ClientOAuth2.Token(this, options)
}

/**
Expand Down Expand Up @@ -296,21 +296,23 @@ function ClientOAuth2Token (client, data) {
this.accessToken = data.access_token
this.refreshToken = data.refresh_token

this.expiresIn(data.expires_in)
this.expiresIn(Number(data.expires_in))
}

/**
* Expire after some seconds.
* Expire the token after some time.
*
* @param {Number} duration
* @param {Number|Date} duration Seconds from now to expire, or a date to expire on.
* @return {Date}
*/
ClientOAuth2Token.prototype.expiresIn = function (duration) {
if (!isNaN(duration)) {
if (typeof duration === 'number') {
this.expires = new Date()
this.expires.setSeconds(this.expires.getSeconds() + Number(duration))
this.expires.setSeconds(this.expires.getSeconds() + duration)
} else if (duration instanceof Date) {
this.expires = new Date(duration.getTime())
} else {
this.expires = undefined
throw new TypeError('Unknown duration: ' + duration)
}

return this.expires
Expand Down Expand Up @@ -360,7 +362,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
options = extend(this.client.options, options)

if (!this.refreshToken) {
return Promise.reject(new Error('No refresh token set'))
return Promise.reject(new Error('No refresh token'))
}

return this.client._request(requestOptions({
Expand All @@ -385,11 +387,7 @@ ClientOAuth2Token.prototype.refresh = function (options) {
* @return {Boolean}
*/
ClientOAuth2Token.prototype.expired = function () {
if (this.expires) {
return Date.now() > this.expires.getTime()
}

return false
return Date.now() > this.expires.getTime()
}

/**
Expand Down

0 comments on commit fb56bed

Please sign in to comment.