nickezekias / zfinance-front

Vue3 + Primevue zFinance User App
0 stars 0 forks source link

Fix store pinia store injection error in lib/axios.ts #3

Open nickezekias opened 2 months ago

nickezekias commented 2 months ago

In lib/axios.ts I'm currently using axios.interceptors.response to detect a 401 or 419 response to trigger a logout system wide, but I have a problem with the way authStore is injected. It gets used before pinia is injected into vue app.

Find a way either using a plugin or creating a service class and injecting axios as library in store files to make sure pinia is injected before axios is used.

nickezekias commented 2 months ago

Here is the problematic code:

axios.interceptors.response.use(
  (response) => {
    return response;
  },
  async function (error) {
    if (
      error.response &&
      [401, 419].includes(error.response.status)
    ) {
      const authStore = useAuthStore() // PROBLEM CODE TO FIX
      await authStore.logout()
    }
    return Promise.reject(error);
  }
);

authStore is used before pinia store is injected into vue app