/ users的侦听器失败:permission_denied

was*_*tar 2 ios firebase swift firebase-authentication firebase-realtime-database

我有使用Firebase的注册流程。当我检查数据库中是否已存在电子邮件时,如下所示:

refUsers.queryOrdered(byChild: "email").queryEqual(toValue: emailText).observeSingleEvent(of: .value, with: { snapshot in

      if (snapshot.value is NSNull) {
            print("Unique email")

            // Move to Password View.
            let passwordViewController = self.storyboard?.instantiateViewController(withIdentifier: "PasswordViewController") as! PasswordViewController

            self.navigationController?.present(passwordViewController, animated: true, completion: nil)

            // Pass the emailText to the last View of the flow.
            self.singleton.sharedInstance.emailText = emailText!

      }
      else {
            print("Duplicate email")
      }  
})
Run Code Online (Sandbox Code Playgroud)

问题是,我没有查看数据库中的/ users的权限,原因是我的规则是:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以找到一封电子邮件是否是重复的,Auth.auth().createUser但不仅仅是我要检查的注册流程中的电子邮件。对于唯一的用户名,我也使用相同的方法。我该如何实现?

Zas*_*ssX 5

如您所见,这不是最好的方法。您不应该手动检查电子邮件是否已经存在-用户注册后,Firebase可以为您做到这一点,为什么您不想使用它?

您需要的是另一种方法。我现在可以想到两种方式:

第一:

您可以向Firebase添加新规则,例如:

{
    "rules": {
    "usernames": {
      ".read": true,
      ".write": "auth != null"
    },
    "emails": {
      ".read": true,
      ".write": "auth != null"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您在这里创建的新节点名为 usernames每个用户都可以访问和读取。在这里,您应该持有已注册用户拥有的所有用户名的副本,并在注册时检查用户用户名是否已在此节点内。

第二种方式:

您可以稍微修改注册流程,让用户无需用户名即可注册。创建帐户后,您可以让他们设置用户名。流程顺畅,看起来就像是相同的注册表格。

更新 使用上述规则,用户应该能够阅读emailsusernames无需注册。这样,您可以获取数据并比较是否已使用电子邮件或用户名。

只要确保在用户注册时,您将其电子邮件和用户名插入这两个节点即可。