[英]Flutter sign in with google lost authentication when the internet connection lost, how to re-auth it?
We are currently building an app that requires only google sign in and communicate with the Firebase depending on those data that we get with sign in.我们目前正在构建一个应用程序,它只需要谷歌登录并根据我们通过登录获得的数据与 Firebase 进行通信。
The problem is this;问题是这样的; after logging in to the app if the user lost inte.net connection and then re-establishes that connection without closing the app, FirebaseAuth.currentUser is not null but at the same time the user is not authenticated so the functions that i need to communicate with the DB is not working.
如果用户丢失了 inte.net 连接,然后在不关闭应用程序的情况下重新建立该连接,则登录到应用程序后, FirebaseAuth.currentUser不是 null 但同时用户未经过身份验证,因此我需要通信的功能与数据库不工作。
My question is simply this;我的问题很简单;
How do i check for the authentication and then re-authanticate the user.我如何检查身份验证然后重新验证用户。
My signin method is below, i tried to re-sign in every time when user enters some certain pages but that just not seemed good but does solves the problem.我的登录方法如下,每次用户进入某些特定页面时,我都尝试重新登录,但这看起来不太好,但确实解决了问题。 Is there a optimized way?
有优化的方法吗?
Future<String> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await _googleSignIn!.signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth =
await googleUser!.authentication;
// Create a new credential
final GoogleAuthCredential credential = (GoogleAuthProvider.credential(
accessToken: googleAuth!.accessToken,
idToken: googleAuth.idToken,
) as GoogleAuthCredential);
// Once signed in, return the UserCredential
final UserCredential? authResult =
await _auth!.signInWithCredential(credential);
final User? user = authResult!.user;
//assert(user!.isAnonymous);
assert(await user!.getIdToken() != null);
final User? currentUser = _auth!.currentUser;
assert(user!.uid == currentUser!.uid);
return 'signInWithGoogle succeeded: $user';
}
Edit:编辑:
We were using connectivity_plus plugin so we created a listener for it and if the user changes its connection to wifi or mobile we simply re-signin them.我们使用的是 connectivity_plus 插件,因此我们为其创建了一个侦听器,如果用户更改其与 wifi 或移动设备的连接,我们只需重新登录即可。 It worked for us it may have work for you as well;
它对我们有用,可能对您也有用;
@override
void initState() {
subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) {
if (result == ConnectivityResult.mobile ||
result == ConnectivityResult.wifi ||
result == ConnectivityResult.ethernet) {
Login().signInWithGoogle();
}
print("${result}");
});
// TODO: implement initState
super.initState();
}
dispose() {
super.dispose();
subscription.cancel();
}
The Flutter Firebase docs have a re-authenticate user option and it sounds quite similar to your situation. Flutter Firebase 文档有一个重新验证用户的选项,听起来与您的情况非常相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.