如何从Flutter退出Google Auth表单?

时间:2019-01-28 05:36:25

标签: firebase dart flutter firebase-authentication

我能够成功-使用Firebase同时使用Google和Facebook登录用户:

firebase_auth.dart,flutter_facebook_login.dart,google_sign_in.dart

我可以使用此功能从其他小部件中注销Firebase用户:

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

现在,这是Google和Facebook两种登录类型的全部内容,如何确定用户是否为Google auth用户,在这种情况下我可以执行

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

另外,如果我的退出功能位于其他小部件和文件中,我如何传递要引用的GoogleSignIn对象以退出?

1 个答案:

答案 0 :(得分:1)

GoogleSignIn和FacebookSignIn有bool Future类型的方法。

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

使用此方法,您将得到true或false,可以使用注销方法。 对于解决方案的第二个问题是还要为GoogleSignIn和facebook创建一个全局对象。

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();
}