Clear store after logout

This commit is contained in:
Vesa Luusua 2017-09-14 19:53:15 +03:00
parent f4a3dcad5e
commit b76cbadca3
3 changed files with 17 additions and 2 deletions

View file

@ -20,6 +20,10 @@ export const SIGNUP_REQUEST = 'app/Auth/SIGNUP_REQUEST';
export const SIGNUP_SUCCESS = 'app/Auth/SIGNUP_SUCCESS';
export const SIGNUP_ERROR = 'app/Auth/SIGNUP_ERROR';
// Generic user_logout action that can be handled elsewhere
// E.g. src/reducers.js clears store as a consequence
export const USER_LOGOUT = 'app/USER_LOGOUT';
// ================ Reducer ================ //
const initialState = {
@ -110,6 +114,8 @@ export const signupRequest = () => ({ type: SIGNUP_REQUEST });
export const signupSuccess = () => ({ type: SIGNUP_SUCCESS });
export const signupError = error => ({ type: SIGNUP_ERROR, payload: error, error: true });
export const userLogout = () => ({ type: USER_LOGOUT });
// ================ Thunks ================ //
export const authInfo = () =>
@ -150,6 +156,7 @@ export const logout = () =>
.logout()
.then(() => dispatch(clearCurrentUser()))
.then(() => dispatch(logoutSuccess()))
.then(() => dispatch(userLogout()))
.catch(e => dispatch(logoutError(e)));
};

View file

@ -15,6 +15,7 @@ import reducer, {
signupRequest,
signupSuccess,
signupError,
userLogout,
} from './Auth.duck';
// Create a dispatch function that correctly calls the thunk functions
@ -276,6 +277,7 @@ describe('Auth duck', () => {
[logoutRequest()],
[clearCurrentUser()],
[logoutSuccess()],
[userLogout()],
]);
});
});

View file

@ -1,4 +1,5 @@
import { combineReducers } from 'redux';
import { USER_LOGOUT } from './ducks/Auth.duck';
import * as globalReducers from './ducks';
import * as pageReducers from './containers/reducers';
@ -9,8 +10,13 @@ import * as pageReducers from './containers/reducers';
* which is page specific.
* Future: this structure could take in asyncReducers, which are changed when you navigate pages.
*/
const createReducer = function createReducer() {
return combineReducers({ ...globalReducers, ...pageReducers });
const appReducer = combineReducers({ ...globalReducers, ...pageReducers });
const createReducer = () => {
return (state, action) => {
const appState = action.type === USER_LOGOUT ? undefined : state;
return appReducer(appState, action);
};
};
export default createReducer;