do not show login error, when redirecting

This commit is contained in:
Sebastian Sdorra
2019-04-17 11:45:44 +02:00
parent 20cf0507a2
commit 2b2d7ea0e9
3 changed files with 66 additions and 24 deletions

View File

@@ -11,7 +11,6 @@ import {
fetchIndexResourcesPending,
fetchIndexResourcesSuccess
} from "./indexResource";
import type { History } from "history";
// Action
@@ -30,10 +29,7 @@ export const LOGOUT = "scm/auth/LOGOUT";
export const LOGOUT_PENDING = `${LOGOUT}_${types.PENDING_SUFFIX}`;
export const LOGOUT_SUCCESS = `${LOGOUT}_${types.SUCCESS_SUFFIX}`;
export const LOGOUT_FAILURE = `${LOGOUT}_${types.FAILURE_SUFFIX}`;
type LogoutRedirection = {
logoutRedirect: string
};
export const LOGOUT_REDIRECT = `${LOGOUT}_REDIRECT`;
// Reducer
@@ -59,6 +55,13 @@ export default function reducer(
case LOGOUT_SUCCESS:
return initialState;
case LOGOUT_REDIRECT: {
// we keep the current state until we are redirected to the new page
return {
...state,
redirecting: true
};
}
default:
return state;
}
@@ -94,10 +97,16 @@ export const logoutPending = () => {
export const logoutSuccess = () => {
return {
type: LOGOUT_SUCCESS
type: LOGOUT_SUCCESS,
};
};
export const redirectAfterLogout = () => {
return {
type: LOGOUT_REDIRECT
}
};
export const logoutFailure = (error: Error) => {
return {
type: LOGOUT_FAILURE,
@@ -190,7 +199,7 @@ export const fetchMe = (link: string) => {
};
};
export const logout = (link: string, history: History) => {
export const logout = (link: string) => {
return function(dispatch: any) {
dispatch(logoutPending());
return apiClient
@@ -199,17 +208,24 @@ export const logout = (link: string, history: History) => {
return response.status === 200
? response.json()
: new Promise(function(resolve) {
resolve(undefined);
resolve();
});
})
.then(json => {
let fetchIndex = true;
if (json && json.logoutRedirect) {
dispatch(redirectAfterLogout());
window.location.assign(json.logoutRedirect);
fetchIndex = false;
} else {
dispatch(logoutSuccess());
}
dispatch(logoutSuccess());
return fetchIndex;
})
.then(() => {
dispatch(fetchIndexResources());
.then((fetchIndex: boolean) => {
if (fetchIndex) {
dispatch(fetchIndexResources());
}
})
.catch(error => {
dispatch(logoutFailure(error));
@@ -257,3 +273,8 @@ export const isLogoutPending = (state: Object) => {
export const getLogoutFailure = (state: Object) => {
return getFailure(state, LOGOUT);
};
export const isRedirecting = (state: Object) => {
return !!stateAuth(state).redirecting;
};

View File

@@ -26,7 +26,7 @@ import reducer, {
FETCH_ME,
LOGOUT,
getLoginFailure,
getLogoutFailure,
getLogoutFailure, isRedirecting, LOGOUT_REDIRECT, redirectAfterLogout,
} from "./auth";
import configureMockStore from "redux-mock-store";
@@ -70,6 +70,17 @@ describe("auth reducer", () => {
expect(state.authenticated).toBeUndefined();
});
it("should keep state and set redirecting to true", () => {
const initialState = {
authenticated: true,
me
};
const state = reducer(initialState, redirectAfterLogout());
expect(state.me).toBe(initialState.me);
expect(state.authenticated).toBe(initialState.authenticated);
expect(state.redirecting).toBe(true);
});
it("should set state authenticated and me after login", () => {
const state = reducer(undefined, loginSuccess(me));
expect(state.me).toBe(me);
@@ -246,8 +257,7 @@ describe("auth actions", () => {
const expectedActions = [
{ type: LOGOUT_PENDING },
{ type: LOGOUT_SUCCESS },
{ type: FETCH_INDEXRESOURCES_PENDING }
{ type: LOGOUT_REDIRECT }
];
const store = mockStore({});
@@ -257,8 +267,6 @@ describe("auth actions", () => {
"http://example.com/cas/logout"
);
expect(store.getActions()).toEqual(expectedActions);
// expect(window.location.href).toEqual("http://example.com/cas/logout");
});
});
@@ -345,4 +353,16 @@ describe("auth selectors", () => {
it("should return unknown, if failure state is not set for LOGOUT", () => {
expect(getLogoutFailure({})).toBeUndefined();
});
it("should return false, if redirecting is not set", () => {
expect(isRedirecting({})).toBe(false);
});
it("should return false, if redirecting is false", () => {
expect(isRedirecting({auth: { redirecting: false }})).toBe(false);
});
it("should return true, if redirecting is true", () => {
expect(isRedirecting({auth: { redirecting: true }})).toBe(true);
});
});