6

I am able to successfully - sign in a user using firebase using both Google and Facebook:

firebase_auth.dart, flutter_facebook_login.dart, google_sign_in.dart

I am able to sign out the firebase user using this function from a different widget:

  Future<void>_signOut() async {
    final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
    return _firebaseAuth.signOut();
  }

Now this is a catch-all for both types of logins, Google and Facebook, how can I determine if the user is a Google auth user in which case I can execute

    final GoogleSignIn _googleSignIn = new GoogleSignIn();
...
    _googleSignIn.signOut();

Additionally, if my sign out function is in a different widget and file how can I pass the GoogleSignIn object to be referenced to sign out?

KENdi
  • 7,576
  • 2
  • 16
  • 31
user1961
  • 1,270
  • 2
  • 17
  • 27

1 Answers1

10

There is bool Future type of method for GoogleSignIn and FacebookSignIn.

final facebookLogin = FacebookLogin();
final GoogleSignIn googleSignIn = new GoogleSignIn();
     googleSignIn.isSignedIn().then((s) {});
      facebookLogin.isLoggedIn.then((b) {});

you will get true or false using this you can use sign out method. and for your 2nd problem of the solution is to create a global object for GoogleSignIn and facebook as well.

import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter_facebook_login/flutter_facebook_login.dart';

final facebookLogin = FacebookLogin();
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = new GoogleSignIn();

Future<FirebaseUser> signInWithGoogle() async {
  // Attempt to get the currently authenticated user
  GoogleSignInAccount currentUser = _googleSignIn.currentUser;
  if (currentUser == null) {
    // Attempt to sign in without user interaction
    currentUser = await _googleSignIn.signInSilently();
  }
  if (currentUser == null) {
    // Force the user to interactively sign in
    currentUser = await _googleSignIn.signIn();
  }

  final GoogleSignInAuthentication googleAuth =
      await currentUser.authentication;

  // Authenticate with firebase
  final FirebaseUser user = await firebaseAuth.signInWithGoogle(
    idToken: googleAuth.idToken,
    accessToken: googleAuth.accessToken,
  );

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

  return user;
}

Future<Null> signOutWithGoogle() async {
  // Sign out with firebase
  await firebaseAuth.signOut();
  // Sign out with google
  await googleSignIn.signOut();
}
satish
  • 1,304
  • 3
  • 13
  • 33
  • Thank you Satish - this will work. A follow up question is, how do I make the variables global across files? I've created modules in separate files that can trigger the logout function. – user1961 Jan 28 '19 at 17:36
  • Is this the best solution? https://stackoverflow.com/questions/29182581/global-variables-in-dart – user1961 Jan 28 '19 at 17:55
  • 1
    I am also using this concept, and it's working fine. So i think it's good to go with it – satish Jan 28 '19 at 18:42
  • Ok - I've put all my auth objects into a globals.dart file for reference across the app and it works. The only trick was I can't name/prefix global variables with _ (as someone new to dart, this was new) – user1961 Jan 28 '19 at 19:52
  • 1
    ya sorry, for that we can not _ for global _ mean to create a private var or method. I will edit that mistake. – satish Jan 29 '19 at 08:41