【问题标题】:Cannot Verify phone number using firebase authentication无法使用 Firebase 身份验证验证电话号码
【发布时间】:2023-03-05 01:32:02
【问题描述】:

我正在尝试验证电话号码,但始终失败。我有登录活动,它获取有关用户的所有信息,包括手机号码字段。单击登录按钮后,我希望 OTP 活动将打开,用户可以在此处输入 otp 并单击验证按钮以验证号码。但它总是进入 onVerificationFailed 块并显示 toast Verification Fail 和 Invalid Number。

登录活动

public class Login extends AppCompatActivity{

public FirebaseAuth mAuth;
// [END declare_auth]

public String mVerificationId;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
public PhoneAuthProvider.ForceResendingToken mResendToken;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final String name = txtName.getText().toString();
            final String mobileno = txtMobile.getText().toString();
            final String email = txtEmail.getText().toString();
            final String dob = txtDOB.getText().toString();
            final String address = txtAdd.getText().toString();
            final String city = txtCity.getText().toString();
            final String state = txtState.getText().toString();
            final String country = txtCountry.getText().toString();



                mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                    @Override
                    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                        Toast.makeText(Login.this,"Verification Done"+ phoneAuthCredential,Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onVerificationFailed(FirebaseException e) {
                        Toast.makeText(Login.this,"Verification Fail",Toast.LENGTH_LONG).show();
                        if (e instanceof FirebaseAuthInvalidCredentialsException){
                            Toast.makeText(Login.this, "Invalid Number", Toast.LENGTH_SHORT).show();
                        }else if (e instanceof FirebaseTooManyRequestsException){
                            Toast.makeText(Login.this, "Too many Request", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        mVerificationId = s;
                        mResendToken = forceResendingToken;
                        Toast.makeText(Login.this, "Code Sent", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(Login.this, OTP.class));
                    }
                };
               PhoneAuthProvider.getInstance().verifyPhoneNumber(mobileno, 60, TimeUnit.SECONDS, Login.this, mCallbacks);
            }

        }
    });

}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's informatio
                        Toast.makeText(Login.this,"Verification done",Toast.LENGTH_LONG).show();
                        FirebaseUser user = task.getResult().getUser();
                        // ...
                    } else {
                        // Sign in failed, display a message and update the UI
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid

                            Toast.makeText(Login.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
}
}

OTP 活动

public class OTP extends AppCompatActivity {

public FirebaseAuth mAuth;
public String mVerificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_otp);

    Button btnVerify = (Button) findViewById(R.id.btnVerifyOTP);
    final EditText txtOtp = (EditText) findViewById(R.id.txtOtp);
    btnVerify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId  , txtOtp.getText().toString());
            signInWithPhoneAuthCredential(credential);
        }
    });

}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        //Log.d(TAG, "signInWithCredential:success");
                        Toast.makeText(OTP.this,"Verification done",Toast.LENGTH_LONG).show();
                        FirebaseUser user = task.getResult().getUser();
                        // ...
                    } else {
                        // Sign in failed, display a message and update the UI
                        //Log.w(TAG, "signInWithCredential:failure", task.getException());
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                            Toast.makeText(OTP.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
}
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!您发布的代码似乎比对您的问题合理的代码更多。请阅读 How to Ask 以及如何制作 minimal reproducible example,提供 MCVE 可以帮助用户回答您的问题以及未来的用户与您的问题相关。
  • 好的。已编辑 :) @IsiahMeadows

标签: android firebase firebase-authentication


【解决方案1】:

我遇到了同样的问题,我所做的是在输入电话号码时说如果号码是 79.....6,然后输入为 +9179...6 (IF INDIAN NUMBER)

根据国家/地区,添加国家代码作为前缀以获取短信代码

希望对你有帮助

【讨论】:

    【解决方案2】:

    Firebase 会在某些设备上自动验证 SMS 消息。在您的情况下,它似乎正在尝试验证 SMS 消息但无法这样做,这就是我猜无效电话号码错误的原因。但是您在哪个设备上进行测试?

    更重要的是,您的代码似乎没有涵盖自动短信验证场景... startActivity(new Intent(Login.this, OTP.class));

    您可以在此处查看 Firebase 电话号码身份验证的完整实现http://www.zoftino.com/firebase-phone-number-authentication-android

    【讨论】:

    • 我正在尝试使用 Galaxy J7。如果它是自动验证,那么至少进入 onVerificationCompleted。它总是失败,我尝试使用国家代码但仍然没有相同的结果。
    • 移除 startActivity(new Intent(Login.this, OTP.class));并尝试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多