sathyamurthy
sathyamurthy

Reputation: 9

firebase.auth() error in createusermethod in java Script

This is my java script for Register page for web using firebase authentication.When i fill the email and password till Alert message Testing will execute but firebase.auth()..... it will not execute and the page will get refresh automatically.

    var email=document.getElementById("inemail").value;
    var password=document.getElementById("inpass").value;
    var password1=document.getElementById("inpass1").value;
    var lPassword=password.length;
    var lPassword1=password1.length;
    if(lPassword < 7)
    {
        alert("Password Should more than seven Charecter");
        return;
    }

    else if(password!=password1){
        alert("Correctly Enter the Password");
        return;
    }
    else if(password==password1 || lPassword1==lPassword)
    { 
    alert("Testing");
    firebase.auth().createUserWithEmailAndPassword(email.toString(),password.toString()).catch(function(error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            if (errorCode == 'auth/weak-password') {
              alert('The password is too weak.');
            } else {
              alert(errorMessage);
            }
            console.log(error);
          });

        firebase.auth().onAuthStateChanged(function(user) {
          if (user) {
            var email = user.email;
            alert("Sucessfully Created");
          } 
          else {
           alert("sorry Try again");
          }

        });

    }

}

in This script have any errors?if it have error please help me to find-out.

Upvotes: 1

Views: 83

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

As Ticherhaz explains in his comment, your need to use the then() method to detect when the Promise returned by the asynchronous createUserWithEmailAndPassword() method is fulfilled.

The following should do the trick:

firebase.auth().createUserWithEmailAndPassword(email.toString(),password.toString())
.then(userCredential => {
     var email = userCredential.user.email;
     alert("Sucessfully Created");
})
.catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;
        if (errorCode == 'auth/weak-password') {
          alert('The password is too weak.');
        } else {
          alert(errorMessage);
        }
        console.log(error);
      });
}

Note that, as explained in the doc, "on successful creation of the user account, this user will also be signed in to your application", so you don't need to use the onAuthStateChanged() method in this case.


In one of your comments above you say "only till alert message 'testing' this script is executing". Actually, if there is no error with the new user creation, the rejection handler callback passed to catch() is not called and therefore you don't get a feedback on the fact that the createUserWithEmailAndPassword() was correctly executed. For that you need the then() method.

Upvotes: 2

Related Questions