nuxt-community / axios-module

Secure and easy axios integration for Nuxt 2
https://axios.nuxtjs.org
MIT License
1.19k stars 245 forks source link

call two different api in interceptor #488

Open bagaskarala opened 3 years ago

bagaskarala commented 3 years ago

i setup interceptor like this, there is two apis with two different token, so i handle using url.

  $axios.onRequest((config) => {
    // #1 api
    if (config.url.startsWith('/first-api')) {
      const imersToken = Vue.$cookies.get($config.firstToken)
      $axios.setToken(firstToken, 'Bearer')
    }

    // #2 api
    if (config.url.startsWith('/second-api')) {
      const evmToken = Vue.$cookies.get($config.secondToken)
      $axios.setToken(secondToken, 'Bearer')
    }
  })

but when i call first-api and second-api at the same time, some of them doesn't populate with the token so it result in Error 401. it will fine if i hit same api at the same time. is this bug or it is my worst practice?

ajingopi-bridge commented 3 years ago

@bagaskarala You may update it like below,

  $axios.onRequest((config) => {
    // #1 api
    if (config.url.startsWith('/first-api')) {
      const imersToken = Vue.$cookies.get($config.firstToken)
      config.headers.Authorization = `Bearer ${imersToken}`
    }

    // #2 api
    if (config.url.startsWith('/second-api')) {
      const evmToken = Vue.$cookies.get($config.secondToken)
      config.headers.Authorization = `Bearer ${evmToken}`
    }
  })