Redirection to screen "Home" after validating login - React Native + Firebase

0

I need to implement in the code below the command for once the user is validated to be redirected to the "Home" screen. The changes I made did not solve my problem. I can create the user, save in Firebase but when I log in I can not direct to the "Home" screen.

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
  KeyboardAvoidingView,
  StyleSheet,
  ImageBackground,
} from 'react-native';
import Login from './screens/Login';
import Register from './screens/Register';
import ForgotPassword from './screens/ForgotPassword';
import { w } from './api/Dimensions';
import Home from './screens/Home';

export default class FirebaseLogin extends Component {
  state = {
    currentScreen: 'login', // can be: 'login' or 'register' or 'forgot'
  };

  changeScreen = screenName  => () => {
    this.setState({ currentScreen: screenName  });
  };

  userSuccessfullyLoggedIn = user => {
    this.props.login(user);
  };

  render() {
    let screenToShow;

    switch (this.state.currentScreen) {
      case 'login':
        screenToShow = (
          <Login
            change={this.changeScreen}
            success={this.userSuccessfullyLoggedIn}
          />
        );
        break;
      case 'register':
        screenToShow = <Register change={this.changeScreen} />;
        break;
      case 'forgot':
        screenToShow = <ForgotPassword change={this.changeScreen} />;
        break;
    }

    return (
      <KeyboardAvoidingView
        behavior="position"
        keyboardVerticalOffset={-w(40)}
        style={styles.container}>
        <ImageBackground
          source={this.props.background}
          style={styles.background}
          resizeMode="stretch">
          {screenToShow}
        </ImageBackground>
      </KeyboardAvoidingView>
    );
  }
}

FirebaseLogin.propTypes = {
  login: PropTypes.func.isRequired,
};

FirebaseLogin.defaultProps = {
  background: null,
};
    
asked by anonymous 30.11.2018 / 13:38

1 answer

0

Try this, as you can see, he keeps checking to see if any users have authenticated, and then redirects. Was that your doubt?

componentDidMount() {
firebase.auth().onAuthStateChanged(user => {
  if (user != null) {
    this.props.navigation.navigate('Home')
  }
})

}

    
02.12.2018 / 11:45