Files
cameleer-saas/ui/src/auth/useOrganization.ts
hsiegeln a3a6f99958
All checks were successful
CI / build (push) Successful in 1m6s
CI / docker (push) Successful in 42s
fix: prevent vendor redirect to /tenant on hard refresh
RequireScope and LandingRedirect now wait for scopesReady flag before
evaluating, preventing the race where org-scoped tokens load before
global tokens and the vendor gets incorrectly redirected.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 19:16:46 +02:00

46 lines
1.6 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>;
scopesReady: boolean; // true once OrgResolver has finished loading scopes
username: string | null;
setCurrentOrg: (orgId: string | null) => void;
setOrganizations: (orgs: OrgInfo[]) => void;
setScopes: (scopes: Set<string>) => void;
setUsername: (name: string | null) => void;
}
export const useOrgStore = create<OrgState>((set, get) => ({
currentOrgId: null,
currentTenantId: null,
organizations: [],
scopes: new Set(),
scopesReady: false,
username: null,
setUsername: (name) => set({ username: name }),
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, scopesReady: true }),
}));