|
@@ -1,7 +1,7 @@
|
|
|
import { boot } from "quasar/wrappers";
|
|
import { boot } from "quasar/wrappers";
|
|
|
import { useAuth } from "src/composables/useAuth";
|
|
import { useAuth } from "src/composables/useAuth";
|
|
|
import { Cookies, Notify } from "quasar";
|
|
import { Cookies, Notify } from "quasar";
|
|
|
-import axios from "axios";
|
|
|
|
|
|
|
+import axios, { AxiosError } from "axios";
|
|
|
// Be careful when using SSR for cross-request state pollution
|
|
// Be careful when using SSR for cross-request state pollution
|
|
|
// due to creating a Singleton instance here;
|
|
// due to creating a Singleton instance here;
|
|
|
// If any client changes this (global) instance, it might be a
|
|
// If any client changes this (global) instance, it might be a
|
|
@@ -9,16 +9,6 @@ import axios from "axios";
|
|
|
// "export default () => {}" function below (which runs individually
|
|
// "export default () => {}" function below (which runs individually
|
|
|
// for each client)
|
|
// for each client)
|
|
|
|
|
|
|
|
-let isRefreshing = false;
|
|
|
|
|
-let failedQueue = [];
|
|
|
|
|
-
|
|
|
|
|
-const processQueue = (error, token = null) => {
|
|
|
|
|
- failedQueue.forEach((prom) =>
|
|
|
|
|
- error ? prom.reject(error) : prom.resolve(token),
|
|
|
|
|
- );
|
|
|
|
|
- failedQueue = [];
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
const api = axios.create({
|
|
const api = axios.create({
|
|
|
baseURL: process.env.API_URL + "/api",
|
|
baseURL: process.env.API_URL + "/api",
|
|
|
withCredentials: true,
|
|
withCredentials: true,
|
|
@@ -40,40 +30,39 @@ api.interceptors.request.use(
|
|
|
},
|
|
},
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
-const errorInterceptor = async (error) => {
|
|
|
|
|
- if (error.response.status === 401) {
|
|
|
|
|
- if (!isRefreshing) {
|
|
|
|
|
- isRefreshing = true;
|
|
|
|
|
- const isRefreshValid = await useAuth().refreshToken();
|
|
|
|
|
- isRefreshing = false;
|
|
|
|
|
- if (!isRefreshValid) {
|
|
|
|
|
- Cookies.remove("access_token");
|
|
|
|
|
- Cookies.remove("refresh_token");
|
|
|
|
|
- window.location.href = "/login";
|
|
|
|
|
- processQueue(new Error("Token refresh failed"), null);
|
|
|
|
|
- return Promise.reject(new Error("Token refresh failed"));
|
|
|
|
|
- } else {
|
|
|
|
|
- processQueue(null, true);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
|
- failedQueue.push({ resolve, reject });
|
|
|
|
|
- })
|
|
|
|
|
- .then(() => {
|
|
|
|
|
- return api.request(error.config);
|
|
|
|
|
- })
|
|
|
|
|
- .catch((err) => {
|
|
|
|
|
- return Promise.reject(err);
|
|
|
|
|
|
|
+const errorInterceptor = (error) => {
|
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
|
+ if (error.response.status === 401) {
|
|
|
|
|
+ useAuth()
|
|
|
|
|
+ .refreshToken()
|
|
|
|
|
+ .then((response) => {
|
|
|
|
|
+ if (response instanceof AxiosError) {
|
|
|
|
|
+ Cookies.remove("access_token");
|
|
|
|
|
+ Cookies.remove("refresh_token");
|
|
|
|
|
+ window.location.href = "/login";
|
|
|
|
|
+ reject(response);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ api
|
|
|
|
|
+ .request(error.config)
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ resolve(res);
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((apiError) => {
|
|
|
|
|
+ reject(apiError);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((refreshError) => {
|
|
|
|
|
+ reject(refreshError);
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Notify.create({
|
|
|
|
|
+ message: error.response.data.message,
|
|
|
|
|
+ type: "negative",
|
|
|
});
|
|
});
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Notify.create({
|
|
|
|
|
- message: error.response.data.message,
|
|
|
|
|
- type: "negative",
|
|
|
|
|
|
|
+ reject(error);
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
- return Promise.reject(error);
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const successInterceptor = (response) => {
|
|
const successInterceptor = (response) => {
|