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.62k stars 3.95k forks source link

[firebase_auth_web]: Google Sign In Fails on Web #13229

Closed LoadJulz closed 1 week ago

LoadJulz commented 2 weeks ago

Is there an existing issue for this?

Which plugins are affected?

Auth

Which platforms are affected?

Web

Description

After issues with the new google sign in I now want to use signInWithPopup from FirebaseAuth. As stated with in the docu this seems pretty straight forward.

My prev code looked like this:

@override
  Future<Either<String, auth.User>> signInWithGoogle() async {
    try {
      final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

      final GoogleSignInAuthentication? googleAuth =
          await googleUser?.authentication;

      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth?.accessToken,
        idToken: googleAuth?.idToken,
      );

      final result =
          await FirebaseAuth.instance.signInWithCredential(credential);

      final newUser = result.user;

      if (newUser == null) {
        throw Error();
      } else {
        return right(newUser);
      }
    } catch (e) {
      print("Error");
      print(e);
      return left(WebserviceErrorHandler.handleError(e));
    }
  }

And now I have the replaced it with the following, because the old one failed with the new versions:

@override
  Future<Either<String, auth.User>> signInWithGoogle() async {
    try {
      GoogleAuthProvider googleProvider = GoogleAuthProvider();

      googleProvider.addScope('email');
      googleProvider.addScope('profile');

      final result =
          await FirebaseAuth.instance.signInWithPopup(googleProvider);

      final newUser = result.user;

      if (newUser == null) {
        throw Error();
      } else {
        return right(newUser);
      }
    } catch (e) {
      print("Error");
      print(e);
      return left(WebserviceErrorHandler.handleError(e));
    }
  }

But now I am getting the error: The popup has been closed by the user before finalizing the operation. Is there any workaround to do so?

Reproducing the issue

  1. Trigger the signInWithPopup
  2. Login with a google account
  3. See the error in the browser console

Firebase Core version

3.4.0

Flutter Version

3.24.0

Relevant Log Output

[firebase_auth/popup-closed-by-user] The popup has been closed by the user before finalizing the operation.

Flutter dependencies

Dart SDK 3.5.0 Flutter SDK 3.24.0 applearning 1.0.0+1

dependencies:

dev dependencies:

dependency overrides:

transitive dependencies:

Additional context and comments

No response

SelaseKay commented 2 weeks ago

Hi @LoadJulz . Thanks for reporting. I'm unable to reproduce this issue. Which browser did you experience this on?

LoadJulz commented 2 weeks ago

Tried Chrome Desktop and Mobile and Safari. All on Apple devices.

LoadJulz commented 2 weeks ago

Could it be that this has something to do with a wrong redirect URL? My host app uses a Firebase URL, but without the 'firebaseapp' ending. However, on the Google auth screen, it says that it will redirect to a URL with 'firebaseapp'. You can also check the screenshot for better understanding. IMG_A00C408BADCE-1

SelaseKay commented 2 weeks ago

I'm not quite sure. Can you provide a full sample code reproducing this issue?

LoadJulz commented 1 week ago

I've found the issue. Because I was migrating to WASM on another target in my code I needed to add the following to my firebase.json:

"headers": [
      {
        "source": "**",
        "headers": [
          {
            "key": "Cross-Origin-Embedder-Policy",
            "value": "require-corp"
          },
          {
            "key": "Cross-Origin-Opener-Policy",
            "value": "same-origin"
          }
        ]
      }
    ]

This leaded to the error for builds that don't use WASM. I will now differentiate between different targets and which firebase.json to use.

SelaseKay commented 1 week ago

Great