简体   繁体   English

如何从Flutter退出Google Auth表单?

[英]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同时使用Google和Facebook登录用户:

firebase_auth.dart, flutter_facebook_login.dart, google_sign_in.dart 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: 我可以使用此功能从其他窗口小部件注销Firebase用户:

  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 现在,这是Google和Facebook这两种登录方式的全部内容,如何确定该用户是否为Google auth用户,在这种情况下我可以执行

    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? 此外,如果我的退出功能位于其他小部件和文件中,如何传递要引用的GoogleSignIn对象以退出?

There is bool Future type of method for GoogleSignIn and FacebookSignIn. GoogleSignIn和FacebookSignIn的方法类型为bool。

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. 使用此方法,您将得到true或false,可以使用注销方法。 and for your 2nd problem of the solution is to create a global object for GoogleSignIn and facebook as well. 对于解决方案的第二个问题是还要为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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM