1

I solved this issue (App crashes upon phone authentication after changing package name - Flutter) of app crash by adding implementation "androidx.browser:browser:1.2.0" into app/build.gradle dependencies.

But NOW whole phone authentication procedure got changed. Now app open a browser to do Not a robot test. But I don't want app to open a browser just to verify it's not a robot it make entire process slow and ugly. Below is the video example. How to get rid of this issue? It shows app firebase address in the browser link too.

Video example of issue is below

https://drive.google.com/file/d/1G7noQWyyAHvyTo_Te0v6d2O3IvaiClAw/view?usp=sharing

Below is the code snippet of verifyPhone function.

  Future<dynamic> verifyPhone(phoneNo, BuildContext context) async {
    var completer = Completer<dynamic>();
    dynamic newUserResult;

    Future<String> getOTPresult() async {
      print("Dialog shown");
      await showModalBottomSheet(
        context: context,
        backgroundColor: Colors.transparent,
        builder: (context) => Container(
          height: 270,
          child: OTPBottomSheet(controller: _otpController),
        ),
      );
      return _otpController.text;
    }
 
    //  >>>>>>>>>>>>> On Complete

    final PhoneVerificationCompleted verificationComplete =
        (AuthCredential authCred) async {
      print(" I N S I D E   C O M P L E T E ");
      newUserResult = await signInWithPhoneNumber(authCred);
      completer.complete(newUserResult);
    };
 
    //  >>>>>>>>>>>>> On Timeout

    final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verID) {
      print("\n2. Auto retrieval time out");
      completer.complete(newUserResult);
    };

    // >>>>>>>>>>>>>  On manual code verification

    final PhoneCodeSent smsCodeSent =
        (String verID, [int forceCodeResend]) async {
      print(" I N S I D E   C O D E   S E N T");
      var OTPDialogResult = await getOTPresult();
       if (OTPDialogResult != null) {
        AuthCredential authCred = PhoneAuthProvider.credential(
            verificationId: verID, smsCode: OTPDialogResult);
         newUserResult = AuthService().signInWithPhoneNumber(authCred);
        if (!completer.isCompleted) {
          completer.complete(newUserResult);
        }
      }
    };

      //  >>>>>>>>>>>>> On Ver failed
    
      final PhoneVerificationFailed verificationFailed =
        (Exception authException) {
       completer.complete(newUserResult);
    };

    await FirebaseAuth.instance
        .verifyPhoneNumber(
          phoneNumber: phoneNo,
          timeout: Duration(seconds: 50),
          verificationCompleted: verificationComplete,
          verificationFailed: verificationFailed,
          codeSent: smsCodeSent,
          codeAutoRetrievalTimeout: autoRetrieve,
        ).catchError((error) {
      print(error.toString());
    });

    print("New user result at the end before await: " + newUserResult.toString());
    newUserResult = await completer.future;
    print("New user result at the end after await: " + newUserResult.toString());
    return newUserResult;
  }

signInWithPhoneNumber function


  Future signInWithPhoneNumber(AuthCredential authCreds) async {
    try {
      UserCredential result = await FirebaseAuth.instance.signInWithCredential(authCreds);
      User customUser = result.user;
    return _userFormFirebaseUser(customUser).getuid;
  }

  CustData _userFormFirebaseUser(User user) {
    print("----> Inside _userFormFirebaseUser and user ID: " + user.uid);
    return user != null
        ? CustData(
            custId: user.uid,
          )
        : null;
  }


// --- CustData  model class 

class CustData {
  String custId;
  String custName;
  String custPhNo;
  String custContactNO;
  DateTime custDateOfBirth;
  Map<String, dynamic> address;
  String cartID;
  CustData({
    this.custId,
    this.custName,
    this.custPhNo,
    this.custDateOfBirth,
    this.address,
    this.cartID,
    this.custContactNO,
  });

  CustData.initial() : custId = '';
  String get getuid => this.custId;
}
Faizan Kamal
  • 1,732
  • 3
  • 27
  • 56

2 Answers2

0

Already making the whole procedure slow...

Try this Auth Version and it will work well for you :)

implementation com.google.firebase:firebase-auth:19.3.1

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
Mark Nashat
  • 668
  • 8
  • 9
0

The reason behind this is explained in the official documentation https://firebase.google.com/docs/auth/android/phone-auth#enable-app-verification

reCAPTCHA verification: In the event that SafetyNet cannot be used, such as when the user does not have Google Play Services support, or when testing your app on an emulator, Firebase Authentication uses a reCAPTCHA verification to complete the phone sign-in flow. The reCAPTCHA challenge can often be completed without the user having to solve anything. Please note that this flow requires that a SHA-1 is associated with your application.

To solve it you have to :

  • In the Google Cloud Console, enable the Android DeviceCheck API for your project. The default Firebase API Key will be used, and needs to be allowed to access the DeviceCheck API.
  • If you haven't yet specified your app's SHA-256 fingerprint, do so from the Settings Page of the Firebase console. Refer to Authenticating Your Client for details on how to get your app's SHA-256 fingerprint.
Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44