【问题标题】:Resend Verification through Flutter for Firebase通过 Flutter for Firebase 重新发送验证
【发布时间】:2020-09-15 05:39:05
【问题描述】:

当用户尝试注册已创建的帐户时,我遇到了向他们发送电子邮件验证链接的问题。我正在使用 Flutter 和 Firebase 作为后端数据库进行编码。


final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

   abstract class BaseAuth {
     Stream<String> get onAuthStateChanged;
     Future<String> currentUser();
     Future<String> resendVerifyEmail(BaseAuth auth, String email);
   } 

   class Auth implements BaseAuth {...}

   Future<String> currentUser() async {
      final FirebaseUser user = await _firebaseAuth.currentUser();
      print("Getting current user: " + user?.uid);
      return user?.uid;
   }

  Future<String> resendVerifyEmail(BaseAuth auth, String email) async {
    try {
      final FirebaseUser user = await _firebaseAuth.currentUser();
      user.sendEmailVerification();
      return user?.uid;
    } catch (e) {
      print("An Error occurred while sending verification link");
      print(e.toString());
      return null;
    }
   }

在try块内部,当调用currentUser()方法时,代码在try-catch块没有结束的情况下退出并返回调用resendVerifyEmail的方法。

我很难在调用 resendVerifyEmail 的弹出窗口中重新发送电子邮件验证

请让我知道使用 Flutter 从 Firebase 发送电子邮件验证的任何其他方法或如何处理。

  void _showVerifyEmailDialog(BuildContext context, String email) {
final BaseAuth auth = AuthProvider.of(context).auth;
showDialog(
  context: context,
  builder: (BuildContext context) {
    // return object of type Dialog
    return AlertDialog(
      title: new Text("Account Already Exists"),
      content: new Text("Please verify account in the link sent to email"),
      actions: <Widget>[
        new FlatButton(
          child: new Text("Resend verification email"),
          onPressed: () {
            Navigator.of(context).pop();
            auth.resendVerifyEmail();   <- Called from this line in login.dart

为什么代码在下面的语句中退出而没有移动到下一行发送电子邮件?

final FirebaseUser user = await _firebaseAuth.currentUser();

【问题讨论】:

    标签: flutter firebase-authentication email-verification


    【解决方案1】:

    似乎您在重新发送邮件功能中添加了不必要的项目。以下是我自己的实现方式:

      Future<void> sendEmailVerification() async {
        final FirebaseUser user = await _firebaseAuth.currentUser();
        user.sendEmailVerification();
      }
    

    像这样注册用户后调用它:

      await Auth().sendEmailVerification();
      await Auth().signOut();
    

    确保将用户注销,以便他们在关闭和打开您的应用时无法自动登录。如果用户的电子邮件验证如下:

      Future<bool> isEmailVerified() async {
        final FirebaseUser user = await _firebaseAuth.currentUser();
        return user.isEmailVerified == null ? false : user.isEmailVerified;
      }
    

    只需在您登录此人时调用它。

    【讨论】:

    • 我面临的问题是从弹出窗口调用 resendVerifyEmail 并且最终 FirebaseUser user = await _firebaseAuth.currentUser() 立即退出而不发送帮助链接
    • 您确定要调用代码吗?尝试在 try 函数的第一行添加一个 print 语句来检查。我很确定您使用 Auth().resendVerifyEmail(); 调用它.如果这不起作用,则放置 await auth.resendVerifyEmail();在您的 navigator.pop() 上方
    • _firebaseAuth.currentUser() 导致问题。要么是用户没有登录,要么是你调用不正确。
    • 每次创建账号时,都会自动登录,如果有错误,则登录失败。您可以显示发生了什么的消息。在发送电子邮件之前,您必须确保用户已登录。再次查看我的答案,以便您了解如何发送电子邮件。发送电子邮件后,将他们注销,这样他们在重新启动您的应用程序时就不会进入登录页面。
    • 不知道为什么会这样。你可以谷歌一下。我从来没有想过。
    猜你喜欢
    • 2019-12-04
    • 2020-07-22
    • 2017-09-09
    • 1970-01-01
    • 2021-01-16
    • 2020-12-12
    • 2021-10-07
    • 2020-07-20
    • 2021-10-11
    相关资源
    最近更新 更多