mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
Fix redirect after logout
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user