Fix log out endless loop which occurred on slow connections and could tear down the SCM-Manager server.

This commit is contained in:
Eduard Heimbuch
2021-11-09 16:17:42 +01:00
parent f680c9335a
commit 49195fd2e1
2 changed files with 16 additions and 13 deletions

View File

@@ -0,0 +1,2 @@
- type: fixed
description: Fix logout endless loop on slow connections

View File

@@ -28,17 +28,18 @@ import { apiClient } from "./apiclient";
import { ApiResult, useIndexLink } from "./base";
import { useLegacyContext } from "./LegacyContext";
import { useReset } from "./reset";
import { useCallback } from "react";
export const useMe = (): ApiResult<Me> => {
const legacy = useLegacyContext();
const link = useIndexLink("me");
return useQuery<Me, Error>("me", () => apiClient.get(link!).then((response) => response.json()), {
return useQuery<Me, Error>("me", () => apiClient.get(link!).then(response => response.json()), {
enabled: !!link,
onSuccess: (me) => {
onSuccess: me => {
if (legacy.onMeFetched) {
legacy.onMeFetched(me);
}
},
}
});
};
@@ -60,7 +61,7 @@ export const useSubject = () => {
isAnonymous,
isLoading,
error,
me,
me
};
};
@@ -75,9 +76,9 @@ export const useLogin = () => {
const link = useIndexLink("login");
const reset = useReset();
const { mutate, isLoading, error } = useMutation<unknown, Error, Credentials>(
(credentials) => apiClient.post(link!, credentials),
credentials => apiClient.post(link!, credentials),
{
onSuccess: reset,
onSuccess: reset
}
);
@@ -91,7 +92,7 @@ export const useLogin = () => {
return {
login: link ? login : undefined,
isLoading,
error,
error
};
};
@@ -104,24 +105,24 @@ export const useLogout = () => {
const reset = useReset();
const { mutate, isLoading, error, data } = useMutation<LogoutResponse, Error, unknown>(
() => apiClient.delete(link!).then((r) => (r.status === 200 ? r.json() : {})),
() => apiClient.delete(link!).then(r => (r.status === 200 ? r.json() : {})),
{
onSuccess: (response) => {
onSuccess: response => {
if (response?.logoutRedirect) {
window.location.assign(response.logoutRedirect);
}
return reset();
},
}
}
);
const logout = () => {
const logout = useCallback(() => {
mutate({});
};
}, [mutate]);
return {
logout: link && !data ? logout : undefined,
isLoading,
error,
error
};
};