手动验证Firebase OTP

时间:2019-12-07 17:18:23

标签: android firebase one-time-password

我正在寻找一种方法来查找从Firebase电话身份验证发送给我的代码,因为我想手动验证该代码。现在的问题是Firebase正在自动检测短信并调用了onVerificationCompleted()但我有一个按钮,然后我想手动输入otp代码并进行验证。下面是我的代码。任何帮助将不胜感激。谢谢

PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phonenumber,
                120,
                TimeUnit.SECONDS,
                this, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                Intent intent = new Intent(PhoneVerification.this, PictureActivity.class);
                Log.e("iamhere","Credential  IS"+phoneAuthCredential);
                intent.putExtra("email",email);
                intent.putExtra("phoneNumber",phonenumber);
                intent.putExtra("password",password);
                startActivity(intent);
                Toast.makeText(PhoneVerification.this, "Please fill the registration form", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onVerificationFailed(@NonNull FirebaseException e) {
                Toast.makeText(PhoneVerification.this, "Failed: "+e.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(s, forceResendingToken);
                Toast.makeText(PhoneVerification.this, "Check your phone for verification code", Toast.LENGTH_LONG).show();
                String mVerificationId = s;
            }
        });

1 个答案:

答案 0 :(得分:0)

如果未自动检测到代码,则用户必须通过EditText手动输入代码

    loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String txt = otpEditText.getText().toString().trim();
                if (txt.isEmpty() || txt.length() < 6) {
                    otp.setError("Enter valid code");

                    return;
                }

                //verifying the code entered manually
                if (isOnline()) {
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, txt);

                    signInWithPhoneAuthCredential(credential);
                } else {
                    showSnack(relativeLayout, "Unable to connect! Check internet connection");
                }
            }
        });



private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(Verification.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        //verification successfull do next task
                    } else {

                        //verification unsuccessful.. display an error message


                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            Toast.makeText(Verification.this, "Incorrect OTP entered", Toast.LENGTH_LONG).show();

                        } else {
                            Toast.makeText(Verification.this, "Unable to verify please retry later", Toast.LENGTH_LONG).show();


                        }


                    }
                }
            });
}

这是自动验证:

 private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    @Override
    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

        //Getting the code sent by SMS
        String code = phoneAuthCredential.getSmsCode();

        //sometime the code is not detected automatically
        //in this case the code will be null
        //so user has to manually enter the code
        if (code != null) {
            otpEditText.setText(code);
            //verifying the code
            verifyVerificationCode(code);
        }
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        FancyToast.makeText(Verification.this, e.getMessage(), FancyToast.LENGTH_LONG, FancyToast.CONFUSING, false).show();
    }

    @Override
    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        super.onCodeSent(s, forceResendingToken);

        //storing the verification id that is sent to the user
        mVerificationId = s;
    }
};

查看Firebase documentation以获得更多信息。