【问题标题】:How to verify an email in firebase auth in flutter?如何在flutter中验证firebase auth中的电子邮件?
【发布时间】:2025-11-24 15:35:01
【问题描述】:

我使用 firebase 和 flutter 创建了一个登录屏幕,一切正常,但我希望用户使用真实电子邮件(已验证)而不是任何电子邮件登录。

如果用户使用类似这样的电子邮件点击登录按钮:shsuhsafk@uisl.com,它将接受此电子邮件。

如何验证电子邮件不是伪造的并且实际上属于真实地址。

【问题讨论】:

    标签: firebase email authentication flutter dart


    【解决方案1】:

    为了真正验证用户的电子邮件地址,您需要发送一封需要用户操作的验证邮件。

    仅检查地址是否存在是不够的,因为电子邮件地址可能属于任何人。

    您可以在 Firebase 控制台中设置邮件模板,并使用以下代码发送验证邮件。

    FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
         try {
            await user.sendEmailVerification();
            return user.uid;
         } catch (e) {
            print("An error occured while trying to send email        verification");
            print(e.message);
         }
       }
    

    【讨论】:

      【解决方案2】:
      String email = "user@example.com";
      try {
        String link = FirebaseAuth.getInstance().generateSignInWithEmailLink(
            email, actionCodeSettings);
        // Construct email verification template, embed the link and send
        // using custom SMTP server.
        sendCustomPasswordResetEmail(email, displayName, link);
      } catch (FirebaseAuthException e) {
        System.out.println("Error generating email link: " + e.getMessage());
      }
      

      更多信息请参考以下链接:https://firebase.google.com/docs/auth/admin/email-action-links

      【讨论】: