Files
cameleer-saas/ui/src/auth/useOrganization.ts
hsiegeln 9c2a1d27b7 feat: replace hardcoded permission map with direct OAuth2 scope checks
Remove role-to-permission mapping (usePermissions, RequirePermission) and replace
with direct scope reads from the Logto access token JWT. OrgResolver decodes the
scope claim after /api/me resolves and stores scopes in Zustand. RequireScope and
useScopes replace the old hooks/components across all pages.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-05 14:04:06 +02:00

40 lines
1.3 KiB
TypeScript

import { create } from 'zustand';
export interface OrgInfo {
id: string; // Logto org ID (for token scoping)
name: string;
slug?: string;
tenantId?: string; // DB tenant UUID (for API calls)
}
interface OrgState {
currentOrgId: string | null; // Logto org ID — used for getAccessToken(resource, orgId)
currentTenantId: string | null; // DB UUID — used for API calls like /api/tenants/{id}
organizations: OrgInfo[];
scopes: Set<string>;
setCurrentOrg: (orgId: string | null) => void;
setOrganizations: (orgs: OrgInfo[]) => void;
setScopes: (scopes: Set<string>) => void;
}
export const useOrgStore = create<OrgState>((set, get) => ({
currentOrgId: null,
currentTenantId: null,
organizations: [],
scopes: new Set(),
setCurrentOrg: (orgId) => {
const org = get().organizations.find((o) => o.id === orgId);
set({ currentOrgId: orgId, currentTenantId: org?.tenantId ?? null });
},
setOrganizations: (orgs) => {
const { currentOrgId } = get();
const match = currentOrgId ? orgs.find((o) => o.id === currentOrgId) : null;
set({
organizations: orgs,
// Re-resolve tenantId when org list is enriched (e.g., OrgResolver adds tenantId)
currentTenantId: match?.tenantId ?? get().currentTenantId,
});
},
setScopes: (scopes) => set({ scopes }),
}));