【问题标题】:Firebase reference without uid没有 uid 的 Firebase 参考
【发布时间】:2018-02-09 02:19:31
【问题描述】:

我正在尝试创建对我的 firebase 数据库的引用并发送 user.uid,但不写入数据库中的该路径。这可能吗?

var dbObj = firebase.database().ref().child(`match/${user.uid}/${randomID}`);
var milliseconds = (new Date).getTime();
return dbObj.update({
    // Set the interested value in the db
    [milliseconds]: 'text'
});

所以我使用 user.uid 来验证用户是否通过 firebase 数据库规则有效登录。我将它与 $uid 匹配,这很有效。

"match": {
  "$uid": {
    ".write": "auth.uid == $uid",
    ".read": "auth.uid == $uid",
  }
}

但我想写入的数据库位置是:../match/randomID/otherData INSTEAD of ../match/userID/randomID/otherData

【问题讨论】:

  • 我不明白你想在这里完成什么。这听起来像是一个我不清楚问题是什么的解决方案(所谓的XY problem)。您能否换句话说解释一下这段代码的目标/预期结果是什么?例如。如果你把这段 sn-p 的代码想象成一个函数,那这个函数的名字是什么?
  • 我问有没有办法将 uid 发送到 firebase 进行规则身份验证,但不写入包含 uid 的数据库路径。示例在问题的最后一行。

标签: javascript firebase-realtime-database firebase-security


【解决方案1】:

是的,可以创建对 Firebase 数据库的引用并发送 user.uid 而无需写入该路径。

假设您有一个spaceships 路径,其中子键是宇宙飞船 ID,值是 shapeship 对象(可能具有重量、成本、启动日期和 creator_id 等键,这是用户 id那艘飞船的创造者)

您的规则可能如下所示:

{
  "rules": {
    "spaceships": {
      "$spaceship_id": {
        // everyone can retrieve the data here
        ".read": true,
        // only the creator can edit this spaceship
        ".write": "data.child('creator_id').val() === $auth.uid"
      }
    }
  }
}

在这种情况下,登录者的 uid 对确定谁可以读取宇宙飞船对象没有任何影响(因为 任何人 都可以)。 uid 值仍用于确定谁可以写入该路径(基于 creator_id 子键),但它包含在您正在检查的路径中,/spaceships/$spaceship_id。 p>

【讨论】:

    猜你喜欢
    • 2020-07-23
    • 2018-02-13
    • 2018-05-19
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多