Saad Bashir
Saad Bashir

Reputation: 4519

Flutter + Firebase - Phone Authentication OTP Issue

I am trying to authenticate Phone number by sending OTP and asking for the user to enter that OTP once received. This functionality based on the following code works fine on iOS emulator but not on Android.

verifyPhoneNumber() async {
    await auth.verifyPhoneNumber(
      phoneNumber: '+92${widget.phoneNumber}',
      verificationCompleted: (PhoneAuthCredential credential) async {
        await auth.currentUser.updatePhoneNumber(credential);
      },
      verificationFailed: (FirebaseAuthException e) {
        print(e.message);
      },
      codeSent: (String verificationId, int resendToken) {
        setState(() {
          _otpcode = verificationId;
        });
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        setState(() {
          _otpcode = verificationId;
        });
      },
    );
  }

In order to make it work on android I have added SHA1 & SHA256 in the Firebase Console. I have also enabled Android DeviceCheck API.

On android when I press next after entering phone number in text field, it seems as if it tries to open a browser for recaptcha but nothing appears and just moves to the next screen asking for OTP. So I assume that it is doing invisible recaptcha.

The debug console shows following error:

( 9044): SafetyNet Attestation fails basic integrity.
I/zzjy    ( 9044): Provider GmsCore_OpenSSL not available
W/System  ( 9044): Ignoring header X-Firebase-Locale because its value was null.
E/FirebaseAuth( 9044): [GetAuthDomainTask] Error getting project config. Failed with {
E/FirebaseAuth( 9044):   "error": {
E/FirebaseAuth( 9044):     "code": 400,
E/FirebaseAuth( 9044):     "message": "INVALID_CERT_HASH",
E/FirebaseAuth( 9044):     "errors": [
E/FirebaseAuth( 9044):       {
E/FirebaseAuth( 9044):         "message": "INVALID_CERT_HASH",
E/FirebaseAuth( 9044):         "domain": "global",
E/FirebaseAuth( 9044):         "reason": "invalid"
E/FirebaseAuth( 9044):       }
E/FirebaseAuth( 9044):     ]
E/FirebaseAuth( 9044):   }
E/FirebaseAuth( 9044): }
E/FirebaseAuth( 9044):  400
E/zzf     ( 9044): Failed to get reCAPTCHA token - calling backend without app verification
W/System  ( 9044): Ignoring header X-Firebase-Locale because its value was null.
E/FirebaseAuth( 9044): [SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17093 null
I/flutter ( 9044): This request is missing a valid app identifier, meaning that neither SafetyNet checks nor reCAPTCHA checks succeeded. Please try again, or check the logcat for more details.

As mentioned above, the same code is working perfectly on iOS but for some odd reason not on Android. Please let me know what I might be missing. Thank you.

Upvotes: 2

Views: 2787

Answers (2)

zakiblacki
zakiblacki

Reputation: 233

You need to include the dependency androidx.browser:browser to make it work on emulator

The SHA256 key and android device verification API is for a real device to pass Safetynet check.

Upvotes: 0

GloatingCoder
GloatingCoder

Reputation: 1223

firebase otp doesn't work on emulator, try on a real device by building the apk or give a static otp code to your defined number from firebase console and then try on emulator.

Upvotes: 1

Related Questions