首页 > 解决方案 > Firebase/数据库 - 第 2 版安全规则?

问题描述

我刚刚开始使用 Firebase,并注意到有很多教程/文档指导您将以下内容放入数据库规则中:

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

但是,似乎有这个代码的新版本,版本 2。我想知道我上面的代码是否已经过时,版本 1(我猜),基本上等同于这个代码:

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

来自 google firebase docs ( https://firebase.google.com/docs/firestore/security/get-started )

谢谢

标签: firebasesecurityfirebase-realtime-databasefirebase-securityrules

解决方案


您正在显示来自彼此不直接相关的两种不同产品的规则。

您的第一个示例用于 Firebase 实时数据库。这种基于 JSON 的规则语言多年来没有改变。

您的第二个示例适用于 Firestore。这是一种完全不同的安全规则语言,与 Firebase 实时数据库有点相似,但完全不同。

您所指的规则“版本 2”仅适用于 Firestore。它改变了语言的几个方面的行为,仅此而已。


推荐阅读