* fix 401 redirect in auth routes (#674)

* fix config getter
This commit is contained in:
Philip Okugbe
2025-01-26 14:01:08 +00:00
committed by GitHub
parent 990612793f
commit 85159a2c95
2 changed files with 39 additions and 44 deletions

View File

@ -1,5 +1,5 @@
import axios, { AxiosInstance } from "axios"; import axios, { AxiosInstance } from "axios";
import Routes from "@/lib/app-route.ts"; import APP_ROUTE from "@/lib/app-route.ts";
const api: AxiosInstance = axios.create({ const api: AxiosInstance = axios.create({
baseURL: "/api", baseURL: "/api",
@ -41,8 +41,8 @@ api.interceptors.response.use(
.includes("workspace not found") .includes("workspace not found")
) { ) {
console.log("workspace not found"); console.log("workspace not found");
if (window.location.pathname != Routes.AUTH.SETUP) { if (window.location.pathname != APP_ROUTE.AUTH.SETUP) {
window.location.href = Routes.AUTH.SETUP; window.location.href = APP_ROUTE.AUTH.SETUP;
} }
} }
break; break;
@ -58,11 +58,14 @@ api.interceptors.response.use(
); );
function redirectToLogin() { function redirectToLogin() {
if ( const exemptPaths = [
window.location.pathname != Routes.AUTH.LOGIN && APP_ROUTE.AUTH.LOGIN,
window.location.pathname != Routes.AUTH.SIGNUP APP_ROUTE.AUTH.SIGNUP,
) { APP_ROUTE.AUTH.FORGOT_PASSWORD,
window.location.href = Routes.AUTH.LOGIN; APP_ROUTE.AUTH.PASSWORD_RESET,
];
if (!exemptPaths.some((path) => window.location.pathname === path)) {
window.location.href = APP_ROUTE.AUTH.LOGIN;
} }
} }

View File

@ -1,70 +1,62 @@
import bytes from "bytes"; import bytes from "bytes";
declare global { declare global {
interface Window { interface Window {
CONFIG?: Record<string, string>; CONFIG?: Record<string, string>;
} }
} }
export function getAppName(): string{ export function getAppName(): string {
return 'Docmost'; return "Docmost";
} }
export function getAppUrl(): string { export function getAppUrl(): string {
//let appUrl = window.CONFIG?.APP_URL || process.env.APP_URL; return `${window.location.protocol}//${window.location.host}`;
// if (import.meta.env.DEV) {
// return appUrl || "http://localhost:3000";
//}
return `${window.location.protocol}//${window.location.host}`;
} }
export function getBackendUrl(): string { export function getBackendUrl(): string {
return getAppUrl() + '/api'; return getAppUrl() + "/api";
} }
export function getCollaborationUrl(): string { export function getCollaborationUrl(): string {
const COLLAB_PATH = '/collab'; const COLLAB_PATH = "/collab";
let url = getAppUrl(); let url = getAppUrl();
if (import.meta.env.DEV) { if (import.meta.env.DEV) {
url = process.env.APP_URL; url = process.env.APP_URL;
} }
const wsProtocol = url.startsWith('https') ? 'wss' : 'ws'; const wsProtocol = url.startsWith("https") ? "wss" : "ws";
return `${wsProtocol}://${url.split('://')[1]}${COLLAB_PATH}`; return `${wsProtocol}://${url.split("://")[1]}${COLLAB_PATH}`;
} }
export function getAvatarUrl(avatarUrl: string) { export function getAvatarUrl(avatarUrl: string) {
if (!avatarUrl) { if (!avatarUrl) return null;
return null; if (avatarUrl?.startsWith("http")) return avatarUrl;
}
if (avatarUrl?.startsWith('http')) { return getBackendUrl() + "/attachments/img/avatar/" + avatarUrl;
return avatarUrl;
}
return getBackendUrl() + '/attachments/img/avatar/' + avatarUrl;
} }
export function getSpaceUrl(spaceSlug: string) { export function getSpaceUrl(spaceSlug: string) {
return '/s/' + spaceSlug; return "/s/" + spaceSlug;
} }
export function getFileUrl(src: string) { export function getFileUrl(src: string) {
return src?.startsWith('/files/') ? getBackendUrl() + src : src; return src?.startsWith("/files/") ? getBackendUrl() + src : src;
} }
export function getFileUploadSizeLimit() { export function getFileUploadSizeLimit() {
const limit =getConfigValue("FILE_UPLOAD_SIZE_LIMIT", "50mb"); const limit = getConfigValue("FILE_UPLOAD_SIZE_LIMIT", "50mb");
return bytes(limit); return bytes(limit);
} }
export function getDrawioUrl() { export function getDrawioUrl() {
return getConfigValue("DRAWIO_URL", "https://embed.diagrams.net"); return getConfigValue("DRAWIO_URL", "https://embed.diagrams.net");
} }
function getConfigValue(key: string, defaultValue: string = undefined) { function getConfigValue(key: string, defaultValue: string = undefined): string {
return window.CONFIG?.[key] || process?.env?.[key] || defaultValue; const rawValue = import.meta.env.DEV
} ? process?.env?.[key]
: window?.CONFIG?.[key];
return rawValue ?? defaultValue;
}