Skip to content

Instantly share code, notes, and snippets.

@shubhnik
Last active January 15, 2018 10:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shubhnik/9f7bafd3145e66434705cd1e995d1356 to your computer and use it in GitHub Desktop.
Save shubhnik/9f7bafd3145e66434705cd1e995d1356 to your computer and use it in GitHub Desktop.
Our new navigationReducer capable of handling authentication flow.
// Don't forget to check the complete code here ---> https://github.com/shubhnik/redux-react-navigation/tree/authFlow
import { NavigationActions } from "react-navigation";
// AppNavigator is the one defined in the start of PART-2 of this blog --> https://medium.com/@shubhnik/a-comprehensive-guide-for-integrating-react-navigation-with-redux-including-authentication-flow-cb7b90611adf
const ActionForLoggedOut = AppNavigator.router.getActionForPathAndParams("login");
const ActionForLoggedIn = AppNavigator.router.getActionForPathAndParams("screen1");
const stateForLoggedOut = AppNavigator.router.getStateForAction(ActionForLoggedOut);
const stateForLoggedIn = AppNavigator.router.getStateForAction(ActionForLoggedIn);
const initialState = { stateForLoggedOut, stateForLoggedIn };
const navigationReducer = (state = initialState, action) => {
switch (action.type) {
case "@@redux/INIT":
return {
...state,
stateForLoggedIn: AppNavigator.router.getStateForAction(ActionForLoggedIn, stateForLoggedOut)
};
case "LOGIN":
return {
...state,
stateForLoggedIn: AppNavigator.router.getStateForAction(ActionForLoggedIn, stateForLoggedOut)
};
case "LOGOUT":
return {
...state,
stateForLoggedOut: AppNavigator.router.getStateForAction(
NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: "login" })]
})
)
};
default:
return {
...state,
stateForLoggedIn: AppNavigator.router.getStateForAction(action,state.stateForLoggedIn)
};
}
};
export default navigationReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment