2026-04-05 02:50:51 +02:00
|
|
|
import { useEffect } from 'react';
|
2026-04-05 14:04:06 +02:00
|
|
|
import { useLogto } from '@logto/react';
|
2026-04-05 02:50:51 +02:00
|
|
|
import { Spinner } from '@cameleer/design-system';
|
|
|
|
|
import { useMe } from '../api/hooks';
|
|
|
|
|
import { useOrgStore } from './useOrganization';
|
2026-04-05 14:04:06 +02:00
|
|
|
import { fetchConfig } from '../config';
|
2026-04-05 02:50:51 +02:00
|
|
|
|
|
|
|
|
/**
|
2026-04-05 14:04:06 +02:00
|
|
|
* Fetches /api/me and populates the org store with tenant-to-org mapping.
|
|
|
|
|
* Also reads OAuth2 scopes from the access token and stores them.
|
|
|
|
|
* Renders children once resolved.
|
2026-04-05 02:50:51 +02:00
|
|
|
*/
|
|
|
|
|
export function OrgResolver({ children }: { children: React.ReactNode }) {
|
|
|
|
|
const { data: me, isLoading, isError } = useMe();
|
2026-04-05 14:04:06 +02:00
|
|
|
const { getAccessToken } = useLogto();
|
|
|
|
|
const { setOrganizations, setCurrentOrg, setScopes, currentOrgId } = useOrgStore();
|
2026-04-05 02:50:51 +02:00
|
|
|
|
2026-04-05 15:32:53 +02:00
|
|
|
// Effect 1: Org population — runs when /api/me data loads
|
2026-04-05 02:50:51 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!me) return;
|
|
|
|
|
|
|
|
|
|
// Map tenants: logtoOrgId is the org ID for token scoping, id is the DB UUID
|
|
|
|
|
const orgEntries = me.tenants.map((t) => ({
|
|
|
|
|
id: t.logtoOrgId,
|
|
|
|
|
name: t.name,
|
|
|
|
|
slug: t.slug,
|
|
|
|
|
tenantId: t.id,
|
|
|
|
|
}));
|
|
|
|
|
setOrganizations(orgEntries);
|
|
|
|
|
|
|
|
|
|
// Auto-select if single tenant and no org selected yet
|
|
|
|
|
if (orgEntries.length === 1 && !currentOrgId) {
|
|
|
|
|
setCurrentOrg(orgEntries[0].id);
|
|
|
|
|
}
|
2026-04-05 15:32:53 +02:00
|
|
|
}, [me]);
|
|
|
|
|
|
|
|
|
|
// Effect 2: Scope fetching — runs when me loads OR when currentOrgId changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!me) return;
|
2026-04-05 14:04:06 +02:00
|
|
|
|
2026-04-05 15:32:53 +02:00
|
|
|
// Read scopes from access tokens:
|
|
|
|
|
// - org-scoped resource token → tenant-level scopes (apps:manage, observe:read, etc.)
|
|
|
|
|
// - global resource token → platform-level scopes (platform:admin)
|
|
|
|
|
fetchConfig().then(async (config) => {
|
2026-04-05 14:04:06 +02:00
|
|
|
if (!config.logtoResource) return;
|
2026-04-05 15:32:53 +02:00
|
|
|
|
|
|
|
|
const extractScopes = (token: string | undefined): string[] => {
|
|
|
|
|
if (!token) return [];
|
2026-04-05 14:04:06 +02:00
|
|
|
try {
|
|
|
|
|
const payload = JSON.parse(atob(token.split('.')[1]));
|
2026-04-05 15:32:53 +02:00
|
|
|
return ((payload.scope as string) ?? '').split(' ').filter(Boolean);
|
2026-04-05 14:04:06 +02:00
|
|
|
} catch {
|
2026-04-05 15:32:53 +02:00
|
|
|
return [];
|
2026-04-05 14:04:06 +02:00
|
|
|
}
|
2026-04-05 15:32:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const [orgToken, globalToken] = await Promise.all([
|
|
|
|
|
currentOrgId
|
|
|
|
|
? getAccessToken(config.logtoResource, currentOrgId).catch(() => undefined)
|
|
|
|
|
: Promise.resolve(undefined),
|
|
|
|
|
getAccessToken(config.logtoResource).catch(() => undefined),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const merged = new Set([
|
|
|
|
|
...extractScopes(orgToken),
|
|
|
|
|
...extractScopes(globalToken),
|
|
|
|
|
]);
|
|
|
|
|
setScopes(merged);
|
|
|
|
|
} catch {
|
|
|
|
|
setScopes(new Set());
|
|
|
|
|
}
|
2026-04-05 14:04:06 +02:00
|
|
|
});
|
2026-04-05 15:32:53 +02:00
|
|
|
}, [me, currentOrgId]);
|
2026-04-05 02:50:51 +02:00
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return (
|
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
|
|
|
|
|
<Spinner />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isError) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|