簡體   English   中英

如何從Flutter退出Google Auth表單?

[英]How do I sign out form Google Auth from Flutter?

我能夠成功-使用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對象以退出?

GoogleSignIn和FacebookSignIn的方法類型為bool。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM