0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(console): redirect to current tenant when needed

This commit is contained in:
Gao Sun 2023-06-28 15:51:45 +08:00
parent 51fb943986
commit c1fa47f280
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
3 changed files with 21 additions and 3 deletions

View file

@ -9,6 +9,7 @@ import { withTranslation } from 'react-i18next';
import AppError from '@/components/AppError';
import SessionExpired from '@/components/SessionExpired';
import { isInCallback } from '@/utils/url';
type Props = {
children: ReactNode;
@ -59,8 +60,7 @@ class ErrorBoundary extends Component<Props, State> {
if (error) {
// Different strategies for handling errors in callback pages since the callback errors
// are likely unexpected and unrecoverable.
const { pathname } = window.location;
if (['/callback', '-callback'].some((path) => pathname.endsWith(path))) {
if (isInCallback()) {
if (error instanceof LogtoError && error.data instanceof OidcError) {
return (
<AppError

View file

@ -2,11 +2,21 @@ import { useHandleSignInCallback } from '@logto/react';
import { useNavigate } from 'react-router-dom';
import AppLoading from '@/components/AppLoading';
import { getUserTenantId } from '@/consts';
import { isInFirstLevelCallback } from '@/utils/url';
function Callback() {
const navigate = useNavigate();
useHandleSignInCallback(() => {
navigate('/', { replace: true });
/**
* The first level callback check is due to the usage of `basename`
* for tenant-specific routes, e.g., `/:tenantId/applications`.
* Once we merge all the routes into one router, we can remove this check.
*/
navigate(isInFirstLevelCallback() ? `/${getUserTenantId()}` : '/', {
replace: true,
});
});
return <AppLoading />;

View file

@ -2,3 +2,11 @@ export const buildUrl = (path: string, searchParameters: Record<string, string>)
`${path}?${new URLSearchParams(searchParameters).toString()}`;
export const formatSearchKeyword = (keyword: string) => `%${keyword}%`;
/** If the current pathname is `/callback` or ends with `-callback`, we consider it as a callback page. */
export const isInCallback = () =>
['/callback', '-callback'].some((path) => window.location.pathname.endsWith(path));
/** If the current pathname is a callback page and the pathname only has one level. */
export const isInFirstLevelCallback = () =>
window.location.pathname.split('/').length === 1 && isInCallback();