diff --git a/src/auth/AuthProvider.test.tsx b/src/auth/AuthProvider.test.tsx index f1ce0fcc0..6ada0e4f7 100644 --- a/src/auth/AuthProvider.test.tsx +++ b/src/auth/AuthProvider.test.tsx @@ -2,6 +2,7 @@ import { createSaleorClient, SaleorProvider } from "@saleor/sdk"; import setupApi from "@test/api"; import { act, renderHook } from "@testing-library/react-hooks"; import React from "react"; +import { MemoryRouter as Router } from "react-router-dom"; import { useAuthProvider } from "./hooks/useAuthProvider"; @@ -17,7 +18,9 @@ function renderAuthProvider() { channel: "" }); const wrapper = ({ children }) => ( - {children} + + {children} + ); const { result } = renderHook( diff --git a/src/auth/hooks/useAuthProvider.ts b/src/auth/hooks/useAuthProvider.ts index 48c0b8d02..9843d0d6a 100644 --- a/src/auth/hooks/useAuthProvider.ts +++ b/src/auth/hooks/useAuthProvider.ts @@ -1,5 +1,6 @@ import { IMessageContext } from "@saleor/components/messages"; import { APP_DEFAULT_URI, APP_MOUNT_URI, DEMO_MODE } from "@saleor/config"; +import useNavigator from "@saleor/hooks/useNavigator"; import { commonMessages } from "@saleor/intl"; import { GetExternalAccessTokenData, @@ -46,6 +47,7 @@ export function useAuthProvider({ getExternalAccessToken, logout } = useAuth(); + const navigate = useNavigator(); const { authenticated, authenticating, user } = useAuthState(); const [error, setError] = useState(); @@ -67,12 +69,12 @@ export function useAuthProvider({ }); const handleLogout = async () => { + const path = APP_MOUNT_URI === APP_DEFAULT_URI ? "" : APP_MOUNT_URI; + const returnTo = urlJoin(window.location.origin, path); + const result = await logout({ input: JSON.stringify({ - returnTo: urlJoin( - window.location.origin, - APP_MOUNT_URI === APP_DEFAULT_URI ? "" : APP_MOUNT_URI - ) + returnTo } as RequestExternalLogoutInput) }); @@ -80,17 +82,21 @@ export function useAuthProvider({ navigator.credentials.preventSilentAccess(); } - if (!result) { - return; + const errors = result?.errors || []; + + const externalLogoutUrl = result + ? JSON.parse(result.data?.externalLogout?.logoutData || null)?.logoutUrl + : ""; + + if (!errors.length) { + if (externalLogoutUrl) { + window.location.href = externalLogoutUrl; + } else { + navigate(path); + } } - const errors = result.errors || []; - const logoutUrl = JSON.parse( - result.data?.externalLogout?.logoutData || null - )?.logoutUrl; - if (!errors.length && logoutUrl) { - window.location.href = logoutUrl; - } + return; }; const handleLogin = async (email: string, password: string) => {