Fix redirect after logout

This commit is contained in:
Eduard Heimbuch
2021-03-02 13:59:07 +01:00
parent bac8ee246c
commit 2a425af205
3 changed files with 21 additions and 6 deletions

View File

@@ -206,7 +206,7 @@ describe("Test login hooks", () => {
const queryClient = createInfiniteCachingClient();
setIndexLink(queryClient, "logout", "/logout");
fetchMock.deleteOnce("/api/v2/logout", "");
fetchMock.deleteOnce("/api/v2/logout", {});
const { result, waitForNextUpdate } = renderHook(() => useLogout(), {
wrapper: createWrapper(undefined, queryClient)

View File

@@ -23,6 +23,7 @@
*/
import { Me } from "@scm-manager/ui-types";
import { useEffect } from "react";
import { useMutation, useQuery } from "react-query";
import { apiClient } from "./apiclient";
import { ApiResult, useIndexLink } from "./base";
@@ -95,21 +96,31 @@ export const useLogin = () => {
};
};
type LogoutResponse = {
logoutRedirect?: string;
};
export const useLogout = () => {
const link = useIndexLink("logout");
const reset = useReset();
const { mutate, isLoading, error, data } = useMutation<boolean, Error, unknown>(
() => apiClient.delete(link!).then(() => true),
{
onSuccess: reset
}
const { mutate, isLoading, error, data } = useMutation<LogoutResponse, Error, unknown>(() =>
apiClient.delete(link!).then(r => (r.status === 200 ? r.json() : {}))
);
const logout = () => {
mutate({});
};
useEffect(() => {
if (data?.logoutRedirect) {
window.location.assign(data.logoutRedirect);
}
if (data) {
reset();
}
}, [data, reset]);
return {
logout: link && !data ? logout : undefined,
isLoading,