Skip to content

Commit

Permalink
fix(refresh): this.scheme instead of this.$scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasCiarlo committed Mar 8, 2020
1 parent 5a8a167 commit 9fef2a2
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/core/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RefreshController {

_doRefresh () {
this.refreshPromise = new Promise(resolve => {
this.$scheme._refreshToken().then(response => {
this.scheme._refreshToken().then(response => {
this.refreshPromise = null
resolve(response)
})
Expand All @@ -34,7 +34,7 @@ export class RefreshController {

initializeAutomaticRefresh () {
// If auto refresh is enabled, schedule token refresh
if (this.$scheme.options.autoRefresh.enable) {
if (this.scheme.options.autoRefresh.enable) {
this.initializeScheduledRefresh()
return
}
Expand All @@ -49,10 +49,10 @@ export class RefreshController {
// ---------------------------------------------------------------
initializeScheduledRefresh () {
// Sync tokens
this.$scheme._setToken(this.$scheme.$auth.syncToken(this.$scheme.name))
this.$scheme.$auth.syncRefreshToken(this.$scheme.name)
this.scheme._setToken(this.scheme.$auth.syncToken(this.scheme.name))
this.scheme.$auth.syncRefreshToken(this.scheme.name)

let intervalDuration = (this.$scheme._getTokenExpiration() - Date.now()) * 0.75
let intervalDuration = (this.scheme._getTokenExpiration() - Date.now()) * 0.75
if (intervalDuration < 1000) {
// in case you misconfigured refreshing this will save your auth-server from a self-induced DDoS-Attack
intervalDuration = 1000
Expand All @@ -68,18 +68,18 @@ export class RefreshController {
// Refresh tokens if token has expired
// ---------------------------------------------------------------
initializeRequestInterceptor (refreshEndpoint) {
this.$scheme.$auth.ctx.app.$axios.onRequest(async config => {
this.scheme.$auth.ctx.app.$axios.onRequest(async config => {
// Don't intercept refresh token requests
if (config.url === refreshEndpoint) {
return config
}

// Sync tokens
const token = this.$scheme.$auth.syncToken(this.$scheme.name) || false
const refreshToken = this.$scheme.$auth.syncRefreshToken(this.$scheme.name)
this.$scheme._setToken(token)
this.$scheme._syncTokenExpiration()
this.$scheme._syncRefreshTokenExpiration()
const token = this.scheme.$auth.syncToken(this.scheme.name) || false
const refreshToken = this.scheme.$auth.syncRefreshToken(this.scheme.name)
this.scheme._setToken(token)
this.scheme._syncTokenExpiration()
this.scheme._syncRefreshTokenExpiration()

// If no token or no refresh token, bail
if (!token || !refreshToken) {
Expand All @@ -93,16 +93,16 @@ export class RefreshController {
}

// Sync token expiration status
this.$scheme.tokenStatus.syncStatus()
this.scheme.tokenStatus.syncStatus()

// Token is still valid, let the request pass
if (this.$scheme.tokenStatus.valid() || this.$scheme.tokenStatus.unknown()) {
if (this.scheme.tokenStatus.valid() || this.scheme.tokenStatus.unknown()) {
return this.getUpdatedRequestConfig(config)
}

// Refresh token has also expired. There is no way to refresh. Force reset.
if (this.$scheme.tokenStatus.refreshExpired()) {
await this.$scheme.$auth.reset()
if (this.scheme.tokenStatus.refreshExpired()) {
await this.scheme.$auth.reset()

throw new ExpiredAuthSessionError()
}
Expand All @@ -120,12 +120,12 @@ export class RefreshController {
}

getUpdatedRequestConfig (config) {
config.headers[this.$scheme.options.tokenName] = this.$scheme.$auth.getToken(this.$scheme.name)
config.headers[this.scheme.options.tokenName] = this.scheme.$auth.getToken(this.scheme.name)
return config
}

requestHasAuthorizationHeader (config) {
return !!config.headers.common[this.$scheme.options.tokenName]
return !!config.headers.common[this.scheme.options.tokenName]
}
}

Expand Down

0 comments on commit 9fef2a2

Please sign in to comment.