Source: server/util/secureReduxState.js

import compose from 'redux/lib/compose';
import stripPrivateEnvironmentConfig from './stripPrivateEnvironmentConfig';
import nonSerializableReducer from '../../app/reducers/nonSerializableReducer';
import asyncReducer from '../../app/reducers/asyncReducer';

/** @module */

/**
 * Removes the authentication tokens from the authentication reducer
 * @function secureAuthTokens
 * @private
 * @param state The current redux state
 */
const secureAuthState = state => {
  if (state.authentication) {
    const authenticationState = state.authentication;
    /* eslint-disable-next-line no-underscore-dangle */
    return {
      ...state,
      authentication: {
        ...authenticationState,
        accessToken: null,
        idToken: null,
      },
    };
  }

  return state;
};

/**
 * Removes the private environment config (all keys that have been prefixed with '__')
 * from the authentication reducer
 * @function secureEnvironmentConfig
 * @private
 * @param state The current redux state
 */
const secureEnvironmentConfig = state => ({
  ...state,
  config: {
    ...state.config,
    environmentConfig: stripPrivateEnvironmentConfig(state.config.environmentConfig),
  },
});

/**
 * Strips the non-serializable state (as we cannot serialize it to send it to the client).
 * @function stripNonSerializable
 * @private
 * @param state The current redux state
 */
const stripNonSerializable = state => ({
  ...state,
  nonSerializable: nonSerializableReducer(undefined, { type: '@@INIT' }),
});

/**
 * Strips the performed async actions. They are used for the ProgressBar, but the progress
 * of server actions is not relevant for the client
 * @function stripAsyncActions
 * @private
 * @param state The current redux state
 */
const stripAsyncActions = state => ({
  ...state,
  async: asyncReducer(undefined, { type: '@@INIT' }),
});

/**
 * Filters the current redux store state before exposing it to the client
 * @function secureReduxState
 * @param state The redux store state
 * @returns {object} The secured redux store state that will be serialized and passed to
 * the client.
 * @default
 */
const secureReduxState = compose(
  secureEnvironmentConfig,
  secureAuthState,
  stripNonSerializable,
  stripAsyncActions,
);

export default secureReduxState;