[英]Fetch database data from firebase to my blog without auth
I've followed the guide from a documentation of firebase on how to fetch the data. 我遵循了Firebase文档中的指南,了解如何获取数据。
I try to play around with the: sample of firebase here 我尝试玩一下: Firebase示例在这里
However, to get database data need to auth. 但是,要获取数据库数据需要进行身份验证。 Is there anyway just only fetch like feed from firebase without auth? 无论如何,是否只有从Firebase像feed那样获取内容而没有auth? I need to read on my own blog to be read for public. 我需要在自己的博客上阅读才能公开阅读。
Please..help me to solve this issue. 请..帮助我解决这个问题。
Thanks. 谢谢。
You could set your rules to this: 您可以为此设置规则:
{
"rules": {
".read": "true",
".write": "auth != null"
}
}
But this means anyone who has access the database can read all the data. 但这意味着任何有权访问数据库的人都可以读取所有数据。
In your code you would do something like this: 在您的代码中,您将执行以下操作:
firebase.database().ref("blogItems").on('value', function(snapshot) {
console.log(snapshot.val());
});
If your setup is like this: 如果您的设置是这样的:
firebase-database-123
|
|_blogItems
|
|_entry1
|
|_entry2
I would recommend you get familiar with Firebase Database Security Rules . 我建议您熟悉Firebase数据库安全规则 。 These are the ones which dictate who can read/write into the Firebase Database. 这些决定了谁可以读写Firebase数据库。
All new projects starts with the rules 所有新项目都从规则开始
{
rules: {
.read: auth != null,
.write: auth != null
}
}
This means anyone who isn't authenticated won't be able to read or write in our database. 这意味着未经身份验证的任何人将无法在我们的数据库中进行读取或写入。
To achieve what you might need without compromising other data you may do something like the following: 为了实现您可能需要的功能而不损害其他数据,您可以执行以下操作:
{
rules: {
.read: auth != null,
.write: auth != null
blogEntries: {
.read: true,
.write: auth != null
}
}
}
By doing this you are allowing everyone to read the data inside blogEntries, and this means ALL the data inside, while if someone wants to write data to blogEntries they should be authenticated. 通过这样做,您允许所有人读取blogEntries中的数据,这意味着其中的所有数据,而如果有人想将数据写入blogEntries,则应该对其进行身份验证。
I recommend watching The key to Firebase Security to further understand what can be achieved and how Security Rules work. 我建议观看Firebase Security的钥匙,以进一步了解可以实现的内容以及安全规则的工作方式。
Yes, you can get the data without authenticating, but it's not recommended . 是的,您无需验证即可获取数据,但不建议这样做 。 I did the following setup in the Firebase. 我在Firebase中进行了以下设置。 Step 1 第1步
Click on Realtime Database's GET STARTED 单击实时数据库的入门
Click on second option start in test mode you can access data without auth 单击第二个选项以测试模式启动,您可以不经过身份验证即可访问数据
or Add following lines in rules 或在规则中添加以下行
{
"rules": {
".read": true,
".write": true
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.