ff-admin/src/serverCom.ts
Julian Krauser b1dbb806c6 slidein notification
show error message to user
2024-12-19 11:16:05 +01:00

91 lines
2.4 KiB
TypeScript

import axios from "axios";
import { isAuthenticatedPromise, type Payload } from "./router/authGuard";
import router from "./router";
import { useNotificationStore } from "./stores/notification";
let devMode = process.env.NODE_ENV === "development";
let host = devMode ? "localhost:5000" : process.env.SERVER_ADDRESS || "";
let url = devMode ? "http://" + host : (host ? "https://" : "") + host;
const http = axios.create({
baseURL: url + "/api",
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}`;
}
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
http.interceptors.response.use(
(response) => {
return response;
},
async (error) => {
if (!error.config.url.includes("/admin") || !error.config.url.includes("/user")) {
return Promise.reject(error);
}
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);
})
.catch();
}
const notificationStore = useNotificationStore();
notificationStore.push("Fehler", error.response.data, "error");
return Promise.reject(error);
}
);
export async function refreshToken(): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
await http
.post(`/auth/refresh`, {
accessToken: localStorage.getItem("accessToken"),
refreshToken: localStorage.getItem("refreshToken"),
})
.then(async (response) => {
const { accessToken, refreshToken } = response.data;
localStorage.setItem("accessToken", accessToken);
localStorage.setItem("refreshToken", refreshToken);
await isAuthenticatedPromise().catch((err: string) => {
router.push({ name: err ?? "login" });
reject(err);
});
resolve();
})
.catch((error) => {
console.error("Error refreshing token:", error);
reject("login");
});
});
}
export { http, host };