简体   繁体   English

Flutter sign in with google lost authentication 当inte.net连接丢失时,如何重新授权?

[英]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 文档有一个重新验证用户的选项,听起来与您的情况非常相似。

Re-authenticate a user 重新验证用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌使用 flutter 和 firebase auth 登录 - google sign in with flutter and firebase auth Firebase Auth 注销是否需要互联网连接? - Does Firebase Auth sign out requires internet connection? 如何向数据库发出我失去连接的信号 - Java - How to signal to the database that I have lost the connection - Java Github 操作:因“失去连接”而失败 - Github Action : failed with "lost connection" Flutter 集成测试 firebase 身份验证(电子邮件链接或谷歌登录) - Flutter integration test firebase auth (email link or google sign in) Flutter 离线更新后快照丢失数据 - Flutter snapshot lost data after update in offline 如何让 python 脚本 (abc.py) 在连接丢失或 SSH 连接终止后继续在 AWS 上执行? - How to let the python script (abc.py) keep executing on AWS even after Connection lost or SSH connection is terminated? 尝试使用 Flutter Firebase 再次登录时,Google 登录包的断开连接方法调用导致错误 - Google Sign In package's disconnect method call results in error when trying to sign in again using Flutter Firebase 您如何使用 NextJS 和 Safari 上的 Firebase Auth“使用 Google 登录” - How do you "Sign In with Google" with NextJS and Firebase Auth on Safari 完全迷失了如何使用 js async - Absolutely lost on how to use js async
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM