【问题标题】:How do I create a record in firebase with a specific users credential?如何使用特定用户凭据在 Firebase 中创建记录?
【发布时间】:2021-10-14 05:18:39
【问题描述】:

我需要一个具有联系表单的应用。我需要使用设置为特定用户的 uid 的实时数据库规则安全地添加记录,说我的凭据,以防止有人从我的表单以外的任何地方将记录添加到 dB。 我看过的所有文章都谈到了测试模式下的 firebase,规则设置为 read 和 write 为 true,但这样做有人可以访问我的 dB 并从任何地方填充记录。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    查看 implementing content-owner only access 上的 Firebase 文档,其中包含实时数据库的这些规则:

    {
      "rules": {
        "some_path": {
          "$uid": {
            // Allow only authenticated content owners access to their data
            ".read": "auth != null && auth.uid == $uid"
            ".write": "auth != null && auth.uid == $uid"
          }
        }
      }
    }
    

    要写入此数据库,用户需要登录,然后:

    firebase.database().ref('some_path').child(firebase.auth().currentUser.uid).set("hello world");
    

    【讨论】:

    • 我已经对此进行了研究,但不确定我们如何在 javascript 中实现这一点。我的理解是首先我们必须使用内容所有者的 id 登录,这将设置 auth.uid,但是我们如何在数据库中创建记录时传递该 uid。有没有一种方法可以在创建记录时指定内容所有者的 uid。
    • @sud 您是否要为特定用户创建收件箱?
    • 用户登录后,他们的 ID 令牌会随每个请求自动传递给 Firebase,并且会自动填充规则中的 auth.uid
    • @FrankvanPuffelen 谢谢。它就像一个魅力。
    猜你喜欢
    • 2021-06-10
    • 2019-02-22
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2019-06-02
    • 2017-01-24
    • 1970-01-01
    相关资源
    最近更新 更多