Fix missing redirect after login (#1592)

Each unauthorized error was caught by the token expired handler, which has reset the whole query state and this leads sometimes to a missing redirect after login.
This commit is contained in:
Sebastian Sdorra
2021-03-16 16:13:43 +01:00
committed by GitHub
parent ce0e94098e
commit 9ef076fb2f
8 changed files with 98 additions and 19 deletions

View File

@@ -23,7 +23,15 @@
*/
import { contextPath } from "./urls";
import { BackendErrorContent, createBackendError, ForbiddenError, isBackendError, UnauthorizedError } from "./errors";
import {
BackendErrorContent,
createBackendError,
ForbiddenError,
isBackendError,
TOKEN_EXPIRED_ERROR_CODE,
TokenExpiredError,
UnauthorizedError
} from "./errors";
type SubscriptionEvent = {
type: string;
@@ -119,6 +127,15 @@ const applyFetchOptions: (p: RequestInit) => RequestInit = o => {
function handleFailure(response: Response) {
if (!response.ok) {
if (response.status === 401) {
if (isBackendError(response)) {
return response.json().then((content: BackendErrorContent) => {
if (content.errorCode === TOKEN_EXPIRED_ERROR_CODE) {
throw new TokenExpiredError("Token expired", 401);
} else {
throw new UnauthorizedError("Unauthorized", 401);
}
});
}
throw new UnauthorizedError("Unauthorized", 401);
} else if (response.status === 403) {
throw new ForbiddenError("Forbidden", 403);

View File

@@ -72,14 +72,18 @@ export class BackendError extends Error {
export class UnauthorizedError extends Error {
statusCode: number;
constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;
}
}
export class TokenExpiredError extends UnauthorizedError {}
export class ForbiddenError extends Error {
statusCode: number;
constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;

View File

@@ -23,7 +23,6 @@
*/
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";
@@ -111,7 +110,7 @@ export const useLogout = () => {
if (response?.logoutRedirect) {
window.location.assign(response.logoutRedirect);
}
reset();
return reset();
}
}
);