From ab8c75efa84f92cfebd4bb37bd9e05283da34250 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Thu, 24 Aug 2023 14:54:08 +0800 Subject: [PATCH] fix(console): use correct sign-out redirect uri for api hooks --- packages/console/src/hooks/use-api.ts | 9 ++++----- packages/console/src/hooks/use-redirect-uri.ts | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/console/src/hooks/use-api.ts b/packages/console/src/hooks/use-api.ts index 465d1a930..2094f44c6 100644 --- a/packages/console/src/hooks/use-api.ts +++ b/packages/console/src/hooks/use-api.ts @@ -6,13 +6,13 @@ import ky from 'ky'; import { useCallback, useContext, useMemo } from 'react'; import { toast } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; -import { useHref } from 'react-router-dom'; import { requestTimeout } from '@/consts'; import { AppDataContext } from '@/contexts/AppDataProvider'; import { TenantsContext } from '@/contexts/TenantsProvider'; import { useConfirmModal } from './use-confirm-modal'; +import useRedirectUri from './use-redirect-uri'; export class RequestError extends Error { constructor( @@ -33,7 +33,7 @@ export const useStaticApi = ({ prefixUrl, hideErrorToast, resourceIndicator }: S const { isAuthenticated, getAccessToken, signOut } = useLogto(); const { t, i18n } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { show } = useConfirmModal(); - const href = useHref('/'); + const postSignOutRedirectUri = useRedirectUri('signOut'); const toastError = useCallback( async (response: Response) => { @@ -51,7 +51,7 @@ export const useStaticApi = ({ prefixUrl, hideErrorToast, resourceIndicator }: S cancelButtonText: 'general.got_it', }); - await signOut(href); + await signOut(postSignOutRedirectUri.href); return; } @@ -60,7 +60,7 @@ export const useStaticApi = ({ prefixUrl, hideErrorToast, resourceIndicator }: S toast.error(httpCodeToMessage[response.status] ?? fallbackErrorMessage); } }, - [show, signOut, t, href] + [show, signOut, t, postSignOutRedirectUri] ); const api = useMemo( @@ -73,7 +73,6 @@ export const useStaticApi = ({ prefixUrl, hideErrorToast, resourceIndicator }: S !hideErrorToast && (async (error) => { await toastError(error.response); - return error; }) ), diff --git a/packages/console/src/hooks/use-redirect-uri.ts b/packages/console/src/hooks/use-redirect-uri.ts index 25708075f..85129c841 100644 --- a/packages/console/src/hooks/use-redirect-uri.ts +++ b/packages/console/src/hooks/use-redirect-uri.ts @@ -1,5 +1,5 @@ import { ossConsolePath } from '@logto/schemas'; -import { conditionalArray } from '@silverhand/essentials'; +import { conditionalArray, joinPath } from '@silverhand/essentials'; import { useHref } from 'react-router-dom'; import { isCloud } from '@/consts/env'; @@ -11,7 +11,7 @@ import { isCloud } from '@/consts/env'; */ const useRedirectUri = (flow: 'signIn' | 'signOut' = 'signIn') => { const path = useHref( - conditionalArray(!isCloud && ossConsolePath, flow === 'signIn' && '/callback').join('') + joinPath(...conditionalArray(!isCloud && ossConsolePath, flow === 'signIn' ? '/callback' : '/')) ); return new URL(path, window.location.origin);