Logout redirects user to home page (#1721)

* Redirect to home page on logout

* Change internal redirect to use router

* Update authProvider test
This commit is contained in:
Wojciech Mista 2022-01-11 13:32:59 +01:00 committed by GitHub
parent 3bb7209db7
commit d4a28fedf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 14 deletions

View file

@ -2,6 +2,7 @@ import { createSaleorClient, SaleorProvider } from "@saleor/sdk";
import setupApi from "@test/api"; import setupApi from "@test/api";
import { act, renderHook } from "@testing-library/react-hooks"; import { act, renderHook } from "@testing-library/react-hooks";
import React from "react"; import React from "react";
import { MemoryRouter as Router } from "react-router-dom";
import { useAuthProvider } from "./hooks/useAuthProvider"; import { useAuthProvider } from "./hooks/useAuthProvider";
@ -17,7 +18,9 @@ function renderAuthProvider() {
channel: "" channel: ""
}); });
const wrapper = ({ children }) => ( const wrapper = ({ children }) => (
<SaleorProvider client={saleorClient}>{children}</SaleorProvider> <Router>
<SaleorProvider client={saleorClient}>{children}</SaleorProvider>
</Router>
); );
const { result } = renderHook( const { result } = renderHook(

View file

@ -1,5 +1,6 @@
import { IMessageContext } from "@saleor/components/messages"; import { IMessageContext } from "@saleor/components/messages";
import { APP_DEFAULT_URI, APP_MOUNT_URI, DEMO_MODE } from "@saleor/config"; import { APP_DEFAULT_URI, APP_MOUNT_URI, DEMO_MODE } from "@saleor/config";
import useNavigator from "@saleor/hooks/useNavigator";
import { commonMessages } from "@saleor/intl"; import { commonMessages } from "@saleor/intl";
import { import {
GetExternalAccessTokenData, GetExternalAccessTokenData,
@ -46,6 +47,7 @@ export function useAuthProvider({
getExternalAccessToken, getExternalAccessToken,
logout logout
} = useAuth(); } = useAuth();
const navigate = useNavigator();
const { authenticated, authenticating, user } = useAuthState(); const { authenticated, authenticating, user } = useAuthState();
const [error, setError] = useState<UserContextError>(); const [error, setError] = useState<UserContextError>();
@ -67,12 +69,12 @@ export function useAuthProvider({
}); });
const handleLogout = async () => { 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({ const result = await logout({
input: JSON.stringify({ input: JSON.stringify({
returnTo: urlJoin( returnTo
window.location.origin,
APP_MOUNT_URI === APP_DEFAULT_URI ? "" : APP_MOUNT_URI
)
} as RequestExternalLogoutInput) } as RequestExternalLogoutInput)
}); });
@ -80,17 +82,21 @@ export function useAuthProvider({
navigator.credentials.preventSilentAccess(); navigator.credentials.preventSilentAccess();
} }
if (!result) { const errors = result?.errors || [];
return;
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 || []; return;
const logoutUrl = JSON.parse(
result.data?.externalLogout?.logoutData || null
)?.logoutUrl;
if (!errors.length && logoutUrl) {
window.location.href = logoutUrl;
}
}; };
const handleLogin = async (email: string, password: string) => { const handleLogin = async (email: string, password: string) => {