Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, temporary proof, or enrollment ID

I'm working on firebase phone authentication, after receiving code on my phone number the code jumps to verifysignincode() method, it fails to create phoneAuthCredentials. The exception which program catches is "Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, temporary proof, or enrollment ID."

This is my send code method:

    public void send_code(){
    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            Log.d("code", "onCodeSent:" + s);
            verificationID=s;
        }
        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {

        }
        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
            Toast.makeText(PhoneAuthentication.this, e.getMessage(), Toast.LENGTH_SHORT);
        }
    };
    mPhoneAuthProvider.verifyPhoneNumber(
            user_contact , 60, TimeUnit.SECONDS, PhoneAuthentication.this, mCallbacks
    );}

This is my verify sign in method:

    public void verifySignInCode(String code){
    try {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationID, code);
        signInWithPhoneAuthCredential(credential);
    }catch (Exception e){
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

    }
}

this is my signin method with phone credentials:

    private  void signInWithPhoneAuthCredential(PhoneAuthCredential credential){
    mFirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(task.isSuccessful()){

                        startActivity(new Intent(PhoneAuthentication.this, UserHome.class));
                    }
                    else {
                        Log.d("error:", task.getException().getMessage());

                    }
                }
            });
}

ERROR:

Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, temporary proof, or enrollment ID.

like image 935
Ali Zamin Avatar asked Jan 04 '20 08:01

Ali Zamin


2 Answers

Problem: This has burned me quite a long and after researching I got to know that this happens due to either low memory on phone or low network connection. When you maintain log with verification id i.e. log.d(TAG, verificationId) it will show null in verificationId and as per the error it says cannot create PhoneAuthCredential without verification proof and verificationId is our verification proof.

Solution:

  1. Clear phone cache and try signing again. Go to app info and clear cache.
  2. Restore Instance State
private String verificationId;
private static final String KEY_VERIFICATION_ID = "key_verification_id";

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

    // Restore instance state
    // put this code after starting phone number verification
    if (verificationId == null && savedInstanceState != null) {
        onRestoreInstanceState(savedInstanceState);
    }
}//end of onCreate

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(KEY_VERIFICATION_ID,verificationId);
}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    verificationId = savedInstanceState.getString(KEY_VERIFICATION_ID);
}

What happens is the verificationId is stored locally and it is lost sometimes due to less memory. So to tackle this situation we have to restore Instance State which won't let the verificationId to be lost.

like image 168
Abhishek Avatar answered Oct 06 '22 02:10

Abhishek


I'm struggling with the same issue being reported in Firebase Crashlytics and Play Store pre-launch testing, but it only seems to happen when I launch the app on Google Play Store and it automatically tries to test the app on many different devices. So I'm thinking it's more an issue with that testing process. It doesn't seem to be happening to any real users. I could be wrong though, I just notice a spike in these errors only when I launch the app and it goes through that testing phase. The rest of the time, no such errors are reported.

like image 26
The Fluffy T Rex Avatar answered Oct 06 '22 00:10

The Fluffy T Rex