firebase.auth()中createUser方法的JavaScript错误

huangapple go评论68阅读模式
英文:

firebase.auth() error in createusermethod in java Script

问题

以下是您的JavaScript代码的翻译部分:

function logbtn() {
	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("密码应至少包含七个字符");
		return;
	} else if (password != password1) {
		alert("请正确输入密码");
		return;
	} else if (password == password1 || lPassword1 == lPassword) {
		alert("测试中");
		firebase.auth().createUserWithEmailAndPassword(email.toString(), password.toString()).catch(function (error) {
			var errorCode = error.code;
			var errorMessage = error.message;
			if (errorCode == 'auth/weak-password') {
				alert('密码太弱。');
			} else {
				alert(errorMessage);
			}
			console.log(error);
		});

		firebase.auth().onAuthStateChanged(function (user) {
			if (user) {
				var email = user.email;
				alert("创建成功");
			} else {
				alert("抱歉,请重试");
			}
		});
	}
}

请注意,上述代码中的内容是对您提供的JavaScript代码的翻译。如果您有任何问题或需要进一步的帮助来查找潜在的错误,请随时提问。

英文:

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(&quot;inemail&quot;).value;
	var password=document.getElementById(&quot;inpass&quot;).value;
	var password1=document.getElementById(&quot;inpass1&quot;).value;
	var lPassword=password.length;
	var lPassword1=password1.length;
    if(lPassword &lt; 7)
	{
		alert(&quot;Password Should more than seven Charecter&quot;);
		return;
	}
	
	else if(password!=password1){
		alert(&quot;Correctly Enter the Password&quot;);
		return;
	}
	else if(password==password1 || lPassword1==lPassword)
	{ 
	alert(&quot;Testing&quot;);
	firebase.auth().createUserWithEmailAndPassword(email.toString(),password.toString()).catch(function(error) {
	        var errorCode = error.code;
	        var errorMessage = error.message;
	        if (errorCode == &#39;auth/weak-password&#39;) {
	          alert(&#39;The password is too weak.&#39;);
	        } else {
	          alert(errorMessage);
	        }
	        console.log(error);
	      });

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

		});
	
	}

}

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

答案1

得分: 2

正如Ticherhaz在他的评论中解释的那样,您需要使用then()方法来检测由异步createUserWithEmailAndPassword()方法返回的Promise何时被满足。

以下代码应该可以解决问题:

firebase.auth().createUserWithEmailAndPassword(email.toString(), password.toString())
.then(userCredential => {
     var email = userCredential.user.email;
     alert("Successfully 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);
      });
}

请注意,正如文档中所解释的那样,“成功创建用户帐户后,此用户也将登录到您的应用程序”,因此在这种情况下不需要使用onAuthStateChanged()方法


在您上面的一条评论中,您说“只有在警告消息'testing'之前,此脚本正在执行”。实际上,如果新用户创建没有错误,那么传递给catch()的拒绝处理程序回调不会被调用,因此您无法获得createUserWithEmailAndPassword()正确执行的反馈。为此,您需要使用then()方法。

英文:

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 =&gt; {
     var email = userCredential.user.email;
     alert(&quot;Sucessfully Created&quot;);
})
.catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;
        if (errorCode == &#39;auth/weak-password&#39;) {
          alert(&#39;The password is too weak.&#39;);
        } 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.

huangapple
  • 本文由 发表于 2020年1月6日 17:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定