Files
leocrm/frontend/src/hooks/useUserPermissions.ts
T
Agent Zero 744f2a1dbf fix(frontend): URL-Normalisierung gegen Doppel-Präfix — Workspace-UI & Permission-Refresh in Produktion repariert
Problem: Axios baseURL '/api/v1' + 16 apiX-Calls mit vollem Präfix
('/api/v1/workspaces/...') ergaben '/api/v1/api/v1/...' → 404 live
(bewiesen per curl + Node). Betroffen: komplette Workspace-UI
(Switcher, Manager, Phase-N-Scope-Editor) + Permission-Refresh.

Fix (Defense-in-Depth):
- normalizeApiUrl() in client.ts: alle apiX-Wrapper strippen redundanten
  '/api/v1'-Präfix — deckt auch DYNAMISCHE Backend-Contract-Endpoints
  (N2-Scope-Editor-Wertquellen) ab, die Call-Sites nicht umschreiben können
- workspaces.ts + useUserPermissions.ts auf relative Pfade gesäubert (16 Calls)

Tests: clientUrl 8/8 neu (Normalisierung + Wrapper-Beweis), tsc clean,
Build OK, Workspace-Regression 3 Dateien grün
2026-09-08 22:53:11 +02:00

40 lines
1.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { apiGet } from '@/api/client';
import { useAuthStore } from '@/store/authStore';
import { useEffect } from 'react';
interface PermissionsResponse {
permissions: string[];
denied_permissions: string[];
field_permissions: Record<string, any>;
is_system_admin: boolean;
}
/**
* Fetches the current user's resolved permissions from /api/v1/auth/me/permissions
* and stores them in the authStore.
*/
export function useUserPermissions() {
const { isAuthenticated, setPermissions } = useAuthStore();
const { data, isSuccess } = useQuery<PermissionsResponse>({
queryKey: ['user-permissions'],
queryFn: () => apiGet<PermissionsResponse>('/auth/me/permissions'),
enabled: isAuthenticated,
staleTime: 5 * 60 * 1000,
retry: 1,
});
useEffect(() => {
if (isSuccess && data) {
setPermissions(
data.permissions || [],
data.is_system_admin || false,
data.field_permissions || {},
);
}
}, [isSuccess, data, setPermissions]);
return { data, isSuccess };
}