how to clean a ngrx store when logged out

377 Views Asked by At

I want to clean all my states in the store after logging out . this is my reducers/index.ts

export interface AppState {

}
export const reducers: ActionReducerMap<AppState> = {
router: routerReducer
};
export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [storeFreeze] : [];

my app.module.ts

    StoreModule.forRoot(reducers, {metaReducers:[]}) 

beside i already has my auth reducer(authState) with two actions login and logout

1

There are 1 best solutions below

0
Mohamed Jamel On

I create a meta-reducer function in my index that when user logged out it will clear the store

export function clearState(reducer) {
  return function (state, action) {

    if (action.type === Auth.AuthActionsTypes.LogoutAction) {
      state = undefined;
    }

    return reducer(state, action);
  };
}

and then added it in my app.module

StoreModule.forRoot(reducers, {metaReducers:[clearState]})