Files
leocrm/frontend/src/hooks/useUserPermissions.ts
T

40 lines
1.1 KiB
TypeScript
Raw Normal View History

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 };
}