尝试使用Firebase身份验证设置确认密码 [英] Trying to set confirm password with firebase auth
问题描述
我正在尝试编写我的第一个应用程序,并且我使用firebase auth电子邮件密码作为我的登录方法.在我的注册活动中,我想要输入电子邮件,密码和确认密码.在创建帐户之前,我无法让该应用检查密码> = 6个字符以及密码和确认密码是否相等.该应用程序似乎会检查密码> = 6,但是如果是的话,它将创建该帐户而不检查密码=确认密码.我还想显示一条错误消息,说明用户名已被使用.
I'm trying to program my first app and I'm using firebase auth email-password as my login method. On my sign up activity I want to have email, password and confirm password. I can't make the app check if password >= 6 characters and if password and confirm password are equals before the account is created. The app seems to check if password >= 6 but if thats true it creates the account without checking if password = confirm password. I also would like to display an error message saying that the username is already being used.
这是我的代码
private void signUpUser(String email, final String password) {
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful())
{
if (password.length() < 6)
snackbar = Snackbar.make(activity_criar_conta, "Your password must have at least 6 characters.", Snackbar.LENGTH_SHORT);
snackbar.show();
String pass2 = etConfirmarSenha.getText().toString();
if (!password.equals(pass2))
snackbar = Snackbar.make(activity_criar_conta, "Both password fields must be identic", Snackbar.LENGTH_SHORT);
snackbar.show();
}
else{
String emailuser = etCriarEmail.getText().toString();
snackbar = Snackbar.make(activity_criar_conta, "Your account was created with sucess: "+emailuser,Snackbar.LENGTH_SHORT);
snackbar.show();
推荐答案
您要在调用auth.createUserWithEmailAndPassword之前检查条件.
You want to check your conditions before calling auth.createUserWithEmailAndPassword.
您的if语句上还缺少{,因此它没有达到您的期望.应该看起来像这样:
You've also got a missing { on your if statement, so its not doing what you expect. Should look something like this:
private void signUpUser(String email, final String password) {
if (password.length() < 6) {
snackbar = Snackbar.make(activity_criar_conta, "Your password must have at least 6 characters.", Snackbar.LENGTH_SHORT);
snackbar.show();
return;
}
String pass2 = etConfirmarSenha.getText().toString();
if (!password.equals(pass2)) {
snackbar = Snackbar.make(activity_criar_conta, "Both password fields must be identical", Snackbar.LENGTH_SHORT);
snackbar.show();
return;
}
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String emailuser = etCriarEmail.getText().toString();
snackbar = Snackbar.make(activity_criar_conta, "Your account was created with sucess: "+emailuser,Snackbar.LENGTH_SHORT);
snackbar.show();
这篇关于尝试使用Firebase身份验证设置确认密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!