loginWithGoogle method

Future<void> loginWithGoogle()

Implementation

Future<void> loginWithGoogle() async {
  try {
    _requireFirebaseAuth();
    final googleSign = GoogleSignIn(scopes: ['profile', 'email']);
    GoogleSignInAccount? account = await googleSign.signInSilently();
    account ??= await googleSign.signIn();
    final googleAuth = await account?.authentication;
    final cred = GoogleAuthProvider.credential(
      idToken: googleAuth?.idToken,
      accessToken: googleAuth?.accessToken,
    );
    final userCred = await _firebaseAuth!.signInWithCredential(cred);
    final accessToken = cred.accessToken ?? googleAuth?.idToken ?? '';

    requireSmartLinksNativeBridge();
    final data = await SmartLinksNativeApi.socialAuthGoogle(
      accessToken: accessToken,
      project: CommunityConstants.firebaseProject.isNotEmpty
          ? CommunityConstants.firebaseProject
          : null,
    );
    if (data != null && data['token'] != null) {
      await CommunityStorage.saveAuthToken(data['token'] as String);
      user.value = userCred.user;
      if (user.value != null) {
        isSignedIn.value = true;
        _onLogin();
      }
    }
  } catch (e) {
    Get.snackbar('Error', 'Google sign-in failed. ${e.toString()}');
    debugPrint('Google Login Failed: $e');
  }
}