Async firebase call inside vuejs2 http interceptors

110 Views Asked by At

Using Firebase authentication and VueJS2 HTTP interceptors I want to add the IDToken to the header if the request so I don't have to manually do it everytime. I'm sending the request to a NodeJS API so I can see the headers of the resquest once it gets there. Here's the part of the main.js where I push the interceptor :

Vue.http.interceptors.push((request, next) => {
  auth.currentUser
    .getIdToken()
    .then(token => {
      request.headers.set("Authorization", token);
      request.headers.set("Accept", "application/json");
      next();
    })
    .catch(e => {
      console.log(e);
    });
});

If I do it as written above, the resquest is sent but the header is not set. If I do something like this then the header is sent :

Vue.http.interceptors.push((request, next) => {
  request.headers.set("Authorization", "TEST");
  request.headers.set("Accept", "application/json");
  next();
});

So I would think that it is a problem with the fact that the getIdToken() method is async, but I see no way to make it work unless I take that code outside of the intercptor, something I would like to avoid. Any idea on how to do this properly?

0

There are 0 best solutions below