fix: handle auth errors and JSON parsing in admin panel

- 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 <noreply@anthropic.com>
This commit is contained in:
2025-07-20 21:58:53 +02:00
parent 4a7a3bba07
commit b2ae5f18ad
2 changed files with 7 additions and 2 deletions
+2 -2
View File
@@ -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')) {
@@ -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