use*_*961 6 dart firebase firebase-authentication flutter
我能够成功 - 使用 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();
}
Run Code Online (Sandbox Code Playgroud)
现在这是 Google 和 Facebook 两种登录类型的统称,我如何确定用户是否是 Google auth 用户,在这种情况下我可以执行
final GoogleSignIn _googleSignIn = new GoogleSignIn();
...
_googleSignIn.signOut();
Run Code Online (Sandbox Code Playgroud)
此外,如果我的注销功能在不同的小部件和文件中,我如何传递要引用的 GoogleSignIn 对象以注销?
GoogleSignIn 和 FacebookSignIn 有 bool Future 类型的方法。
final facebookLogin = FacebookLogin();
final GoogleSignIn googleSignIn = new GoogleSignIn();
googleSignIn.isSignedIn().then((s) {});
facebookLogin.isLoggedIn.then((b) {});
Run Code Online (Sandbox Code Playgroud)
你会得到真或假使用这个你可以使用退出方法。解决方案的第二个问题是为 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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3190 次 |
| 最近记录: |