Skip to content

Commit

Permalink
fix(refresh scheme): wait for refresh token request, even if token ha…
Browse files Browse the repository at this point in the history
…sn't expired
  • Loading branch information
JoaoPedroAS51 committed Jul 12, 2019
1 parent e8ef414 commit 080628f
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions lib/schemes/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ export default class RefreshScheme extends LocalScheme {
refreshTokenExpiration = new Date(refreshTokenExpiration).setMilliseconds(0)
}

if (now >= tokenExpiration) {
if (now < refreshTokenExpiration) {
if (!this.isRefreshing) {
if (!this.isRefreshing) {
if (now >= tokenExpiration) {
if (now < refreshTokenExpiration) {
// Refresh the token
return this._tokenRefresh().then(() => {
this._scheduleTokenRefresh()
Expand All @@ -214,21 +214,21 @@ export default class RefreshScheme extends LocalScheme {
config.headers[this.options.tokenName] = this.$auth.getToken(this.name)
return Promise.resolve(config)
}).catch(error => Promise.reject(error))
} else if (config.url !== this.options.endpoints.refresh.url) {
return new Promise(async (resolve, reject) => {
let waitForRefresh
await (waitForRefresh = () => {
if (!this.isRefreshing) {
config.headers[this.options.tokenName] = this.$auth.getToken(this.name)
return resolve(config)
}
setTimeout(waitForRefresh, 30)
})()
})
} else {
this._logoutLocally()
}
} else {
this._logoutLocally()
}
} else if (config.url !== this.options.endpoints.refresh.url) {
return new Promise(async (resolve, reject) => {
let waitForRefresh
await (waitForRefresh = () => {
if (!this.isRefreshing) {
config.headers[this.options.tokenName] = this.$auth.getToken(this.name)
return resolve(config)
}
setTimeout(waitForRefresh, 30)
})()
})
}

return config
Expand Down Expand Up @@ -267,11 +267,9 @@ export default class RefreshScheme extends LocalScheme {

// Only refresh token if user is logged in and is client side
if (process.client && this.$auth.loggedIn && this.options.autoRefresh.enable) {
setTimeout(() => {
this._tokenRefresh().then(() => {
this._scheduleTokenRefresh()
})
}, 1000)
this._tokenRefresh().then(() => {
this._scheduleTokenRefresh()
})
}
})
}
Expand Down

0 comments on commit 080628f

Please sign in to comment.