logto-io / logto

🧑‍🚀 The better identity infrastructure for developers and the open-source alternative to Auth0.
https://logto.io
Mozilla Public License 2.0
8.31k stars 406 forks source link

fix(console): check scope only when data is ready #6329

Closed gao-sun closed 1 month ago

gao-sun commented 1 month ago

Summary

the scope fallback will cause false positive on tenant access assertion, thus an accessible tenant will be removed from the list. sometimes isLoading may be false but the data is not loaded.

Original

const { scopes = [], isLoading } = useCurrentTenantScopes(); // `scopes` falls back to an empty array

useEffect(() => {
  // `scopes` may not be loaded at all, which is `undefined` in the return of `useCurrentTenantScopes()`.
  // However, the fallback value `[]` will be used here, while the fetching process is not started yet
  // (`isLoading` is `false`) which causes a false positive.
  if (isCloud && !isLoading && scopes.length === 0) {
    removeTenant(currentTenantId);
    navigateTenant('');
  }
}, [currentTenantId, isLoading, navigateTenant, removeTenant, scopes.length]);

Now

// No fallback. Everyone should be happy now.
const { scopes, isLoading } = useCurrentTenantScopes();

useEffect(() => {
  // When `scopes` is `undefined`, the `scopes?.length === 0` expression will be `false`
  if (isCloud && !isLoading && scopes?.length === 0) {
    removeTenant(currentTenantId);
    navigateTenant('');
  }
}, [currentTenantId, isLoading, navigateTenant, removeTenant, scopes?.length]);

Testing

cloud tests ok

image

Checklist

github-actions[bot] commented 1 month ago

COMPARE TO master

Total Size Diff :chart_with_downwards_trend: -1 Bytes

Diff by File |Name|Diff| |---|---| |packages/console/src/containers/ConsoleContent/hooks.ts|:chart_with_downwards_trend: -1 Bytes|