Firebase Phone Auth not working in APK Release mode

Viewed 3331

I read following two questions but none of these are solution to my problem:

  1. Firebase Phone Auth not working in release build

  2. Firebase Authentication not working in Signed APK

Solution mentioned in both of these questions says that I need to copy SHA-1 from Play console and place it in Firebase console SHA-1 for my app. Problem is My app is not on play store, I just signed it and build the apks as we normally do for apk.relase. And I also updated firebase_auth and firebase_core dependencies to latest version.

Below is my code for phone auth:


  verificationCompleted() {
    return (AuthCredential credential) async {
      await _auth.signInWithCredential(credential).then((AuthResult value) {
        if (value.user != null) {
          Navigator.pop(context);
          Navigator.push(
              context,
              new MaterialPageRoute(
                  builder: (context) => AddContacts(
                        user: value.user,
                      )));
        }
      });
    };
  }

  verificationFailed() {
    return (AuthException exception) {
      Toast.show('Verification Failed: ${exception.code}', context,
          backgroundColor: Colors.red,
          duration: 3,
          backgroundRadius: 5,
          gravity: Toast.BOTTOM);
    };
  }

  codeSent() {
    return (String verificationID, [int forceResendingToken]) {
      showDialog(
          context: context,
          barrierDismissible: false,
          builder: (context) {
            return AlertDialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12)),
              title: Text('Enter 6-Digit Code'),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  TextField(
                    style: TextStyle(
                        fontSize: MediaQuery.of(context).size.height * 0.025),
                    controller: _controllerCode,
                    keyboardType: TextInputType.number,
                    maxLength: 6,
                    cursorColor: Colors.black,
                    decoration: InputDecoration(
                      errorText: validateCode(_controllerCode.text),
                      hintText: 'Enter Code',
                      enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0)),
                      focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0)),
                      errorBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0),
                          borderSide: BorderSide(color: Colors.red)),
                      focusedErrorBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12.0),
                          borderSide: BorderSide(color: Colors.red)),
                    ),
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.01,
                  ),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(
                        Icons.info,
                        color: Colors.red,
                        size: MediaQuery.of(context).size.height * 0.03,
                      ),
                      SizedBox(
                        width: MediaQuery.of(context).size.width * 0.02,
                      ),
                      Text(
                        'Wait for Automatic Detection!',
                        style: TextStyle(
                            fontFamily: 'Sogoe',
                            color: Colors.red,
                            fontSize:
                                MediaQuery.of(context).size.height * 0.017),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.01,
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height * 0.06,
                    width: MediaQuery.of(context).size.width,
                    child: FlatButton(
                      padding: EdgeInsets.symmetric(
                          horizontal: MediaQuery.of(context).size.width * .05),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5)),
                      child: Text(
                        "Confirm",
                      ),
                      textColor: Colors.white,
                      color: Colors.lightBlue,
                      onPressed: () async {
                        setState(() {
                          _controllerCode.text.isEmpty
                              ? codeValidate = true
                              : codeValidate = false;
                        });
                        _auth.currentUser().then((user) {
                          if (user != null) {
                            Navigator.pop(context);
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => AddContacts(
                                          user: user,
                                        )));
                          } else {
                            _verifyCode();
                          }
                        });
                        _controllerCode.clear();
                      },
                    ),
                  )
                ],
              ),
            );
          });
      _verificationId = verificationID;
    };
  }

  codeAutoRetrievalTimeout() {
    return (String verificationID) {
      _verificationId = verificationID;
    };
  }

  Future<void> _verifyPhoneNumber(String phone) async {
    _auth.verifyPhoneNumber(
      phoneNumber: phone,
      timeout: const Duration(seconds: 60),
      verificationCompleted: verificationCompleted(),
      verificationFailed: verificationFailed(),
      codeSent: codeSent(),
      codeAutoRetrievalTimeout: codeAutoRetrievalTimeout(),
    );
  }

  Future<void> _verifyCode() async {
    final AuthCredential credential = PhoneAuthProvider.getCredential(
        verificationId: _verificationId, smsCode: _controllerCode.text);
    final FirebaseUser user =
        (await _auth.signInWithCredential(credential)).user;
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    if (user != null) {
      Navigator.pop(context);
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => AddContacts(
                    user: user,
                  )));
    }
  }

2 Answers

Found the solution

Yes I added SHA-1 Key but I came to know that SHA-1 for debug and release mode are separate and this answer is solution

ADD YOUR PLAYSTORE SIGNING KEY CERTIFICATES TO FIREBASE SHA1 AND SHA256 CERTIFICATES.
you'll find the key certificates in

googele play console > under release - setup > app integrity > App signing key certificate enter image description here

Related