Firebase 安全:通过电子邮件查找其他用户

Firebase security: find other users via email

假设我们在 Firebase 中有这些数据。

usersMail
    - "example1@mail.com": "1"
    - "example2@mail.com": "2"
    - "example3@mail.com": "3"

user1 是否可以在不将 usersMail 设置为所有用户都可迭代的情况下查询自己的电子邮件列表以查找正在使用该应用程序的朋友?

如果这在 Firebase 中不可行,我是否必须设置一个具有管理员帐户的服务器来进行查询?对后端的东西还是很陌生,所以我很感激你的帮助!

所以问题是:Firebase 中存在两个用户,uid_0 和 uid_1,我们需要 uid_0 能够通过电子邮件搜索 uid_1。

但是,我们要防止其他用户迭代用户节点。

答案是否定的。这是不可能的。要通过电子邮件查询用户节点中的另一个用户,用户必须有权访问该节点中的每个用户。

不过可能还有其他选择。假设uid_1(doug)知道uid_0(bob)想找到他加他为好友。

users
   uid_0:
     email: bob@greatwhitenorth.com
   uid_1:
     email: doug@greatwhitenorth.com

然后是一个将用户链接在一起的节点,在这种情况下,doug 知道 bob 会找他,我们包含 doug 的 uid,所以当 bob 读取该节点时,它将包含在子数据中

email_finder
   doug@greatwhitenorth.com
        bob@greatwhitenorth.com: true
        uid: uid_1

和限制访问的规则

rules for email_finder node
   $email
      read: root.child('email_finder').child($email)
                .child(
                       root.child('users').child(auth.uid).child('email').val
                      )
                .val = true

如果我输入正确,

root.child('users').child(auth.uid).child('email').val

应该从用户节点检索当前用户的电子邮件,在本例中为 bob@greatwhitenorth.com,因此调用 X

然后

.child($email).child(X).val = true

确保doug节点内email = true(存在)

然后直接观察 return 包含 uid

的节点
let thisUserRef = emailFinderRef.childByAppendingValue("doug@greatwhitenorth.com")
thisUserRef.ObserveEventOfType(.Value...... {
 //capture the aid
}

您还希望用户节点上的规则只允许用户也读取他们自己的节点。

这完全未经测试,甚至可能完全错误,但可能会为您提供可能解决方案的方向。