Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sign out form Google Auth from Flutter?

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?

like image 816
user1961 Avatar asked Jan 28 '19 05:01

user1961


1 Answers

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();
}
like image 128
satish Avatar answered Nov 14 '22 03:11

satish