【发布时间】:2020-12-19 14:39:53
【问题描述】:
您好,我有 Firebase 项目,当我创建数据库时,我会创建一些测试规则。 现在,它们过期了,他们关闭了我的项目。 这是我第一次使用 Firebase 项目,我没有经验。我将向您展示我是如何为 Cloud Firestore 和实时数据库定义规则的。 该项目是一个应用程序,用户可以在其中注册和离开他们的 cmets。 我应该如何为我的数据库设置安全规则? 我应该如何编写我的规则代码? 我有几天没有参加我的项目,他们从谷歌给我写信,两天后我的项目就关闭了。我已经查找了信息,但我不知道如何创建规则以便它们正确并且我的项目也可以工作
我编辑我的问题以添加详细信息
在我的应用程序中,我只希望注册用户能够编写 cmets。
Firebase 向我显示的警报如下:
“其安全规则被定义为公开的,因此任何人都可以窃取、修改或删除其数据库中的数据。”
数据库是空的,所以还没有记录。
你能帮帮我吗?如果我写的规则不正确,Firebase 将关闭我的项目,规则不应该公开。
我阅读了documentation that Firebase offers,但我并不真正了解如何创建我的规则。
对于经过身份验证的用户,它们显示如下内容:
// 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 != null;
}
}
}
另一方面,它们显示了这些规则:
**// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}**
我不知道我应该确切地使用哪一个,以及我应该如何编写它们,以便用户可以在我的 React Native 应用程序中留下反馈。
你能帮帮我吗?
我展示了我的数据库规则的代码
//REALTIME DATABASE
{
"rules": {
".read": true,
".write": true
}
}
//CLOUD FIRESTORE
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone with your database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020, 9, 2);
}
}
}
【问题讨论】:
-
如果您不提供有关您的数据库的结构(Firestore 还是 Realtime Databaqse BTW?)以及您想要实现哪种访问权限(“应用程序在哪些用户可以注册和离开他们的 cmets”太模糊了)?
-
谢谢@RenaudTarnec,我会编辑这个问题。我只希望在应用程序中注册的用户写入数据库。数据库仍然是空的
-
请查看下面提供的解决方案,并让我们知道它们是否适合您,以便进一步了解。
-
继续写感谢@NibrassH 的支持,目前这对我不起作用
-
我在下面发布了一个答案,请查看它并让我知道它是否适合您。谢谢。
标签: firebase firebase-realtime-database google-cloud-firestore firebase-security rules