2024-08-22 09:48:04 +00:00
|
|
|
import axios from "axios";
|
2024-11-21 14:58:58 +00:00
|
|
|
import { isAuthenticatedPromise, type Payload } from "./router/authGuard";
|
2024-08-26 11:46:54 +00:00
|
|
|
import router from "./router";
|
2024-12-19 10:16:05 +00:00
|
|
|
import { useNotificationStore } from "./stores/notification";
|
2024-12-31 13:23:42 +00:00
|
|
|
import { EventSourcePolyfill } from "event-source-polyfill";
|
2025-01-06 14:04:47 +00:00
|
|
|
import { config } from "./config";
|
2024-08-22 09:48:04 +00:00
|
|
|
|
2024-08-25 11:37:23 +00:00
|
|
|
let devMode = process.env.NODE_ENV === "development";
|
|
|
|
|
2025-01-06 15:10:47 +00:00
|
|
|
let host = devMode ? "localhost:5000" : (config.server_address ?? "").replace(/(^\w+:|^)\/\//, "");
|
|
|
|
let url = devMode ? "http://" + host : config.server_address;
|
2024-12-05 15:37:10 +00:00
|
|
|
|
2024-08-22 09:48:04 +00:00
|
|
|
const http = axios.create({
|
2024-12-05 15:37:10 +00:00
|
|
|
baseURL: url + "/api",
|
2024-08-22 09:48:04 +00:00
|
|
|
headers: {
|
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
Pragma: "no-cache",
|
|
|
|
Expires: "0",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
http.interceptors.request.use(
|
|
|
|
(config) => {
|
|
|
|
const token = localStorage.getItem("accessToken");
|
|
|
|
if (token) {
|
|
|
|
if (config.headers) {
|
|
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-11 13:45:48 +00:00
|
|
|
const isPWA =
|
|
|
|
window.matchMedia("(display-mode: standalone)").matches ||
|
|
|
|
window.matchMedia("(display-mode: fullscreen)").matches;
|
|
|
|
if (isPWA) {
|
|
|
|
if (config.headers) {
|
|
|
|
config.headers["X-PWA-Client"] = isPWA ? "true" : "false";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-22 09:48:04 +00:00
|
|
|
return config;
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
http.interceptors.response.use(
|
|
|
|
(response) => {
|
|
|
|
return response;
|
|
|
|
},
|
|
|
|
async (error) => {
|
2024-12-19 11:32:02 +00:00
|
|
|
if (!error.config.url.includes("/admin") && !error.config.url.includes("/user")) {
|
2024-08-23 12:42:32 +00:00
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
|
2024-08-22 09:48:04 +00:00
|
|
|
const originalRequest = error.config;
|
|
|
|
|
|
|
|
// Handle token expiration and retry the request with a refreshed token
|
|
|
|
if (error.response && error.response.status === 401 && !originalRequest._retry) {
|
|
|
|
originalRequest._retry = true;
|
|
|
|
return await refreshToken()
|
|
|
|
.then(() => {
|
|
|
|
return http(originalRequest);
|
|
|
|
})
|
2025-01-12 12:25:55 +00:00
|
|
|
.catch(() => {});
|
2024-08-22 09:48:04 +00:00
|
|
|
}
|
|
|
|
|
2024-12-19 10:16:05 +00:00
|
|
|
const notificationStore = useNotificationStore();
|
2025-01-13 09:24:03 +00:00
|
|
|
if (error.toString().includes("Network Error")) {
|
|
|
|
notificationStore.push("Netzwerkfehler", "Server nicht erreichbar!", "error");
|
|
|
|
} else {
|
|
|
|
notificationStore.push("Fehler", error.response.data, "error");
|
|
|
|
}
|
2024-12-19 10:16:05 +00:00
|
|
|
|
2024-08-22 09:48:04 +00:00
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-08-25 08:10:11 +00:00
|
|
|
export async function refreshToken(): Promise<void> {
|
2024-08-22 09:48:04 +00:00
|
|
|
return new Promise<void>(async (resolve, reject) => {
|
|
|
|
await http
|
|
|
|
.post(`/auth/refresh`, {
|
2024-08-25 08:10:11 +00:00
|
|
|
accessToken: localStorage.getItem("accessToken"),
|
2024-08-22 09:48:04 +00:00
|
|
|
refreshToken: localStorage.getItem("refreshToken"),
|
|
|
|
})
|
2024-08-26 11:46:54 +00:00
|
|
|
.then(async (response) => {
|
2024-08-25 08:10:11 +00:00
|
|
|
const { accessToken, refreshToken } = response.data;
|
2024-08-22 09:48:04 +00:00
|
|
|
|
2024-08-25 08:10:11 +00:00
|
|
|
localStorage.setItem("accessToken", accessToken);
|
2024-08-22 09:48:04 +00:00
|
|
|
localStorage.setItem("refreshToken", refreshToken);
|
|
|
|
|
2024-08-26 11:46:54 +00:00
|
|
|
await isAuthenticatedPromise().catch((err: string) => {
|
|
|
|
router.push({ name: err ?? "login" });
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
|
2024-08-22 09:48:04 +00:00
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error("Error refreshing token:", error);
|
2024-08-26 11:46:54 +00:00
|
|
|
reject("login");
|
2024-08-22 09:48:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-31 13:23:42 +00:00
|
|
|
function newEventSource(path: string) {
|
|
|
|
const token = localStorage.getItem("accessToken");
|
|
|
|
return new EventSourcePolyfill(url + "/api" + path, {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-01-12 12:25:55 +00:00
|
|
|
async function* streamingFetch(path: string, abort?: AbortController) {
|
|
|
|
await refreshToken()
|
|
|
|
.then(() => {})
|
|
|
|
.catch(() => {});
|
|
|
|
|
|
|
|
const token = localStorage.getItem("accessToken");
|
|
|
|
const response = await fetch(url + "/api" + path, {
|
|
|
|
signal: abort?.signal,
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const reader = response.body?.getReader();
|
|
|
|
while (true && reader) {
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
yield new TextDecoder().decode(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { http, newEventSource, streamingFetch, host };
|