How to setToken with axios-module for multiple API's

samuells
0
samuells
commented a year ago

Hi,

I have 2 api, from which I need to get data. Both of them are using Bearer authentication token - but this token is different for each api. How can I set token to the axios, if I am using proxy to switch baseURL, but the token is set on the axios instance?

0
hqqxxf
0
hqqxxf
commented a year ago

I have the same question

0
yoann54
0
yoann54
commented a year ago

Hello,

Did u find a solution to this problem ? i have exactly the same and i don't find any docs on how to manage multiple api at the same time .

0
diegotejadav
0
diegotejadav
commented a year ago

@samuells @hqqxxf @yoann54, same question here, did some of you find any solution or example?

0
samuells
0
samuells
commented a year ago

No luck .. I didn't dig more on this.

0
llavre
5
llavre
commented a year ago

@samuells why did you don't create two axios instances?

In my case one for public and one for private apis.

0
samuells
0
samuells
commented a year ago

It was a long time ago … but I was trying achieve using nuxt settings file .. not sure anymore. Sorry

0
LTroya
0
LTroya
commented 7 months ago

@llavre how can you create two instances of $axios? axios has a create method for doing that

0
llavre
5
llavre
commented 7 months ago

@LTroya, not sure if I get your question right, but you should create two axios instances and then inject it in to context trough nuxt plugin or straight-forward prorotype in to Vue:

export default (ctx, inject) => {
  let privateAPI = axios.create({ baseURL: API_URL })
  Vue.prototype.$privateAPI = privateAPI
  ctx.privateAPI = privateAPI
  inject('privateAPI', privateAPI)
}

Then you can use it in your components and/or store.
https://nuxtjs.org/guide/plugins

1
cranx
0
cranx
commented 4 months ago

According to docs:

If you need the function in the context, Vue instances and maybe even in the Vuex store, you can use the inject function, which is the second parameter of the plugins exported function.

So just inject should be enough

0
Informations
QuestionUnresolved
#c3 - Created a year ago