redux-auth
redux-auth copied to clipboard
Accessing auth state from a different reducer
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?
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);
};
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.