Firebase 的电话身份验证不适用于自己的设备

时间:2021-02-24 07:32:30

标签: android firebase authentication

我有一个使用 Firebase 电话身份验证的 android 应用程序,此功能正在运行,但现在电话身份验证不适用于在同一设备上具有该应用程序和 SIM 卡号码的物理设备并收到错误 - < strong>如果没有 verifyProof、sessionInfo、临时证明或注册 ID,则无法创建 PhoneAuthCredential。但是当尝试不同的号码时,它会发送 OTP,我可以验证并登录。以下是 Verification 类的代码。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_o_t_p_verification);
        getSupportActionBar().hide();
        StartFirebaseLogin();
        FindViews();
        prefs = PreferenceManager.getDefaultSharedPreferences(this);

        registerPhone.requestFocus();

        registerPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regPhoneNumber = driverPhoneNumber.getText().toString();
                if(regPhoneNumber.startsWith("+251"))
                {
                    GlobalVariables.regPhoneNumber = driverPhoneNumber.getText().toString();
                }
                else if(regPhoneNumber.startsWith("09"))
                {
                    try {
                        regPhoneNumber = driverPhoneNumber.getText().toString().substring(1, 10);
                    }
                    catch (Exception e){}
                    GlobalVariables.regPhoneNumber = country_code.getText().toString() + regPhoneNumber;
                }
                else if(regPhoneNumber.startsWith("9"))
                {
                    GlobalVariables.regPhoneNumber = country_code.getText().toString() + driverPhoneNumber.getText().toString();
                }
                else{
                    GlobalVariables.regPhoneNumber = country_code.getText().toString() + driverPhoneNumber.getText().toString();
                }

                //GlobalVariables.regPhoneNumber = "+251" + driverPhoneNumber.getText().toString();
                try {
                    PhoneAuthProvider.getInstance().verifyPhoneNumber(
                            GlobalVariables.regPhoneNumber,
                            60,
                            TimeUnit.SECONDS,
                            OTPVerification.this,
                            mCallback);
                } catch (Exception e) {
                    Toasty.error(getApplicationContext(), "Please try again later!!!", Toast.LENGTH_SHORT).show();
                }

                new CountDownTimer(60000, 1000) {

                    public void onTick(long millisUntilFinished) {
                        if(millisUntilFinished > 10000)
                        {
                            countDownTimer.setText("0:" + millisUntilFinished / 1000 + "\t");
                        }
                        else if(millisUntilFinished < 10000)
                        {
                            countDownTimer.setText("0:0" + millisUntilFinished / 1000 + "\t");
                        }
                    }

                    public void onFinish() {
                        resendOTPText = new SpannableString("RESEND OTP");
                        resendOTPText.setSpan(new UnderlineSpan(), 0, 10, 0);
                        countDownTimer.setText(getResources().getText(R.string.didn_t_get_otp));
                        resendOTP.setEnabled(true);
                        resendOTP.setText(resendOTPText);
                        resendOTP.setTextColor(getResources().getColor(R.color.colorPrimary));
                    }

                }.start();
            }

            });


        verifyOTP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GlobalVariables.otpNumber = OTPNumber.getText().toString();
                try {
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(GlobalVariables.verificationCode, GlobalVariables.otpNumber);
                    SigninWithPhone(credential);
                }catch (Exception e){
                    Log.i("exception",e.toString());
                }

            }
        });

        resendOTP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resendVerificationCode(GlobalVariables.regPhoneNumber, token);
            }
        });
    }

    private void FindViews() {
        driverPhoneNumber = findViewById(R.id.MobileNumberOTP);
        OTPNumber = findViewById(R.id.OTP);
        verifyOTP = findViewById(R.id.dotpButton);
        registerPhone = findViewById(R.id.registerPhone);
        resendOTP = findViewById(R.id.resend_otp);
        countDownTimer = findViewById(R.id.count_down_timer);
        regPhoneNumber = driverPhoneNumber.getText().toString();
        country_code = findViewById(R.id.country_code);
    }

    private void StartFirebaseLogin() {

        auth = FirebaseAuth.getInstance();

        mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                String code = phoneAuthCredential.getSmsCode();
                OTPNumber.setText(code);
                verifyOTP.performClick();
                Toasty.info(OTPVerification.this,"Verification Completed",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                Toasty.error(OTPVerification.this,"Verification Failed",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(s, forceResendingToken);
                OTPNumber.setVisibility(View.VISIBLE);
                verifyOTP.setVisibility(View.VISIBLE);
                GlobalVariables.verificationCode = s;
                Toasty.info(OTPVerification.this,"A 6-Digit Verification code has been sent to: " + GlobalVariables.regPhoneNumber +
                        " \n Check Your Messages",Toast.LENGTH_SHORT).show();
            }
        };
    }

    private void SigninWithPhone(PhoneAuthCredential credential) {

        auth.signInWithCredential(credential)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            GlobalVariables.firebaseUID = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
                            startActivity(new Intent(OTPVerification.this,SignupActivity.class));
                            finish();
                        } else {
                            Toasty.error(OTPVerification.this,"Incorrect OTP", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

我该怎么做才能让它在自己的设备上运行?有什么我遗漏的吗?

0 个答案:

没有答案