手动OTP验证在Firebase身份验证API中不起作用

时间:2019-03-14 01:16:46

标签: android firebase firebase-authentication

我正在使用Firebase身份验证API通过电话号码实现用户身份验证。我可以使用即时验证方式进行身份验证,但是当我尝试通过自己的UI输入SMS代码时,就会不断抛出

com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The sms code has expired. Please re-send the verification code to try again.

这是我的代码段,

    public void authenticateWithMSISDN(String phoneNumber) {

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            "+" + phoneNumber,        // Phone number to verify
            120,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            mContext,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks


}

回调

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

 mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                Log.d(TAG, "onVerificationCompleted");
                signInWithPhoneAuthCredential(phoneAuthCredential);


            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                Log.e(TAG, "login failed -- > " + e.getMessage());
                ToastUtil.showAuthenticationFailed(mContext);

            }

            @Override
            public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(s, forceResendingToken);
                Log.d(TAG, "onCodeSent  -- > " + s);

                verificationid = s;
                Log.d(TAG," onCodeSent  " + verificationid);
                authenticationCallback.onCodeReceived(verificationid);
            }
        };

验证方法:

public void verify(String verificationId, String code) {

    if (verificationId != null && code != null) {

        Log.d(TAG, "verification  code -- > " + verificationId + "Code :: " + code);

        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
        signInWithPhoneAuthCredential(credential);
    } else {
        ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.invalid_otp_enter));
    }
}

获得身份验证的主要方法:

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(mContext, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "login sucess");
//                            authenticationCallback.onAuthenticationSuccess();
                            getUserToken();

                        } else {
                            // Sign in failed, display a message and update the UI
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {

                                Log.d(TAG, "login failed");
                                ToastUtil.showInvalidPinToast(mContext);

                            }

                        }
                    }
                });
    }

每次尝试进行手动身份验证时,我都会遇到错误,但是它在即时验证中可以正常工作。

0 个答案:

没有答案