3

My skills with firebase is basic, what i want to get is, only the users who have the verified mail will be able to enter the app, if not show the error of the email that has not been verified. here my code:

login (){

    const user = firebase.auth().currentUser;
    const emailVerified = user.emailVerified;

    const validate = this.refs.formId.getValue();
    if (validate && emailVerified != 'false') {
        firebase.auth().signInWithEmailAndPassword(validate.email, validate.password)
        .then(() => {

        })
        .catch((error) => {
            const errorCode = error.code;
            const errorMessage = error.message;
            if (errorCode === 'auth/wrong-password') {

            Toast.show({ text: 'Wrong password!', position: 'bottom', buttonText: 'Try Again' })

            if (emailVerified === 'false') {

            Toast.show({ text: 'Email Not Verified!', position: 'bottom', buttonText: 'Try Again' })

            }else{
            Toast.show({ text: 'Something Wrong!', position: 'bottom', buttonText: 'Try Again' })
            }

        });
    }

}

I get this error: null is not an object (evaluating 'user.emailVerified)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
bdroid
  • 606
  • 2
  • 12
  • 27

1 Answers1

1

From notice under the example just like your way to utilize firebase.auth().currentUser in documentation:

Note: currentUser might also be null because the auth object has not finished initializing. If you use an observer to keep track of the user's sign-in status, you don't need to handle this case.

@bdroid, you don't need to integrate them, by reforming the flow of this login process. I think this also provides a proper login flow, call signInWithEmailAndPassword first, after detecting if the user is verified, decide what to do separately on complete authorization login and uncompleted one:

login (){

  const validate = this.refs.formId.getValue();
  firebase.auth().signInWithEmailAndPassword(validate.email, validate.password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    if (errorCode === 'auth/wrong-password') {
      Toast.show({ text: 'Wrong password!', position: 'bottom', buttonText: 'Try Again' });
    }

  });

  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      if (user.emailVerified === false) {
        Toast.show({ text: 'Email Not Verified!', position: 'bottom', buttonText: 'Try Again' });
      } else {

        // successful login 

      }
    } else {
      //  Toast.show({ text: 'Something Wrong!', position: 'bottom', buttonText: 'No user is signed in.' }); 
    }
  });

}
Carr
  • 2,691
  • 1
  • 19
  • 27
  • I test it but i still can login when my the . email is not verified – bdroid Apr 15 '18 at 13:39
  • i get this error: null is not an object (evaluating 'validate.email) – bdroid Apr 15 '18 at 13:40
  • @bdroid It's seem the problem is not at the `firebase.auth()` at all, you don't fetch the value in the form properly in very first. – Carr Apr 15 '18 at 13:42
  • @bdorid, but I'm wondering how you can login when `validate` is null ? `I test it but i still can login when my the . email is not verified` – Carr Apr 15 '18 at 13:45
  • i have the form:
    – bdroid Apr 15 '18 at 13:47
  • @bdroid, I've updated the answer that I remove the single quote around `'false'`, if the method return boolean value, it causes problem, but this does not explain why `validate` being `null` yet. – Carr Apr 15 '18 at 13:49
  • @bdroid, and make sure you deploy this login function in `componentDidMount()` or after the component is rendered. `ref` could not refer to element which is not mounted yet. – Carr Apr 15 '18 at 13:51
  • @bdroid, you need to explain this more clearly, you said the user logged in, but you said there is error message: validate is null – Carr Apr 15 '18 at 14:37