signInUser method

Future<GoogleSignInAccount?> signInUser()

Implementation

Future<GoogleSignInAccount?> signInUser() async {
  Completer<GoogleSignInAccount?> completer =
      Completer<GoogleSignInAccount?>();

  try {
    final iosClientId = PurpiRuntime.config.googleIosClientId;
    final serverClientId = PurpiRuntime.config.googleServerClientId;
    await signIn.initialize(
      clientId: Platform.isIOS && iosClientId != null && iosClientId.isNotEmpty
          ? iosClientId
          : null,
      serverClientId:
          serverClientId != null && serverClientId.isNotEmpty ? serverClientId : null,
    );

    signIn.authenticationEvents
        .listen((GoogleSignInAuthenticationEvent event) {
      if (event is GoogleSignInAuthenticationEventSignIn) {
        completer.complete(event.user);
      } else if (event is GoogleSignInAuthenticationEventSignOut) {
        print('User signed out');
        completer.complete(null);
      }
    });

    await signIn.authenticate();
  } catch (e) {
    print("Google sign-in initialization failed: $e");
    completer.complete(null);
  }

  return completer.future;
}