电话身份验证始终在Firebase中产生负面响应

时间:2018-08-01 15:07:42

标签: java 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) {
            Toast.makeText(HomePage.this, "Verfied Phone", Toast.LENGTH_SHORT).show();
            signincredentials(phoneAuthCredential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // ...
                Toast.makeText(HomePage.this, "Invalid Phone Number", Toast.LENGTH_SHORT).show();
                pDialog.dismiss();

            } 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
                {
                    pauthcreddential=PhoneAuthProvider.getCredential(pvercode,vcode.getText().toString());
                    signincredentials(pauthcreddential);
                }
        }
    });

}
public void signincredentials(PhoneAuthCredential pcredential)
{
    mauth.signInWithCredential(pcredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                pverified=true;
                user = task.getResult().getUser();
                Toast.makeText(HomePage.this, "Phone number verification sucessful", Toast.LENGTH_SHORT).show();
                verifyemail();
            } else {
                if(task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                    pstatus.setText("Verification was unsuccessful.You entered wrong code!");
                    vcode.setError("Wrong Verification code was entered.");
                    Toast.makeText(HomePage.this, "Pverified="+vcode.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}
public void verifyemail()
{
    pDialog.dismiss();
    mauth.signOut();
    mauth=FirebaseAuth.getInstance();
    Toast.makeText(this, "Processing email verification", Toast.LENGTH_SHORT).show();
    mauth.createUserWithEmailAndPassword(rmail.getText().toString(), rpass.getText().toString()).addOnCompleteListener(this,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();
            }
        }
    });
}

即使我输入了正确的OTP(即使我为白名单号码输入了有效的OTP),我也总是得到错误的验证码作为输出。我已经尝试了几个小时来解决这个问题。请帮帮我。

在自动验证OTP期间,我没有遇到此问题。相反,验证邮件失败的代码的后半部分

该错误似乎与signincredentials()方法有关。当我在模拟器中运行时,出现以下错误: LOGCAT详细信息

08-01 20:21:17.634 16978-16978 / akshay.shoppingapplication E / AndroidRuntime:FATAL EXCEPTION:main     流程:akshay.shoppingapplication,PID:16978     java.lang.IllegalArgumentException:如果没有verifyProof,sessionInfo或临时证明,则无法创建PhoneAuthCredential。         在com.google.android.gms.common.internal.zzbq.checkArgument处(未知来源:8)         com.google.firebase.auth.PhoneAuthCredential。(未知来源:40)         在com.google.firebase.auth.PhoneAuthProvider.getCredential(未知来源:9)         在akshay.shoppingapplication.HomePage $ 4.onClick(HomePage.java:188)         在android.view.View.performClick(View.java:6597)         在android.view.View.performClickInternal(View.java:6574)         在android.view.View.access $ 3100(View.java:778)         在android.view.View $ PerformClick.run(View.java:25883)         在android.os.Handler.handleCallback(Handler.java:873)         在android.os.Handler.dispatchMessage(Handler.java:99)         在android.os.Looper.loop(Looper.java:193)         在android.app.ActivityThread.main(ActivityThread.java:6642)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 08-01 20:21:17.686 16978-16978 / akshay.shoppingapplication I / Process:正在发送信号。 PID:16978 SIG:9

0 个答案:

没有答案