【问题标题】:Firebase email verification FlutterFirebase 电子邮件验证 Flutter
【发布时间】:2020-07-16 07:52:06
【问题描述】:

我知道这个问题被问了很多,我花了很多时间阅读并尝试实施答案。因此,我试图从 Firebase Auth 获取来自 isEmailVerified 的响应,它确实有效,但现在它总是返回 false,除非我刷新应用程序或关闭它并重新打开它。这显然是一种糟糕的用户体验。如何在不关闭应用的情况下获得更新响应。

这里是相关的代码。

 Future<bool> isEmailVerified() async {
    FirebaseUser user = await _auth.currentUser();

    if (user == null) {
      return false;
    } else {
     await user.reload();
     user = await _auth.currentUser();
      return user.isEmailVerified;
    }
  }

main.dart

  child: Consumer<Auth>(
        builder: (_, auth, __) => MaterialApp(
          theme: Provider.of<ThemeNotifier>(context).getTheme(),
          home: FutureBuilder(
            future: Future.wait([auth.isEmailVerified(), auth.tryAutoLogin()]),
            builder: (BuildContext ctx, AsyncSnapshot authResultSnapshot) =>
                authResultSnapshot.connectionState == ConnectionState.done
                    ? authResultSnapshot.data[1]
                        ? authResultSnapshot.data[0]
                            ? HearingsScreen()
                            : SplashScreen(
                                emailVerified: true,
                              )
                        : LoginScreen()
                    : SplashScreen(),
          ),

在我重新启动应用程序之前它不会返回 true
除此之外我尝试过的事情:
1) await user.getIdToken(refresh: true);
2) 注销用户然后重新登录
3) firebase_user_stream 包

感谢任何帮助。

【问题讨论】:

  • 尝试在isEmailVerified() 中添加notifylistener() 这是我的猜测
  • notifyListener() 在我的ConnectionState.waiting 中卡住FutureBuilder
  • 你一般不能使用未来的建设者和提供者
  • 我显然不知道我会检查出来thx

标签: firebase flutter firebase-authentication


【解决方案1】:

我已经用下面的代码在闪屏中实现了相同的场景,您可以根据您的要求进行更改。 :

//To check is User is logged in
  Future<bool> isLoggedIn() async {
    FirebaseUser user = await _fireBaseAuth.currentUser();
    if (user == null) {
      return false;
    }
    return user.isEmailVerified;
  }

countDownTime() async {
    return Timer(
      Duration(seconds: splashDuration),
      () async {
        if (await userAuth.isLoggedIn()) {
            Navigator.pushReplacement(
              context,
              ScaleRoute(
                widget: HomeScreen(),),
            );
          }
        } else {
          Navigator.pushReplacement(
            context,
            ScaleRoute(
              widget: LoginScreen(),),
          );
        }
      },
    );
  }

  @override
  void initState() {
    super.initState();
    countDownTime();
  }

更新
需要定期在 initState() 函数中实现 isEmailVerified ,这可能是使用 firebase 执行验证的理想方法。

bool _isUserEmailVerified;
Timer _timer;

@override
void initState() {
    super.initState();
    // ... any code here ...
    Future(() async {
        _timer = Timer.periodic(Duration(seconds: 10), (timer) async {
            await FirebaseAuth.instance.currentUser()..reload();
            var user = await FirebaseAuth.instance.currentUser();
            if (user.isEmailVerified) {
                setState((){
                    _isUserEmailVerified = user.isEmailVerified;
                });
                timer.cancel();
            }
        });
    });
}

@override
void dispose() {
    super.dispose();
    if (_timer != null) {
        _timer.cancel();
    }
}

【讨论】:

  • 您是否在您的应用程序中进行电子邮件验证,这是我在您的示例中遇到问题的步骤,您的所有检查是用户是否不为空,这不能保证 isEmailVerified 等于 true跨度>
  • 您可以通过“user.isEmailVerified”访问。
  • 我猜你不需要在其他部分创建新的用户实例。之前定义的同一个实例足以知道“user.isEmailVerified”。
  • 是的,我实际上已经在我的应用程序中实现了与此非常相似的代码,但 user.isEmailVerified 总是返回 false,直到我关闭应用程序并重新启动它。
  • 你应该试试这个:stackoverflow.com/a/60471750/6911538
猜你喜欢
  • 2021-06-24
  • 2020-12-12
  • 1970-01-01
  • 2022-01-24
  • 2020-10-19
  • 2021-05-13
  • 2017-05-30
相关资源
最近更新 更多