Skip to content

Commit

Permalink
Clear namespaces on logout (#1171)
Browse files Browse the repository at this point in the history
* Clear namespaces on logout.

* Avoid 302 permanent redirect.

* Simplify action def without payload
  • Loading branch information
absoludity authored Sep 24, 2019
1 parent 59074c0 commit fc3e1ff
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions dashboard/src/actions/auth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ describe("OIDC authentication", () => {
payload: { authenticated: false, oidc: false, defaultNamespace: "" },
type: getType(actions.auth.setAuthenticated),
},
{
type: getType(actions.namespace.clearNamespaces),
},
];

return store.dispatch(actions.auth.expireSession()).then(() => {
Expand Down
9 changes: 8 additions & 1 deletion dashboard/src/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ActionType, createAction } from "typesafe-actions";

import { Auth } from "../shared/Auth";
import { IStoreState } from "../shared/types";
import { clearNamespaces, NamespaceAction } from "./namespace";

export const setAuthenticated = createAction("SET_AUTHENTICATED", resolve => {
return (authenticated: boolean, oidc: boolean, defaultNamespace: string) =>
Expand Down Expand Up @@ -42,10 +43,16 @@ export function authenticate(
};
}

export function logout(): ThunkAction<Promise<void>, IStoreState, null, AuthAction> {
export function logout(): ThunkAction<
Promise<void>,
IStoreState,
null,
AuthAction | NamespaceAction
> {
return async dispatch => {
Auth.unsetAuthToken();
dispatch(setAuthenticated(false, false, ""));
dispatch(clearNamespaces());
};
}

Expand Down
4 changes: 3 additions & 1 deletion dashboard/src/actions/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const errorNamespaces = createAction("ERROR_NAMESPACES", resolve => {
return (err: Error, op: "list") => resolve({ err, op });
});

const allActions = [setNamespace, receiveNamespaces, errorNamespaces];
export const clearNamespaces = createAction("CLEAR_NAMESPACES");

const allActions = [setNamespace, receiveNamespaces, errorNamespaces, clearNamespaces];
export type NamespaceAction = ActionType<typeof allActions[number]>;

export function fetchNamespaces(): ThunkAction<Promise<void>, IStoreState, null, NamespaceAction> {
Expand Down
15 changes: 15 additions & 0 deletions dashboard/src/reducers/namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,19 @@ describe("namespaceReducer", () => {
).toEqual({ ...initialState, errorMsg: err.message });
});
});

context("when CLEAR_NAMESPACES", () => {
const clearedState = {
current: "",
namespaces: [],
};

it("clears any namespaces state", () => {
expect(
namespaceReducer(initialState, {
type: getType(actions.namespace.clearNamespaces),
}),
).toEqual(clearedState);
});
});
});
2 changes: 2 additions & 0 deletions dashboard/src/reducers/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const namespaceReducer = (
return { ...state, current: action.payload };
case getType(actions.namespace.errorNamespaces):
return { ...state, errorMsg: action.payload.err.message };
case getType(actions.namespace.clearNamespaces):
return { ...initialState };
case LOCATION_CHANGE:
const pathname = action.payload.location.pathname;
// looks for /ns/:namespace in URL
Expand Down
3 changes: 3 additions & 0 deletions dashboard/src/shared/AxiosInstance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ describe("createAxiosInterceptorWithAuth", () => {
payload: { authenticated: false, oidc: false, defaultNamespace: "" },
type: "SET_AUTHENTICATED",
},
{
type: "CLEAR_NAMESPACES",
},
];

moxios.stubRequest(testPath, {
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/shared/Namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class Namespace {
}

private static APIBase: string = APIBase;
private static APIEndpoint: string = `${Namespace.APIBase}/api/v1/namespaces`;
private static APIEndpoint: string = `${Namespace.APIBase}/api/v1/namespaces/`;
}

// Set of namespaces used accross the applications as default and "all ns" placeholders
Expand Down

0 comments on commit fc3e1ff

Please sign in to comment.