firebasefluttergoogle-signin

Flutter Firebase Google Sign In not working. Stops after account selection


I am currently trying to implement google sign in and authentication within my app. I have set up a project and created an app in Firebase, put the google-services.json in the application, added the necessary plugin to build.gradle, and set the SHA-1 key in firebase. Email authentication is working great, however google authentication is causing problems.

All authentication is done from an 'authentication' package I have created. It contains an authentication page and performs all auth logic. The 'main' flutter app which is run, imports this package for use. This 'main' project's build.gradle files contain the additions for the google services plugin (com.google.gms:google-services:4.3.2). Once the 'main' project is run it checks if the user is currently logged in. If not it passes control to the 'authentication' package to display an auth screen and handle the login.

The problem occurs after selecting the google account I want to login with. Once the account is selected, the account selection dialog closes and nothing happens after that. The user is not authenticated (confirmed in Firebase), no exceptions are thrown, and the try-catch surrounding everything does not catch any exceptions. It seems as if all execution of the function halts. I can still move through the app, but the sign in method never finishes execution.

You can see the sign in method below. After the execution of the line final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn(); execution does not continue for some reason.

///
  Future<String> _signUpWithGoogle() async {
    bool isLoggedIn = await _firebaseAuth.isLoggedIn();
    if (!isLoggedIn) {
      googleSignIn = GoogleSignIn();
      final GoogleSignInAccount googleSignInAccount =
          await googleSignIn.signIn();
      final GoogleSignInAuthentication googleSignInAuthentication =
          await googleSignInAccount.authentication;

      final AuthCredential credential = GoogleAuthProvider.getCredential(
        accessToken: googleSignInAuthentication.accessToken,
        idToken: googleSignInAuthentication.idToken,
      );

      final AuthResult authResult =
          await _firebaseAuth.signInWithCredential(credential);
      final FirebaseUser user = authResult.user;

      assert(!user.isAnonymous);
      assert(await user.getIdToken() != null);

      final FirebaseUser currentUser = await _firebaseAuth.getUser();
      assert(user.uid == currentUser.uid);

      return 'signInWithGoogle succeeded: $user';
    } else {
      FirebaseUser user = await _firebaseAuth.getUser();
      return 'signInWithGoogle succeeded: $user';
    }
  }

I would think a PlatformException should come back if something went wrong, but no exceptions are returned the first time. If I trigger the google sign in method again I recieve a PlatformException that says:

PlatformException(error, Concurrent operations detected: signIn, signIn, null)

Below you can also find the dependencies for the 'main' app and the 'authentication' package:

'main' app dependencies

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  permission:
  intl_translation:
  flutter_svg:
  sqflite:
  path:
  path_provider:
  uuid:
  qr_flutter:
  vibration:
  reflectable:
  provider:
  launcher_module:
    path: ../modules/launcher_module
  individual:
    path: ../modules/individual
  common:
    path: ../packages/common
  persistence:
    path: ../packages/persistence
  resources:
    path: ../packages/resources
  authentication:
    path: ../packages/authentication
  screen:
  flutter_screen_scaler: ^0.0.1
  page_indicator:
  toast: ^0.1.5
  rxbus: ^0.0.2
  shared_preferences:

'authentication' package dependencies

dependencies:
  flutter:
    sdk: flutter
  firebase_auth: ^0.15.3+1
  google_sign_in: ^4.1.1
  flutter_facebook_login:
  shared_preferences:
  common: 
    path: "../common"
  toast: ^0.1.5

I have also tried moving all authentication logic into the main app and removed the reference to the 'authentication' package. However, the same thing still happens.

Is there something I am doing wrong or missing in all of this?


Solution

  • Just to turn my previous comment into an official answer:

    Initial comment:

    There doesn't seem to be anything wrong with the code above. I have nearly identical code that works, with the same version of the google_sign_in plugin. What I would do is debug the native code of the plugin in order to figure out where it gets stuck and then understand why that happens. Given the PlatformException that you mentioned, and after having a quick glance at the plugin's code, it definitely gets stuck somewhere between the onActivityResult callback when selecting the account to sign in with, and sending the result back to Flutter.

    After author replied:

    It turns out that the author had customized the app's starting Activity such that onActivityResult was no longer calling super.onActivityResult, which prevented the google_sign_in plugin from sending data from the native side back to the Flutter side.

    The same mechanism is used whenever an Intent that is meant to return data is launched, such as requesting permissions, or taking a photo with a different app.