代码之家  ›  专栏  ›  技术社区  ›  Patricia

Firebase验证用户

  •  0
  • Patricia  · 技术社区  · 9 年前

    我想创建一个登录,用户只需要把他/她的邮件地址,并通过电子邮件验证它。到目前为止,使用firebase我有以下代码:

    • 当用户在将邮件放入字段后单击按钮登录时。我尝试创建一个新帐户(密码是随机的,因为我不需要它)。如果它不存在,我会发送一封验证邮件。如果该账户存在,我会与 检查验证者 用户是否通过单击邮件验证帐户。

      firebaseAuth.createUserWithEmailAndPassword(email, Math.random() + "").addOnCompleteListener(new OnCompleteListener<AuthResult>() {
      @Override
      public void onComplete(@NonNull Task<AuthResult> task) {
          if (task.isSuccessful()) {
              FirebaseUser user = firebaseAuth.getCurrentUser();
              user.sendEmailVerification().addOnCompleteListener((Activity) context, new OnCompleteListener() {
                          @Override
                          public void onComplete(@NonNull Task task) {
                              if (task.isSuccessful()) {
                                  Toast.makeText(context, "we sent a mail, please verify", Toast.LENGTH_LONG).show();
                              } else {
                                  Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
                              }
                          }
                      }); 
              checkIfVerifiedUser(email);
          } else {
              progressDialog.dismiss();
              try {
                  throw task.getException();
              } catch (FirebaseAuthUserCollisionException e) {
            // In case the account already exists
                  checkIfVerifiedUser(email);
              } catch (Exception e) {
                  Toast.makeText(context, R.string.error_auth, Toast.LENGTH_SHORT).show();
              }
          }
      }    });
      

    以下是我调用的方法:

    private void checkIfVerifiedUser(String email){
            final FirebaseUser user = firebaseAuth.getCurrentUser();
            Intent intent = new Intent(context, HomeActivity.class);
            if(user != null){
                user.reload();
                // If the user is created, we check if the account is verified
                if(user.isEmailVerified()) {
                    intent.putExtra(IntentEnum.ALREADYREGISTERED.getCode(), true);
                }else{
                    intent.putExtra(IntentEnum.ALREADYREGISTERED.getCode(), false);
                }
            }else{
                intent.putExtra(IntentEnum.ALREADYREGISTERED.getCode(), false);
            }
            ---rest of code---
        }
    

    问题是我要么得到firebaseAuth。getCurrentUser()==null或user。isEmailVerified()始终为false(即使我在收到的电子邮件中单击了链接)。

    有人能帮我吗? 谢谢

    1 回复  |  直到 9 年前
        1
  •  0
  •   Alex Mamo    9 年前

    要解决这个问题,你需要打电话 checkIfVerifiedUser() 方法内部 onComplete()