redux-auth icon indicating copy to clipboard operation
redux-auth copied to clipboard

Accessing auth state from a different reducer

Open nyjy85 opened this issue 8 years ago • 2 comments

Is there a way for me to access the auth state from a different reducer?

I'm trying to update User information when user's edit their profile, but I can't access or set user information from the auth state if I'm not in the authReducer. And there's no component available that allows users to edit their information (with the exception of password). Does anyone have a work around or suggestions?

nyjy85 avatar Apr 06 '16 16:04 nyjy85

You can write a middleware that will look for your action type for updating user profile and extend payload with data taken from authReducer. It will be something like:


const extendUserProfile = store => next => action => {
  const state = store.getState();
  const attributes = state.auth.getIn(['user', 'attributes']);
  if (action.type === SET_USER_PROFILE && attributes) {
    action.payload.attributes = attributes;
    return next(action);
  }

  return next(action);
};

andrei-yanovich avatar Apr 07 '16 18:04 andrei-yanovich

Ah, nice, thanks! I was able to work around it by setting a separate user state in the store (instead of trying to use the user property in auth.user), and whenever 'EMAIL_SIGN_IN_COMPLETE' action was triggered, I would set the user state from the payload. Then I just use that data for profile editing/updating. But I like the middleware approach much better.

nyjy85 avatar Apr 08 '16 15:04 nyjy85