尝试使用firebase身份验证设置确认密码

时间:2017-07-21 21:01:42

标签: android firebase firebase-authentication

我正在尝试编写我的第一个应用程序,并且我使用firebase auth email-password作为我的登录方法。在我的注册活动中,我想要电子邮件,密码和确认密码。在创建帐户之前,我无法让应用检查密码是否> = 6个字符以及密码和确认密码是否相等。 该应用程序似乎检查密码> = 6但如果这是真的它创建帐户而不检查密码=确认密码。 我还想显示一条错误消息,说明用户名已被使用。

这是我的代码

    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();

1 个答案:

答案 0 :(得分:2)

您想在调用auth.createUserWithEmailAndPassword之前检查您的条件。

你的if语句中也有一个遗失{所以它没有按你的意愿行事。应该看起来像这样:

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();