* 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 Routes from "@/lib/app-route.ts";
import APP_ROUTE from "@/lib/app-route.ts";
const api: AxiosInstance = axios.create({
baseURL: "/api",
@ -41,8 +41,8 @@ api.interceptors.response.use(
.includes("workspace not found")
) {
console.log("workspace not found");
if (window.location.pathname != Routes.AUTH.SETUP) {
window.location.href = Routes.AUTH.SETUP;
if (window.location.pathname != APP_ROUTE.AUTH.SETUP) {
window.location.href = APP_ROUTE.AUTH.SETUP;
}
}
break;
@ -58,11 +58,14 @@ api.interceptors.response.use(
);
function redirectToLogin() {
if (
window.location.pathname != Routes.AUTH.LOGIN &&
window.location.pathname != Routes.AUTH.SIGNUP
) {
window.location.href = Routes.AUTH.LOGIN;
const exemptPaths = [
APP_ROUTE.AUTH.LOGIN,
APP_ROUTE.AUTH.SIGNUP,
APP_ROUTE.AUTH.FORGOT_PASSWORD,
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";
declare global {
interface Window {
CONFIG?: Record<string, string>;
}
interface Window {
CONFIG?: Record<string, string>;
}
}
export function getAppName(): string{
return 'Docmost';
export function getAppName(): string {
return "Docmost";
}
export function getAppUrl(): string {
//let appUrl = window.CONFIG?.APP_URL || process.env.APP_URL;
// if (import.meta.env.DEV) {
// return appUrl || "http://localhost:3000";
//}
return `${window.location.protocol}//${window.location.host}`;
return `${window.location.protocol}//${window.location.host}`;
}
export function getBackendUrl(): string {
return getAppUrl() + '/api';
return getAppUrl() + "/api";
}
export function getCollaborationUrl(): string {
const COLLAB_PATH = '/collab';
const COLLAB_PATH = "/collab";
let url = getAppUrl();
if (import.meta.env.DEV) {
url = process.env.APP_URL;
}
let url = getAppUrl();
if (import.meta.env.DEV) {
url = process.env.APP_URL;
}
const wsProtocol = url.startsWith('https') ? 'wss' : 'ws';
return `${wsProtocol}://${url.split('://')[1]}${COLLAB_PATH}`;
const wsProtocol = url.startsWith("https") ? "wss" : "ws";
return `${wsProtocol}://${url.split("://")[1]}${COLLAB_PATH}`;
}
export function getAvatarUrl(avatarUrl: string) {
if (!avatarUrl) {
return null;
}
if (!avatarUrl) return null;
if (avatarUrl?.startsWith("http")) return avatarUrl;
if (avatarUrl?.startsWith('http')) {
return avatarUrl;
}
return getBackendUrl() + '/attachments/img/avatar/' + avatarUrl;
return getBackendUrl() + "/attachments/img/avatar/" + avatarUrl;
}
export function getSpaceUrl(spaceSlug: string) {
return '/s/' + spaceSlug;
return "/s/" + spaceSlug;
}
export function getFileUrl(src: string) {
return src?.startsWith('/files/') ? getBackendUrl() + src : src;
return src?.startsWith("/files/") ? getBackendUrl() + src : src;
}
export function getFileUploadSizeLimit() {
const limit =getConfigValue("FILE_UPLOAD_SIZE_LIMIT", "50mb");
return bytes(limit);
const limit = getConfigValue("FILE_UPLOAD_SIZE_LIMIT", "50mb");
return bytes(limit);
}
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) {
return window.CONFIG?.[key] || process?.env?.[key] || defaultValue;
}
function getConfigValue(key: string, defaultValue: string = undefined): string {
const rawValue = import.meta.env.DEV
? process?.env?.[key]
: window?.CONFIG?.[key];
return rawValue ?? defaultValue;
}