Search code examples
react-nativeauthenticationuser-accounts

React Native: How can I redirect after login to different pages depending on the type of account of a user?


I'm building a react native app using expo and I would like to know how I can send a "UserTypeA" to Homepage and send a "UserTypeB" to Profile upon login.

I have a UserTypeA tab navigator and a UserTypeB tab navigator, with just 2 pages that will be see able by both accounts.

I have my UserTypeA data and UserTypeB data in separate tables so I can identify which user has which type.

Sorry if it's not clear this is my first question.

Thank you for your help!


Solution

  • In your apps main render method, you could do something like this. Basically, you will listen to your redux state and switch main screen depending on the user type.

    class MyApp extends PureComponent {
      constructor(props) {
        super(props);
      }
    
      render() {
        const { auth } = this.props;
        if (auth.userObj.type1) {
          return <Type1MainComponent />;
        } 
        if (auth.userObj.type2) { 
          return <Type2MainComponent />;
        }
        return <LoginScreen />;
      }
    }
    
    function mapStateToProps(state) {
      const { auth } = state;
      return { auth };
    }
    
    export default connect(mapStateToProps)(MyApp);