From 3cbfd7e712d4171bfce6d9b8079264c9f9a9632a Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Sat, 24 Jun 2023 17:55:17 +0800 Subject: [PATCH] refactor(console): set current tenant validated to false when resetting --- packages/console/src/App.tsx | 6 +++--- packages/console/src/cloud/pages/Main/index.tsx | 3 +++ .../console/src/containers/AppContent/index.tsx | 3 --- packages/console/src/contexts/TenantsProvider.tsx | 1 + .../src/hooks/use-validate-tenant-access.ts | 14 +++++++++++--- packages/console/src/onboarding/index.tsx | 3 --- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/console/src/App.tsx b/packages/console/src/App.tsx index 3b26fa38f..1260b949e 100644 --- a/packages/console/src/App.tsx +++ b/packages/console/src/App.tsx @@ -29,7 +29,7 @@ import initI18n from './i18n/init'; void initI18n(); function Content() { - const { tenants, currentTenantId } = useContext(TenantsContext); + const { tenants, currentTenantId, currentTenantValidated } = useContext(TenantsContext); const resources = useMemo( () => @@ -78,9 +78,9 @@ function Content() { {/** * If it's not Cloud (OSS), render the tenant app container directly since only default tenant is available; - * if it's Cloud, render the tenant app container only when init is complete and a tenant ID is available (in a tenant context). + * if it's Cloud, render the tenant app container only when a tenant ID is available (in a tenant context). */} - {!isCloud || currentTenantId ? ( + {!isCloud || (currentTenantId && currentTenantValidated) ? ( diff --git a/packages/console/src/cloud/pages/Main/index.tsx b/packages/console/src/cloud/pages/Main/index.tsx index a015cf302..a9ae386d6 100644 --- a/packages/console/src/cloud/pages/Main/index.tsx +++ b/packages/console/src/cloud/pages/Main/index.tsx @@ -1,6 +1,7 @@ import { useContext } from 'react'; import { TenantsContext } from '@/contexts/TenantsProvider'; +import useValidateTenantAccess from '@/hooks/use-validate-tenant-access'; import useUserOnboardingData from '@/onboarding/hooks/use-user-onboarding-data'; import AutoCreateTenant from './AutoCreateTenant'; @@ -11,6 +12,8 @@ export default function Main() { const { tenants } = useContext(TenantsContext); const { isOnboarding } = useUserOnboardingData(); + useValidateTenantAccess(); + /** * If current tenant ID is not set, but there is at least one tenant available, * trigger a redirect to the first tenant. diff --git a/packages/console/src/containers/AppContent/index.tsx b/packages/console/src/containers/AppContent/index.tsx index b1255b5d8..ede7e02e4 100644 --- a/packages/console/src/containers/AppContent/index.tsx +++ b/packages/console/src/containers/AppContent/index.tsx @@ -11,7 +11,6 @@ import { isCloud } from '@/consts/env'; import useConfigs from '@/hooks/use-configs'; import useScroll from '@/hooks/use-scroll'; import useUserPreferences from '@/hooks/use-user-preferences'; -import useValidateTenantAccess from '@/hooks/use-validate-tenant-access'; import Broadcast from '@/onboarding/components/Broadcast'; import { getPath } from '../ConsoleContent/Sidebar'; @@ -35,8 +34,6 @@ function AppContent() { const { scrollTop } = useScroll(scrollableContent.current); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); - useValidateTenantAccess(); - useEffect(() => { // Navigate to the first menu item after configs are loaded. if (!isLoading && location.pathname === '/') { diff --git a/packages/console/src/contexts/TenantsProvider.tsx b/packages/console/src/contexts/TenantsProvider.tsx index f88bdb66c..c71ccc39a 100644 --- a/packages/console/src/contexts/TenantsProvider.tsx +++ b/packages/console/src/contexts/TenantsProvider.tsx @@ -98,6 +98,7 @@ function TenantsProvider({ children }: Props) { tenants, resetTenants: (tenants: TenantInfo[]) => { setTenants(tenants); + setCurrentTenantValidated(false); setIsInitComplete(true); }, appendTenant: (tenant: TenantInfo) => { diff --git a/packages/console/src/hooks/use-validate-tenant-access.ts b/packages/console/src/hooks/use-validate-tenant-access.ts index 0cba6f365..9156eaada 100644 --- a/packages/console/src/hooks/use-validate-tenant-access.ts +++ b/packages/console/src/hooks/use-validate-tenant-access.ts @@ -8,7 +8,7 @@ import { TenantsContext } from '@/contexts/TenantsProvider'; const useValidateTenantAccess = () => { const { getAccessToken, signIn, isAuthenticated } = useLogto(); - const { currentTenant, currentTenantValidated, setCurrentTenantValidated } = + const { currentTenant, currentTenantId, currentTenantValidated, setCurrentTenantValidated } = useContext(TenantsContext); useEffect(() => { @@ -21,12 +21,20 @@ const useValidateTenantAccess = () => { } }; - if (isAuthenticated && currentTenant && !currentTenantValidated) { + if (isAuthenticated && currentTenantId && !currentTenantValidated) { setCurrentTenantValidated(); - void validate(currentTenant); + if (currentTenant) { + void validate(currentTenant); + } else { + // The current tenant is unavailable to the user, maybe a deleted tenant or a tenant that + // the user has no access to. Fall back to the home page. + // eslint-disable-next-line @silverhand/fp/no-mutation + window.location.href = '/'; + } } }, [ currentTenant, + currentTenantId, currentTenantValidated, getAccessToken, isAuthenticated, diff --git a/packages/console/src/onboarding/index.tsx b/packages/console/src/onboarding/index.tsx index 31e5311b4..5380ce40b 100644 --- a/packages/console/src/onboarding/index.tsx +++ b/packages/console/src/onboarding/index.tsx @@ -11,7 +11,6 @@ import ProtectedRoutes from '@/containers/ProtectedRoutes'; import { AppThemeContext } from '@/contexts/AppThemeProvider'; import Toast from '@/ds-components/Toast'; import useSwrOptions from '@/hooks/use-swr-options'; -import useValidateTenantAccess from '@/hooks/use-validate-tenant-access'; import NotFound from '@/pages/NotFound'; import { gtagAwTrackingId, gtagSignUpConversionId, logtoProductionHostname } from './constants'; @@ -66,8 +65,6 @@ function Layout() { } }, []); - useValidateTenantAccess(); - const { data: { questionnaire }, } = useUserOnboardingData();