mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 06:23:06 +08:00
4d9fe6f804
* Fix invitation signup redirect
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import axios, { AxiosInstance } from "axios";
|
|
import APP_ROUTE from "@/lib/app-route.ts";
|
|
|
|
const api: AxiosInstance = axios.create({
|
|
baseURL: "/api",
|
|
withCredentials: true,
|
|
});
|
|
|
|
api.interceptors.response.use(
|
|
(response) => {
|
|
// we need the response headers for these endpoints
|
|
const exemptEndpoints = ["/api/pages/export", "/api/spaces/export"];
|
|
if (response.request.responseURL) {
|
|
const path = new URL(response.request.responseURL)?.pathname;
|
|
if (path && exemptEndpoints.includes(path)) {
|
|
return response;
|
|
}
|
|
}
|
|
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
if (error.response) {
|
|
switch (error.response.status) {
|
|
case 401: {
|
|
const url = new URL(error.request.responseURL)?.pathname;
|
|
if (url === "/api/auth/collab-token") return;
|
|
|
|
// Handle unauthorized error
|
|
redirectToLogin();
|
|
break;
|
|
}
|
|
case 403:
|
|
// Handle forbidden error
|
|
break;
|
|
case 404:
|
|
// Handle not found error
|
|
if (
|
|
error.response.data.message
|
|
.toLowerCase()
|
|
.includes("workspace not found")
|
|
) {
|
|
console.log("workspace not found");
|
|
if (window.location.pathname != APP_ROUTE.AUTH.SETUP) {
|
|
window.location.href = APP_ROUTE.AUTH.SETUP;
|
|
}
|
|
}
|
|
break;
|
|
case 500:
|
|
// Handle internal server error
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return Promise.reject(error);
|
|
},
|
|
);
|
|
|
|
function redirectToLogin() {
|
|
const exemptPaths = [
|
|
APP_ROUTE.AUTH.LOGIN,
|
|
APP_ROUTE.AUTH.SIGNUP,
|
|
APP_ROUTE.AUTH.FORGOT_PASSWORD,
|
|
APP_ROUTE.AUTH.PASSWORD_RESET,
|
|
"/invites",
|
|
];
|
|
if (!exemptPaths.some((path) => window.location.pathname.startsWith(path))) {
|
|
window.location.href = APP_ROUTE.AUTH.LOGIN;
|
|
}
|
|
}
|
|
|
|
export default api;
|