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.
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:
- Clear phone cache and try signing again. Go to app info and clear cache.
- 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With