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);