Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firebase_auth: ^5.1.1 Cannot create firebase user from facebook auth #13015

Closed
1 task done
alexgrant999 opened this issue Jun 30, 2024 · 4 comments
Closed
1 task done
Assignees
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. platform: ios Issues / PRs which are specifically for iOS. plugin: auth resolution: solution-provided A solution has been provided in the issue. type: bug Something isn't working

Comments

@alexgrant999
Copy link

Is there an existing issue for this?

  • 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 alexgrant999 added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Jun 30, 2024
@alexgrant999
Copy link
Author

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

@KubaStachecki
Copy link

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

@TarekkMA TarekkMA added plugin: auth platform: ios Issues / PRs which are specifically for iOS. labels Jul 1, 2024
@TarekkMA TarekkMA self-assigned this Jul 1, 2024
@TarekkMA
Copy link
Contributor

TarekkMA commented Jul 1, 2024

@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 TarekkMA added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Jul 1, 2024
@alexgrant999
Copy link
Author

alexgrant999 commented Jul 5, 2024

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);
    }
  }


@TarekkMA TarekkMA closed this as completed Jul 5, 2024
@TarekkMA TarekkMA added the resolution: solution-provided A solution has been provided in the issue. label Jul 5, 2024
@firebase firebase locked and limited conversation to collaborators Aug 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. platform: ios Issues / PRs which are specifically for iOS. plugin: auth resolution: solution-provided A solution has been provided in the issue. type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants