简体   繁体   English

在颤动中暂停和恢复 FirebaseAuth 状态更改侦听器

[英]Pause and resume FirebaseAuth state changes listener in flutter

I am working on an application in which When a user is new, he must fill a form before going to the home page after registration.我正在开发一个应用程序,当用户是新用户时,他必须在注册后进入主页之前填写表格。 I am using Firebase authentication with social logins and with email also.我正在将 Firebase 身份验证与社交登录和电子邮件一起使用。

The issue is that as soon as the auth state is changed in social logins module, the listener is notified and it goes to homePage instead of going to that form page (the listener is in the wrapper class that is listening to the state changes whether the user is signed in or not).问题是,只要在社交登录模块中更改身份验证状态,就会通知侦听器,它会转到 homePage 而不是转到该表单页面(侦听器位于正在侦听状态更改的包装类中用户是否已登录)。 But when I register with email directly it works fine.但是当我直接使用电子邮件注册时,它工作正常。

Below is the StreamBuilder which is listening to the auth state changes下面是监听 auth 状态变化的 StreamBuilder

StreamBuilder<User>(
        stream: Authentication().auth.authStateChanges(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.active) {
            User user = snapshot.data;
            if (user != null) {
              return Layout();
            }
            else {
              return SignIn();
            }
          }
          else {
            return Scaffold(
              body: Center(
                child: CircularProgressIndicator(),
              ),
            );
          }
        },
      )

By checking the console logs, I saw that the listener is notified and the state is changed to logged in and the homepage is called immediately, the homepage should only be called if the user is logged in && is an old user.通过查看控制台日志,我看到监听器被通知并且状态变为登录并立即调用主页,只有当用户登录&&是老用户时才应该调用主页。 If the user is new && logged in then the form page should be called.如果用户是新的&&登录,则应调用表单页面。 I am not sure how to handle this condition as the authSateChanges() is returning a Stream which does not give detail that the user is new or old.我不确定如何处理这种情况,因为 authSateChanges() 正在返回一个 Stream,它没有提供用户是新用户还是旧用户的详细信息。

Is there a way to pause the listener in AUthentication class when we get the credentials because there I get a chance to see if the user is new or not by the following line of code当我们获得凭据时,有没有办法暂停 AUthentication 类中的侦听器,因为我有机会通过以下代码行查看用户是否是新用户

UserCredential result = await auth.signInWithCredential(credential);
if(result.additionalUserInfo!.isNewUser){}

Using .isNewUser is probably not a good method to know if the user is newly created if you strictly want the user to fill up the form before going to homepage.如果您严格希望用户在进入主页之前填写表单,则使用.isNewUser可能不是了解用户是否是新创建的好方法。 Because consider the situation where user uninstalls app right after login without filling up the form and then reinstalling again which will then make the user as old user and would thus not ask to fill up the form, when it actually should ask, so this is why this method is suitable.因为考虑到用户在登录后立即卸载应用程序而不填写表格然后再次重新安装的情况,这将使用户成为旧用户,因此不会要求填写表格,当它实际上应该询问时,这就是为什么这个方法很合适。

I suggest you using Custom Claims in firebase authentication using Cloud Functions.我建议您使用 Cloud Functions 在 Firebase 身份验证中使用自定义声明。 With custom claims you can set key, value pairs to denote whether a user is fully registered or not.通过自定义声明,您可以设置键值对来表示用户是否完全注册。 Once registered you can update the custom claims and pass on that to user with updated claims which can thus make user to land up on homepage everytime he signs in.注册后,您可以更新自定义声明并将其传递给具有更新声明的用户,从而使用户每次登录时都能登陆主页。

To update a custom claims from nodejs environment, you can use admin.auth().setCustomUserClaims(uid, {formFilled: true}) when you see the form data is updated.要从 nodejs 环境更新自定义声明,您可以在看到表单数据更新时使用admin.auth().setCustomUserClaims(uid, {formFilled: true})

On the client side update the idToken, to receive updated claims using var result = currentUser.getIdTokenResult() and get claims using result.claims and check if result.claims["formFilled"] == true在客户端更新 idToken,使用var result = currentUser.getIdTokenResult()接收更新的声明并使用result.claims获取声明并检查result.claims["formFilled"] == true

Note that once the custom claims are updated, the client would have to explicitly call the getIdTokenResult() to force refresh tokens.请注意,一旦自定义声明更新,客户端将必须显式调用getIdTokenResult()以强制刷新令牌。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM