gpt4 book ai didi

firebase - 如何从 Flutter 退出 Google Auth?

转载 作者:IT王子 更新时间:2023-10-29 07:23:24 26 4
gpt4 key购买 nike

我能够成功 - 使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com