【问题标题】:How to check if email exists in firebase Auth [duplicate]如何检查firebase Auth中是否存在电子邮件[重复]
【发布时间】:2021-06-12 10:34:09
【问题描述】:

我有一个两步登录表单,用户首先输入他们的电子邮件,然后被带到他们输入密码的下一页。在将用户带到密码页面之前,我想检查用户输入的电子邮件是否有效以及它是否存在于 firebase 中。我已经阅读了一些关于使用fetchSignInMethodsForEmail 的帖子。但是,我不太确定如何实现。

这个想法是,如果电子邮件确实存在,则将用户带到密码页面,但是,如果电子邮件不存在,则会弹出一个带有按钮的快餐栏,将用户带到注册页面。我该怎么做呢?

这就是我目前所拥有的......

FirebaseAuth auth = FirebaseAuth.instance;


    final nextButton = Container(
      margin: const EdgeInsets.only(
        top: 30.0,
      ),
      child: FloatingActionButton(
        onPressed: () async {
          try {
            List<String> userSignInMethods = await auth.fetchSignInMethodsForEmail(email)(
               email: _emailController.text,
            );
            if (userSignInMethods != null) {
              final page = LoginPassword();
              Navigator.of(context).push(CustomPageRoute(page));
            }
          } catch (e) {
            print(e);
            //TODO: Snackbar 
          }
        },
        tooltip: 'Next',
        backgroundColor: AppTheme.define().primaryColor,
        child: Icon(
          AppIcons.next_button,
          size: 20.0,
        ),
      ),
    );

fetchSignInMethodsForEmail 未正确实现,我收到错误消息。

【问题讨论】:

标签: firebase flutter


【解决方案1】:

这可能不是您要查找的内容,但我通常将用户存储在 firestore 的集合中,因为这样我可以为用户存储多个值,例如昵称、图片、积分等。如果您想要一些有关如何执行此操作的示例代码,请告诉我

编辑

这是一些帮助您入门的代码。

首先设置firestore并设置一个名为users的集合。使用您的邮件 ID 在其中创建一个文档,并在其中添加您想要的用户字段。如果您不这样做,您将收到无法找到集合或未设置 Firestore 的错误

注册时,考虑到电子邮件是可变电子邮件和密码,请执行以下操作

FirebaseFirestore.instance.collection('users').doc(email).set((){
   "email":email,
   "pass":pass,
   "nick":nick,//You can add more fields or remove them
});

当您想检查电子邮件是否存在时,请执行以下操作

QuerySnapshot query = await FirebaseFirestore.instance.collection('users').where('email',isEqualTo:email).get();
if (query.docs.length==0){
   //Go to the sign up screen
}
else {
   //Go to the login screen
}

现在要在登录时检查密码,请执行以下操作

QuerySnapshot query = await FirebaseFirestore.instance.collection('users').where('email',isEqualTo:email).get();//You can also pass this query as a parameter when you push the login screen to save some time
if (query.docs[0].get('pass')==pass){
   //Go ahead login
}
else{
   //Display an error message that password is wrong
}

如果您想存储数据以在用户下次打开应用时自动登录,请查看Shared Preference 库。

如果要传递用户数据,请创建一个名为 User() 的类


class User {
  String email;
  String pass;
  String nick;

  User(this.email,this.nick,this.pass);
}

然后你就可以传递它,轻松管理用户数据

【讨论】:

  • 看来这可能是一个不错的解决方案!你能提供代码吗:)
  • 是的。让我添加一些代码
  • 检查一下。我已经添加了您需要登录并使用 firestore 注册您的用户的所有代码
  • 绝妙的答案!非常感谢!!
【解决方案2】:

对于方法fetchSignInMethodsForEmail查看文档,An empty List is returned if the user could not be found. 它不能为空,因为该方法返回一个空列表,因此您可以检查长度。 userSignInMethods.length &gt; 0

【讨论】:

    【解决方案3】:

    您可以像我的代码一样尝试catchexception。并告诉我。

    try {
      _firbaseAuth.createUserWithEmailAndPassword(
        email: 'ab@gmail.com', 
        password: '123456'
      );
    } catch(signUpError) {
      if(signUpError is PlatformException) {
        if(signUpError.code == 'ERROR_EMAIL_ALREADY_IN_USE') {
          /// ab@gmail.com has alread been registered.
        }
      }
    }
    

    【讨论】:

    • 这是个好主意!但是,我在您编写 foo@bar.com 的 if 语句中添加了 print("test")。什么都没有打印。但是终端说电子邮件已经存在。我不确定这里出了什么问题
    • 这应该是注释,你基本上是从另一个 SOF 问题中复制/粘贴代码。
    猜你喜欢
    • 1970-01-01
    • 2020-04-17
    • 2019-07-15
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    相关资源
    最近更新 更多