是否可以使用Firebase身份验证在Android中同时使用电子邮件和电话验证

时间:2018-08-01 13:08:26

标签: android firebase firebase-authentication

private void verifyphone(final String phone)
{

    final String ph=phone;
    pDialog=new Dialog(this);
    pDialog.setContentView(R.layout.phoneverification);
    pDialog.setCancelable(true);
    pcode=pDialog.findViewById(R.id.pcodebtn);
    vcode=pDialog.findViewById(R.id.vcode);
    pstatus=pDialog.findViewById(R.id.pstatus);
    pDialog.show();

    PhoneAuthProvider.getInstance().verifyPhoneNumber(phone, 30, TimeUnit.SECONDS, this, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                pc=phoneAuthCredential;
                continueemail();
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // ...
                Toast.makeText(HomePage.this, "Invalid Phone number.Reenter correct phone number.", Toast.LENGTH_SHORT).show();
                pDialog.dismiss();
                wrongnumber=true;
                Toast.makeText(HomePage.this, "Registration Failed", Toast.LENGTH_SHORT).show();

            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // ...
                Toast.makeText(HomePage.this, "SMS Quota exceeded.This is an internal error.", Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(HomePage.this, "Registration Failed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            Toast.makeText(HomePage.this, "Verification Code sent to the phone number", Toast.LENGTH_SHORT).show();
            pstatus.setText("Verification Code has been sent to "+ph+".Please check phone and enter code to continue.");
            pvercode=s;
            mtoken=forceResendingToken;
        }
    });
    pcode.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TextUtils.isEmpty(vcode.getText().toString())) {
                vcode.setError("Verification Code cannot empty");
            } else if (vcode.getText().toString().length() != 6) {
                vcode.setError("Verification code format is wrong");
            }
            else
                {
                    if(PhoneAuthProvider.getCredential(pvercode,vcode.getText().toString())!=null)
                    {
                        pverified=true;
                        continueemail();
                    }
                    else
                    {
                        pverified=false;
                        pstatus.setText("Verification was unsuccessful.You entered wrong code!");
                        vcode.setError("Wrong Verification code was entered.");
                        Toast.makeText(HomePage.this, "Registration Failed,Pverified="+pverified, Toast.LENGTH_SHORT).show();
                    }

                }
        }
    });

    if(wrongnumber)
    {
        pverified=false;
    }
    if(pverified) {
        pDialog.dismiss();
    }

}

public void continueemail()
{
    pDialog.dismiss();
    mauth=FirebaseAuth.getInstance();
    mauth.createUserWithEmailAndPassword(rmail.getText().toString(), rpass.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                user = mauth.getCurrentUser();
                user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(HomePage.this, "We have sent you an email verification.", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                Toast.makeText(HomePage.this, "Registration sucessful", Toast.LENGTH_SHORT).show();
                myDialog.dismiss();

            }
            else
            {
                rmail.setError("Enter a correct mailID we will be sending a verification mail.");
                Toast.makeText(HomePage.this, "Wrong EmailID"+rmail.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}

}

我能够完成电话验证。但是它总是说电子邮件ID无效(即task.issuccessful()方法总是返回false)。我无法弄清楚为什么会这样。请帮帮我。是否可以同时验证电话和电子邮件验证。在验证电话号码后尝试验证电子邮件之前,我需要注销吗?

0 个答案:

没有答案
相关问题