firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.65k stars 3.96k forks source link

[FirebaseAuth]: Can't listen to Facebook login #13014

Closed MenaRaafat closed 2 months ago

MenaRaafat commented 3 months ago

Is there an existing issue for this?

Which plugins are affected?

Auth

Which platforms are affected?

Web

Description

I can listen to Google sign in + custom Email & password sign in. But, Facebook doesn't call the authStateChanges listener FirebaseAuth.instance.authStateChanges().listen((User? user) I can see that the proper User data is returned in the API responses though.

Future<UserCredential> signInWithFacebook() async {
    isFaceBookAuth = true;
    FacebookAuthProvider facebookProvider = FacebookAuthProvider();
    facebookProvider.addScope('email');
    facebookProvider.setCustomParameters({
      'display': 'popup',
    });
    return await FirebaseAuth.instance.signInWithPopup(facebookProvider);
  }

Reproducing the issue

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(MyApp());
}
------------------------------------------------------

class MyApp extends StatelessWidget {
  const MyApp({super.key});

void listenToAuthStates() {
    listener = FirebaseAuth.instance.authStateChanges().listen((User? user) {
      if (user == null) {
        print('User is currently signed out!');
      } else {
        print('User is signed in!' + "uid= " + user.uid);
        }
      }
    });
  }

  Future<UserCredential> signInWithFacebook() async {
    FacebookAuthProvider facebookProvider = FacebookAuthProvider();
    facebookProvider.addScope('public_profile');
    facebookProvider.setCustomParameters({
      'display': 'popup',
    });
    return await FirebaseAuth.instance.signInWithPopup(facebookProvider);
  }

  @override
  Widget build(BuildContext context) {
return Container(child:
Column(children;[
TextButton(
            onPressed: () {
             listenToAuthStates();
            },
            child: AppText(text: "Start auth listener"),
          ),
Sizedbox(height:25),
TextButton(
            onPressed: () {
              signInWithFacebook();
            },
            child: AppText(text: "Facebook auth"),
          ),
]),
);
}

Firebase Core version

3.1.1

Firebase Auth version

4.17.7

Flutter Version

3.22.2

Relevant Log Output

No response

Flutter dependencies

Expand Flutter dependencies snippet
```yaml firebase_auth: ^4.17.7 firebase_core: ^2.26.0 ```

Additional context and comments

No response

TarekkMA commented 3 months ago

@MenaRaafat Thank you for reporting this issue. Can you please provide a minimal but complete reproducible example repo of this issue? Also, could you specify which Facebook library you are using?

MenaRaafat commented 3 months ago

@TarekkMA I modified the code example in my main question above. If u need extra info, please inform me.

Concerning the Facebook library, I only followed the instructions on Firebase on how to add Facebook auth: https://firebase.google.com/docs/auth/flutter/federated-auth

I did not specify a Facebook library.

TarekkMA commented 3 months ago

You will need to use a library for Facebook login. For example, in the docs, they use this library: flutter_facebook_auth. You can check this library's documentation for more details on how to use it with firebase.

MenaRaafat commented 3 months ago

@TarekkMA But, this is in the section concerning iOS+ & Android. The web section says: "On the web, the Firebase SDK provides support for automatically handling the authentication flow using the Facebook application details provided on the Firebase console."

TarekkMA commented 3 months ago

Oh, thank you for the clarification.

I've replicated the code you have written and I've got the authStateChanges() stream to emit the user change and I didn't encounter any issues. Please check this branch and try to run the example auth app to see if you still have the same issue:

https://github.com/firebase/flutterfire/tree/issue/13014

MenaRaafat commented 2 months ago

@TarekkMA I solved my problem by deleting the Facebook user from the Firebase auth portal. After that, everything worked fine. I could be mistaken because I do not understand the logic of the solution, but now I have no problems.