firebase - 如何从 Flutter 退出 Google Auth?

标签 firebase dart flutter firebase-authentication

我能够成功 - 使用 Google 和 Facebook 使用 firebase 登录用户:

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 身份验证用户,在这种情况下我可以执行

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

此外,如果我的退出功能在不同的小部件和文件中,我如何传递要引用的 GoogleSignIn 对象以退出?

最佳答案

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

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

你会得到 true 或 false 使用这个你可以使用 sign out 方法。 对于您的第二个问题,解决方案是为 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();
}

关于firebase - 如何从 Flutter 退出 Google Auth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54396075/

相关文章:

firebase - 如何配置我的 Google 身份平台以在不自动链接凭据的情况下执行 'single account per email address' 规则?

angularjs - Angular Dart 和表单验证

flutter - Get.to(MyPage())-如何删除所有先前的路线-Flutter GetX

image - 如何在 Flutter 中重叠两个小部件?

android - android studio 中的错误 Firebase 配置

android - FireBaseInstanceId 服务未注册

Laravel API - 使用 Firebase FCM 向 Android 和 iOS 客户端应用程序发送通知

user-interface - 使用 Flutter/Dart 构建自定义卡片

Flutter - firebase_auth_web 5.4.0 取决于 intl ^0.17.0 但我必须使用更高版本(intl ^0.18.0)

flutter - Dart 中的 int 类型是值类型还是引用类型?