Skip to content

Axios Interceptor Refresh Token called multiple times on parallel axios request #1743

@tokidoki11

Description

@tokidoki11

Version

module: "5.0.0-1643791578.532b3d6"
nuxt: ^2.15.7

Nuxt configuration

mode:

  • universal
  • spa

Nuxt configuration

Reproduction

  1. in the cookie, update token expiry to 1 (making the token expired)
  2. Run axios request on parallel

What is expected?

  1. RequestToken is only run once

What is actually happening?

  1. Refresh Token runs twice

image

Additional information

I have separate axios instance for calling API other than auth0

const internalAxios = $axios.create()
internalAxios.onRequest(async (config: AxiosRequestConfig): Promise<AxiosRequestConfig> => {
    const auth0Strategy = app.$auth.strategy as Auth0Scheme
    if (!auth0Strategy.token.status().valid()) {
      if (auth0Strategy.refreshToken.status().valid()) {
        await app.$auth.refreshTokens()
      } else {
        app.$auth.reset()
        app.$auth.loginWith('auth0')
      }
    }
    config.headers.authorization = decodeURI(auth0Strategy.token.get() as string)
    return config
  })

Calling refreshToken via $auth will call the refresh Token only once
On further investigation calling refreshToken via $auth will trigger

refreshTokens(): Promise<HTTPResponse | void> {
if (!(this.getStrategy() as RefreshableScheme).refreshController) {
return Promise.resolve()
}
return Promise.resolve(
(
this.getStrategy() as RefreshableScheme
).refreshController.handleRefresh()
).catch((error) => {
this.callOnError(error, { method: 'refreshTokens' })
return Promise.reject(error)
})
}

handleRefresh(): Promise<HTTPResponse | void> {
// Another request has started refreshing the token, wait for it to complete
if (this._refreshPromise) {
return this._refreshPromise
}
return this._doRefresh()
}

But the interceptor will call this in which it doesnt check whether refresh Token is still ongoing or not

isValid = await (this.scheme as RefreshableScheme)
.refreshTokens()
.then(() => true)
.catch(() => {
// Tokens couldn't be refreshed. Force reset.
this.scheme.reset()
throw new ExpiredAuthSessionError()
})

async refreshTokens(): Promise<HTTPResponse | void> {

Checklist

  • I have tested with the latest Nuxt version and the issue still occurs
  • I have tested with the latest module version and the issue still occurs
  • I have searched the issue tracker and this issue hasn't been reported yet

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions