From 85159a2c95e35ee2b62772c03910a75d120c9b80 Mon Sep 17 00:00:00 2001 From: Philip Okugbe <16838612+Philipinho@users.noreply.github.com> Date: Sun, 26 Jan 2025 14:01:08 +0000 Subject: [PATCH] * fix 401 redirect in auth routes (#674) * fix config getter --- apps/client/src/lib/api-client.ts | 19 +++++---- apps/client/src/lib/config.ts | 64 ++++++++++++++----------------- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/apps/client/src/lib/api-client.ts b/apps/client/src/lib/api-client.ts index d80ce8a6..c666d5fc 100644 --- a/apps/client/src/lib/api-client.ts +++ b/apps/client/src/lib/api-client.ts @@ -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; } } diff --git a/apps/client/src/lib/config.ts b/apps/client/src/lib/config.ts index 828adf08..af92de5d 100644 --- a/apps/client/src/lib/config.ts +++ b/apps/client/src/lib/config.ts @@ -1,70 +1,62 @@ import bytes from "bytes"; declare global { - interface Window { - CONFIG?: Record; - } + interface Window { + CONFIG?: Record; + } } -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; -} \ No newline at end of file +function getConfigValue(key: string, defaultValue: string = undefined): string { + const rawValue = import.meta.env.DEV + ? process?.env?.[key] + : window?.CONFIG?.[key]; + return rawValue ?? defaultValue; +}