Reputation: 65
I'm using Firebase Phone Authentification to verify users accounts. When I try to authenticate with a wrong verification code, I get an IllegalArgumentException. Is there any way to fix that without using try
catch
blocks?
Here is the exception message :
java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, or temporaryProof.
at com.google.android.gms.common.internal.zzbq.checkArgument(Unknown Source)
at com.google.firebase.auth.PhoneAuthCredential.<init>(Unknown Source)
at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source)
at com.example.myApp.testFragment$3$4.onClick(testFragment.java:316)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20926)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
And here is the line where I get the error :
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCode, inputCode);
I'm intentionally using wrong verification code in inputCode
and hoping to get an error message instead of the exception.
Upvotes: 3
Views: 10072
Reputation: 31
I got the same error, just add the SHA certificates. Error will dissolved.
Upvotes: 1
Reputation: 11
I was getting same issue and resolved it. actually you need to check verificationId and code is not null
Thanks.
Upvotes: 1
Reputation: 288
It returns an exception so the aim should be to catch the exception it throws in the debug console. I do that by wrapping the portion of that code in a try catch and Toast it out for the user of the aim to see rather than the usual crash which isnt visible to the End Users.
Like this...
private void verifyCode(String code) {
try {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
signInWithCredential(credential);
}catch (Exception e){
Toast toast = Toast.makeText(this, "Verification Code is wrong", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
}
Upvotes: 7
Reputation: 599641
Calls to PhoneAuthProvider.getCredential(...)
return a PhoneAuthCredential object, which is defined as:
Wraps phone number and verification information for authentication purposes.
So getCredential()
cannot return an error message. Instead it throws an exception if there is a problem with the verification/credential information you provided.
Upvotes: 5