From 2a425af205965aa828b6d1a037c32d00aa9aab3a Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Tue, 2 Mar 2021 13:59:07 +0100 Subject: [PATCH] Fix redirect after logout --- CHANGELOG.md | 4 ++++ scm-ui/ui-api/src/login.test.ts | 2 +- scm-ui/ui-api/src/login.ts | 21 ++++++++++++++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e2af4e9e4..ae0b3de928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), 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 ### 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)) diff --git a/scm-ui/ui-api/src/login.test.ts b/scm-ui/ui-api/src/login.test.ts index 3c83ea1f27..9b332d6a52 100644 --- a/scm-ui/ui-api/src/login.test.ts +++ b/scm-ui/ui-api/src/login.test.ts @@ -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) diff --git a/scm-ui/ui-api/src/login.ts b/scm-ui/ui-api/src/login.ts index eba8c112c6..b4daab0eb4 100644 --- a/scm-ui/ui-api/src/login.ts +++ b/scm-ui/ui-api/src/login.ts @@ -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( - () => apiClient.delete(link!).then(() => true), - { - onSuccess: reset - } + const { mutate, isLoading, error, data } = useMutation(() => + 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,