0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-17 22:31:28 -05:00

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
This commit is contained in:
Charles Zhao 2024-04-17 17:55:40 +08:00 committed by GitHub
parent ddd99865a5
commit 2de2939e30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 4 deletions

View file

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

View file

@ -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<string[], RequestError>(
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 },