From 2de2939e30b697d2033b9dd51acefe0127943063 Mon Sep 17 00:00:00 2001 From: Charles Zhao Date: Wed, 17 Apr 2024 17:55:40 +0800 Subject: [PATCH] fix(console): cloud collaboration minor bug fixes (#5734) * fix(console): oss version should not check user tenant scopes * fix(console): collaborators should leave immediately if they are removed from tenant --- .../console/src/containers/ConsoleContent/hooks.ts | 14 ++++++++++++-- .../console/src/hooks/use-current-tenant-scopes.ts | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/console/src/containers/ConsoleContent/hooks.ts b/packages/console/src/containers/ConsoleContent/hooks.ts index 69a3543a0..d08b99136 100644 --- a/packages/console/src/containers/ConsoleContent/hooks.ts +++ b/packages/console/src/containers/ConsoleContent/hooks.ts @@ -2,6 +2,7 @@ import { Prompt, useLogto } from '@logto/react'; import { getTenantOrganizationId } from '@logto/schemas'; import { useContext, useEffect, useState } from 'react'; +import { isCloud } from '@/consts/env'; import { TenantsContext } from '@/contexts/TenantsProvider'; import useCurrentTenantScopes from '@/hooks/use-current-tenant-scopes'; import useRedirectUri from '@/hooks/use-redirect-uri'; @@ -17,7 +18,7 @@ import { saveRedirect } from '@/utils/storage'; * Note: This hook should only be used once in the ConsoleContent component. */ const useTenantScopeListener = () => { - const { currentTenantId } = useContext(TenantsContext); + const { currentTenantId, removeTenant, navigateTenant } = useContext(TenantsContext); const { clearAccessToken, clearAllTokens, getOrganizationTokenClaims, signIn } = useLogto(); const [tokenClaims, setTokenClaims] = useState(); const redirectUri = useRedirectUri(); @@ -32,7 +33,16 @@ const useTenantScopeListener = () => { }, [currentTenantId, getOrganizationTokenClaims]); useEffect(() => { - if (isLoading || tokenClaims === undefined) { + if (isCloud && !isLoading && scopes.length === 0) { + // User has no access to the current tenant. Navigate to root and it will return to the + // last visited tenant, or fallback to the page where the user can create a new tenant. + removeTenant(currentTenantId); + navigateTenant(''); + } + }, [currentTenantId, isLoading, navigateTenant, removeTenant, scopes.length]); + + useEffect(() => { + if (!isCloud || isLoading || tokenClaims === undefined) { return; } const hasScopesGranted = scopes.some((scope) => !tokenClaims.includes(scope)); diff --git a/packages/console/src/hooks/use-current-tenant-scopes.ts b/packages/console/src/hooks/use-current-tenant-scopes.ts index 56e98a579..21d6fea55 100644 --- a/packages/console/src/hooks/use-current-tenant-scopes.ts +++ b/packages/console/src/hooks/use-current-tenant-scopes.ts @@ -3,13 +3,14 @@ import { useContext, useMemo } from 'react'; import useSWR from 'swr'; import { useAuthedCloudApi } from '@/cloud/hooks/use-cloud-api'; +import { isCloud } from '@/consts/env'; import { TenantsContext } from '@/contexts/TenantsProvider'; import { type RequestError } from './use-api'; import useCurrentUser from './use-current-user'; const useCurrentTenantScopes = () => { - const { currentTenantId, isInitComplete } = useContext(TenantsContext); + const { currentTenantId } = useContext(TenantsContext); const cloudApi = useAuthedCloudApi(); const { user } = useCurrentUser(); const userId = user?.id ?? ''; @@ -19,7 +20,7 @@ const useCurrentTenantScopes = () => { isLoading, mutate, } = useSWR( - userId && isInitComplete && `api/tenants/${currentTenantId}/members/${userId}/scopes`, + isCloud && userId && `api/tenants/${currentTenantId}/members/${userId}/scopes`, async () => { const scopes = await cloudApi.get('/api/tenants/:tenantId/members/:userId/scopes', { params: { tenantId: currentTenantId, userId },