Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 559
magee05 Avatar asked Jan 17 '20 15:01

magee05


2 Answers

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.

like image 161
Ovidiu Avatar answered Oct 18 '22 20:10

Ovidiu


I had the same issue - it looks like we're working from the same tutorial (I'm new at this too). For me, the problem was the SHA certificate fingerprints in Firebase - if you haven't set them up then the sign in simply won't do anything. In the terminals tab I could see that sign-in simply cancels itself.

To fix the issue I did this:

  • Set up my Key file (keystore - yes initially I forgot to do this!)
  • Added my keystore file to the Android folder
  • Created a key.properties file in Android/app/ folder
  • Added my dependencies for the file in Android/app/build.gradle
  • From terminal window run android/gradlew.bat
  • Then in the same window run "gradlew signingReport" - this will give you SHA1 and SHA256 keys
  • Go in to firebase console, project, settings, general, andorid apps and enter the SHA certificate fingerprints that grade gave you.

Then rebuild the app and it should work.

like image 4
James Avatar answered Oct 18 '22 18:10

James