gpt4 book ai didi

flutter - 如何检查用户是否已使用 Google Sign in 和 Firebase Auth 登录以检索他们的数据?

转载 作者:行者123 更新时间:2023-12-03 13:30:34 24 4
gpt4 key购买 nike

我有一个用 Flutter 制作的简单游戏,我可以登录用户,但我不知道如何检查用户是否已登录以及如何获取用户的数据。

对于登录,我使用这种方法,登录用户后,我可以获取他的数据:

Future<String> signInWithGoogle() async{

print("INICIALIZANDO LOGIN COM GOOGLE...");

googleSignInAccount = await googleSignIn.signIn();
googleSignInAuthentication = await googleSignInAccount.authentication;

final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,

);

AuthResult authResult = await _auth.signInWithCredential(credential);

user = authResult.user;

assert(!user.isAnonymous);
assert(await user.getIdToken() != null);

currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);

uid = user.uid;
name = user.displayName;
email = user.email;
imageUrl = user.photoUrl;

print(uid);
print(email);
print(name);
print(imageUrl);

return 'signInWithGoogle succeded: $user';

}

对于 signOut 这个:
  void signOutGoogle() async{

print("DESLOGOU!");
await googleSignIn.signOut();

}

最佳答案

MyAppClient.dart

Future getCurrentUser() async {
FirebaseUser _user = await FirebaseAuth.instance.currentUser();
print("User: ${_user.displayName ?? "None"}");
return _user;}

Main.dart
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: GSTheme.backgroundColor,
),
debugShowCheckedModeBanner: false,
home: MyAppClient().getCurrentUser() != null
? HomeViewController()
: LoginViewController(),
);

关于flutter - 如何检查用户是否已使用 Google Sign in 和 Firebase Auth 登录以检索他们的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57649410/

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