Search code examples
iosflutterfacebook-login

Flutter iOS Facebook Limited Login Bad Signature


Package version used:

flutter_facebook_auth: ^7.0.0
firebase_core: ^2.24.2
firebase_auth: ^4.15.2
FBSDKLoginKit (17.0.2) //pod

I have tested recently with our app and can't login with Facebook and upon debugging, I've seen this error

Unhandled Exception: [firebase_auth/invalid-credential] {"code":190,"message":"Bad signature"}

Login attempt code:

Future<dynamic> signInWithFacebook() async {
  // try {
    final LoginResult loginResult = await _facebookAuth.login();

    if (loginResult.accessToken == null) {
      _hasError = true;
      _loginErrorMsg = 'Login cancelled.';
      notifyListeners();
      return null;
    }

    // Create a credential from the access token
    final OAuthCredential facebookAuthCredential =
        FacebookAuthProvider.credential(loginResult.accessToken!.tokenString);

    UserCredential userCredential = await FirebaseAuth.instance
        .signInWithCredential(facebookAuthCredential);
}

The result of loginResult is LoginStatus.success.

Is someone experiencing the same issue? What are the steps have you done to resolve?


Solution

  • Thanks for the inputs, I'm able to solve my issue by doing the following.

    In info.plist, add this

    <key>NSUserTrackingUsageDescription</key>
    <string>This identifier will be used to deliver personalized ads to you.</string>
    

    Please take note that you have to update the privacy in App Store Connect.

    For my Login Button

    void facebookSignIn(BuildContext context) async {
      final authProvider = AuthServiceProvider();
    
      //since we only have the problem with iOS
      if (Platform.isIOS) {
        TrackingStatus status =
            await AppTrackingTransparency.trackingAuthorizationStatus;
    
        if (status == TrackingStatus.notDetermined) {
          status = await AppTrackingTransparency.requestTrackingAuthorization();
        } else {
          if (status != TrackingStatus.authorized) {
            await openAppSettings();
          }
        }
    
        if (status != TrackingStatus.authorized) {
          Util.showSnackBar(
            context,
            'Login cancelled, to continue please allow this app to track your activity.',
          );
          return;
        }
      }
    
      WidgetsFlutterBinding.ensureInitialized().addPostFrameCallback(
        (_) async {
    
          await authProvider.signInWithFacebook().then(
            (value) async {
              //handle the result here
            },
          );
        },
      );
    }
    

    and in my auth.dart

    Future<dynamic> signInWithFacebook() async {
      try {
        final LoginResult loginResult = await _facebookAuth.login(
          loginTracking: LoginTracking.enabled,
        );
    
        if (loginResult.accessToken == null) {
          _hasError = true;
          _loginErrorMsg = 'Login cancelled.';
          notifyListeners();
          return null;
        }
    
        // Create a credential from the access token
        final OAuthCredential facebookAuthCredential =
            FacebookAuthProvider.credential(loginResult.accessToken!.tokenString);
    
        UserCredential userCredential = await FirebaseAuth.instance
            .signInWithCredential(facebookAuthCredential);
    
        return userCredential;
      } on FirebaseAuthException catch (e) {
        //FirebaseAuthException error handling
        return null;
      } on Exception catch (e) {
        //error handling
        notifyListeners();
        return null;
      }
    }