如何使用Firebase Android验证收到的用于手机号码验证的OTP

时间:2018-12-25 09:47:48

标签: android firebase firebase-authentication

我想在我的应用程序中添加手机号码验证模块。我正在为此使用Firebase,并且OTP已成功发送到特定的手机号码,但是我想验证输入的OTP是否等于发送到相应手机号码的OTP。

不想使用以下代码,

  mAuth.signInWithCredential(credential)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(FirebasePhoneActivity.this, "Verification Success", Toast.LENGTH_SHORT).show();
                } else {
                    if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                        Toast.makeText(FirebasePhoneActivity.this, "Verification Failed, Invalid credentials", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
break; 

由于使用上述代码,因此用户可以使用手机号码登录。我只想对OTP进行交叉验证,并且不想使用移动电话号码登录。

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

        resendToken = forceResendingToken;
    }

使用上面的代码不会给出发送的代码,而是给出一个随机的字符串值。

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

        codeReceivedByUser = phoneAuthCredential.getSmsCode();

    }

以上代码仅在收到其手机的OTP时才被调用,而不是在其他人的手机中被调用(当我们提供其他人的手机号码时)

在不使用signInWithCredential方法发送给他人的手机时,是否有任何方法可以验证OTP?请帮助。预先感谢。

1 个答案:

答案 0 :(得分:0)

尝试下面的代码(一个接一个地调用方法)

private void setListener(){

        callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                Toast.makeText(context, "Verification Completed" + phoneAuthCredential, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                try {
                    ActivityHelper.dismissProgressDialog();

                } catch (Throwable throwable) {
                    CustomLogHandler.printErrorlog(throwable);
                }
                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    Toast.makeText(context ,"Verification Failed" + e, Toast.LENGTH_SHORT).show();
                } else if (e instanceof FirebaseTooManyRequestsException) {
                    Toast.makeText(context, "Verification Failed" + e, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Verification Failed" + e, Toast.LENGTH_SHORT).show();
                }
                isResendClicked = false;
            }

            @Override
            public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(s, forceResendingToken);
                try {
                    ActivityHelper.dismissProgressDialog();
                } catch (Throwable throwable) {
                    CustomLogHandler.printErrorlog(throwable);
                }
                Toast.makeText(getActivity(), "Code sent to your registered device", Toast.LENGTH_SHORT).show();
                isResendClicked = false;
                verificationId = s;
                resendtoken = forceResendingToken;
            }
        };

    }




  private void callGetOTPApi() {
        try {
            ActivityHelper.showProgressDialog(getActivity(), "Loading...", false);
            PhoneAuthProvider.getInstance().verifyPhoneNumber(number, 60, TimeUnit.SECONDS, getActivity(), callbacks);

        } catch (Throwable throwable) {
            CustomLogHandler.printErrorlog(throwable);
        }


    }