3
votes

I am working on a React Native project and using Firebase.

I am trying to get the user to log in after (s)he has verified their email address. I send the user an email on registration, the user clicks on the verification link to verify themselves and then should be able to logon. My current code allows the user to log in post verification but only after I have refreshed the app. I would want the user to login after the verification without refreshing the app. I found out that I can achieve it using getIdToken() in Firebase. But somehow I can't seem to get it working. Any pointers where and what I am doing wrong? Thanks in Advance.

My code snippet for this function is:

_login = () =>{
var me = this;
firebaseRef.auth().onAuthStateChanged(function(user) {
  if(user){
    user.getIdToken(forceRefresh).then(function() {
      if( firebaseRef.auth().currentUser.emailVerified){
      firebaseRef.auth().signInWithEmailAndPassword(this.state.email, this.state.password).then(function(){
        // some function here, which is working perfectly
      }, 
      function(error) {
        alert('The username or password is incorrect');
          console.log(error);
      })
      }
      else {
        alert('Your email has not been verified');
      }
    },
    function(error) {
      alert('There is an email verification error');
        console.log(error);
    })
  } 
}
  ) 
}
1

1 Answers

0
votes
    firebase.auth().signInWithEmailAndPassword(email, password)
                .then(() => {
                    if(firebase.auth().currentUser.emailVerified === false) {
                        Alert.alert('Message')
                    } else {
Actions.screen()
                }})
                .catch(erro => Alert.alert(erro);
        }