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

@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Fixed
- Fix redirect after logout if is set
## [2.14.0] - 2021-03-01 ## [2.14.0] - 2021-03-01
### Added ### Added
- Repository data can be migrated independently to enable the import of dumps from older versions ([#1526](https://github.com/scm-manager/scm-manager/pull/1526)) - Repository data can be migrated independently to enable the import of dumps from older versions ([#1526](https://github.com/scm-manager/scm-manager/pull/1526))

View File

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

View File

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