Redirecting on login
Here, things will get a bit more tricky. First, we'll do some preparation.
Inside our LoginContainer
, when it comes to our signup
and login
methods, we currently just console.log
out the result in our then
statement. In other words, we don't actually do anything once our user logs in:
signup() {
firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(res => {
console.log(res);
}).catch(error => {
console.log(error);
this.setState({ error: 'Error signing up.' });
})
}
Let's change this bit (in both signup
and login
) to call another method, onLogin
:
login() {
firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
.then(res => {
this.onLogin();
}).catch((error) => {
if (error.code === 'auth/user-not-found') {
this.signup();
} else {
this.setState({ error: 'Error logging in.' });
}
});
}
Then, we can define our onLogin
method:
onLogin...