From a67df870130dbc5c54820d41ae487b68a68d7ffd Mon Sep 17 00:00:00 2001 From: paul Date: Sun, 20 Jul 2025 21:58:53 +0200 Subject: [PATCH] fix: handle auth errors and JSON parsing in admin panel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added proper HTTP status check before JSON parsing in AnalyticsPage * Prevents "Unexpected token '<'" error when API returns HTML error pages * Throws proper error for non-OK responses - Enhanced API error handling to treat 403 as auth failure * Both 401 and 403 now trigger redirect to login page * Clears expired admin tokens automatically * Prevents users from staying on admin pages with expired sessions These fixes resolve: 1. JSON parse errors when fetching Umami config 2. 403 Forbidden errors not redirecting to login 3. Backend version display issues due to auth failures 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/src/config/api.ts | 4 ++-- frontend/src/pages/admin/AnalyticsPage.tsx | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/frontend/src/config/api.ts b/frontend/src/config/api.ts index 39572af..724d63f 100644 --- a/frontend/src/config/api.ts +++ b/frontend/src/config/api.ts @@ -82,13 +82,13 @@ api.interceptors.response.use( } } - if (error.response?.status === 401) { + if (error.response?.status === 401 || error.response?.status === 403) { // Check if it's an admin route const isAdminRoute = error.config?.url?.includes('/admin'); const currentPath = window.location.pathname; if (isAdminRoute) { - // Clear admin token on unauthorized + // Clear admin token on unauthorized or forbidden Cookies.remove(ADMIN_TOKEN_KEY); // Only redirect if we're not already on the admin login page if (!currentPath.includes('/admin/login')) { diff --git a/frontend/src/pages/admin/AnalyticsPage.tsx b/frontend/src/pages/admin/AnalyticsPage.tsx index be7993b..c883d8b 100644 --- a/frontend/src/pages/admin/AnalyticsPage.tsx +++ b/frontend/src/pages/admin/AnalyticsPage.tsx @@ -76,6 +76,11 @@ export const AnalyticsPage: React.FC = () => { const fetchUmamiConfig = async () => { try { const response = await fetch(`${import.meta.env.VITE_API_URL}/api/public/settings`); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const settings = await response.json(); // Check if Umami is enabled in admin settings