flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

firebase_auth: ^5.1.1 Cannot create firebase user from facebook auth

Open alexgrant999 opened this issue 1 year ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

Which plugins are affected?

Auth

Which platforms are affected?

iOS

Description

When signing in with Facebook, the functions returns with the error: {"code":190,"message":"Bad signature"}

My AppID and Token are all correct. Plus the clientID and Secret in Firebase.

  Future<void> _signInWithFacebook() async {
    try {
      // Trigger the sign-in flow
      final LoginResult loginResult = await FacebookAuth.instance.login();

      // Check if login was successful
      if (loginResult.status == LoginStatus.success) {
        final AccessToken accessToken = loginResult.accessToken!;


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

        // Sign in with the credential
        final UserCredential userCredential =
            await _auth.signInWithCredential(facebookAuthCredential);

        // Navigate to the home screen if successful
        if (userCredential.user != null) {
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(
              builder: (context) => HomeScreen(user: userCredential.user),
            ),
          );
        }
      } else {
        // Handle unsuccessful login
        print('Facebook login failed: ${loginResult.message}');
        throw FirebaseAuthException(
          code: 'firebase_auth/facebook-login-failed',
          message: 'Facebook login failed: ${loginResult.message}',
        );
      }
    } catch (e) {
      print('Error during Facebook login: $e');
      throw FirebaseAuthException(
        code: 'firebase_auth/facebook-login-error',
        message: 'Error during Facebook login: $e',
      );
    }
  }

Reproducing the issue

Click on a button to activate the login with facebook function.

Firebase Core version

^3.1.1

Flutter Version

3.22.2

Relevant Log Output

flutter: Error during Facebook login: [firebase_auth/invalid-credential] {"code":190,"message":"Bad signature"}
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_auth/firebase_auth/facebook-login-error] Error during Facebook login: [firebase_auth/invalid-credential] {"code":190,"message":"Bad signature"}
#0      _SignInScreenState._signInWithFacebook (package:Orpheus/pages/auth/sign_in_screen.dart:78:7)
<asynchronous suspension>
#1      _SignInScreenState.build.<anonymous closure> (package:Orpheus/pages/auth/sign_in_screen.dart:132:17)
<asynchronous suspension>
Restarted application in 661ms.

Flutter dependencies

Expand Flutter dependencies snippet

dependencies:
  flutter:
    sdk: flutter
  dio: ^4.0.0
  google_fonts: ^6.2.1
  image_picker: ^0.8.5+3
  numberpicker: ^2.1.2
  path_provider: ^2.0.11
  permission_handler: ^10.2.0
  persistent_bottom_nav_bar: ^5.0.2
  provider: ^6.0.3
  shared_preferences: ^2.0.15
  youtube_player_flutter: ^9.0.1
  webview_flutter: ^4.7.0
  volume_controller: ^2.0.7
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.6
  supabase_flutter: ^2.5.3
  url_launcher: ^6.2.6
  dart_rss: ^3.0.3
  audioplayers: ^6.0.0
  flutter_launcher_icons: ^0.13.1
  firebase_core: ^3.1.1
  firebase_auth: ^5.1.1
  firebase_messaging: ^15.0.2
  google_sign_in: ^6.2.1
  flutter_facebook_auth: ^7.0.1

Additional context and comments

No response

alexgrant999 avatar Jun 30 '24 09:06 alexgrant999

herev also https://stackoverflow.com/questions/78673055/firebase-auth-facebook-token-error-code-190

alexgrant999 avatar Jun 30 '24 09:06 alexgrant999

Same issue here: {"code":190,"message":"Bad signature"} when trying to log in using FacebookAuth with limited login.

KubaStachecki avatar Jun 30 '24 13:06 KubaStachecki

@alexgrant999 Thank you for reporting this issue. For Facebook limited login, you have to use the nonce field when invoking the login method. Please refer to this comment for more information: link.

TarekkMA avatar Jul 01 '24 13:07 TarekkMA

This seems to have fixed it, thank you. 🙏🏻

  String sha256ofString(String input) {
    final bytes = utf8.encode(input);
    final digest = sha256.convert(bytes);
    return digest.toString();
  }

  String generateNonce([int length = 32]) {
    // Define the character set to be used in the nonce
    final charset =
        '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-.';

    // Create a secure random number generator
    final random = Random.secure();

    // Generate a string of the specified length using random characters from the charset
    return String.fromCharCodes(List.generate(
        length, (index) => charset.codeUnitAt(random.nextInt(charset.length))));
  }

  Future<void> _signInWithFacebook() async {
    // Trigger the sign-in flow
    final rawNonce = generateNonce();
    final nonce = sha256ofString(rawNonce);
    final result = await FacebookAuth.instance.login(
      loginTracking: LoginTracking.limited,
      nonce: nonce,
    );
    if (result.status == LoginStatus.success) {
      print('${await FacebookAuth.instance.getUserData()}');
      final token = result.accessToken as LimitedToken;
      // Create a credential from the access token
      OAuthCredential credential = OAuthCredential(
        providerId: 'facebook.com',
        signInMethod: 'oauth',
        idToken: token.tokenString,
        rawNonce: rawNonce,
      );
      await FirebaseAuth.instance.signInWithCredential(credential);
    }
  }


alexgrant999 avatar Jul 05 '24 12:07 alexgrant999