My first React project. What I want to do is to first display the logo and then check if the user is logged in. If they are they go straight to next page. If not the login form appears below the logo in the same page. Similar to how the Facebook app works when you open it.
What i got so far using Firebase for auth:
loginCheck(){
const { navigate } = this.props.navigation;
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
navigate('Feed');
} else {
return (
<View style={styles.formContainer}>
<FormLabel>Email</FormLabel>
<FormInput
onChangeText={(email) => this.setState({ email })}/>
<FormLabel>Password</FormLabel>
<FormInput
secureTextEntry
onChangeText={(password) => this.setState({ password })}/>
<Text style={styles.statusText}>{ this.state.error }</Text>
{this.renderButtonOrLoading()}
</View>
)
}
});
}
render() {
return (
<KeyboardAvoidingView behavior="padding" style={styles.container}>
<View style={styles.loginContainer}>
<Image resizeMode="contain" style={styles.logo} source={require('../../components/images/logo.png')} />
</View>
{this.loginCheck()}
</KeyboardAvoidingView>
);
}
}
Aim is to get everything in the return statement of loginCheck() to show in render if user is not logged in. I know my approach is wrong, just don’t know how to go about fixing it.
The renderButtonOrLoading() seems to work fine, however, when I replace {this.loginCheck()} in render() with everything in the return statement of loginCheck().
renderButtonOrLoading(){
if(this.state.loading){
return (<Text style={styles.statusText}>Loading</Text>);
}
return (
<View>
<Button
onPress={this.onLoginPress.bind(this)}
title='Login'/>
<Button
onPress={this.onSignupPress.bind(this)}
title='Sign Up'/>
</View>
);
}
Would appreciate any help, been stuck on this problem for a while.