0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-13 21:30:30 -05:00

refactor(console): set current tenant validated to false when resetting

This commit is contained in:
Gao Sun 2023-06-24 17:55:17 +08:00
parent d7f96a6559
commit 3cbfd7e712
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
6 changed files with 18 additions and 12 deletions

View file

@ -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() {
<ErrorBoundary>
{/**
* 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) ? (
<AppEndpointsProvider>
<AppConfirmModalProvider>
<TenantAppContainer />

View file

@ -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.

View file

@ -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 === '/') {

View file

@ -98,6 +98,7 @@ function TenantsProvider({ children }: Props) {
tenants,
resetTenants: (tenants: TenantInfo[]) => {
setTenants(tenants);
setCurrentTenantValidated(false);
setIsInitComplete(true);
},
appendTenant: (tenant: TenantInfo) => {

View file

@ -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();
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,

View file

@ -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();