简体   繁体   English

如何为 REST 请求设置 Firebase 实时数据库规则?

[英]How to set up Firebase Realtime Database-rules to REST-requests?

In a current project, we are using React to create a website which is going to display some information from our Firebase Realtime Database.在当前项目中,我们正在使用 React 创建一个网站,该网站将显示来自我们的 Firebase 实时数据库的一些信息。 We are using the firebase.auth() -package in npm for our authentication, where the only user that will exist is our admin user.我们使用 npm 中的firebase.auth()包进行身份验证,其中唯一存在的用户是我们的管理员用户。
What I can't seem to figure out is how to authenticate my POST-request, through the Realtime Database-ruleset.我似乎无法弄清楚如何通过实时数据库规则集验证我的 POST 请求。

I've googled for a while, and found the token I apparantly need to pass with the object, which I find by running firebase.auth().currentUser.getIdToken .我用谷歌搜索了一段时间,发现显然需要通过 object 传递的令牌,我通过运行firebase.auth().currentUser.getIdToken找到了该令牌。 However by having my rule set to但是,通过将我的规则设置为

{
  "rules": {
    ".read": true,
    ".write": "auth.uid != null"
  }
}

...it still doesn't let me post. ...它仍然不让我发帖。 This, of course isn't a very safe authorisation, but it should allow me to post, as I give it an access-token.这当然不是一个非常安全的授权,但它应该允许我发布,因为我给了它一个访问令牌。 (I do this in the request-URL with URL.firebaseio.com/.json?access-token=TOKEN ). (我在请求 URL 中使用URL.firebaseio.com/.json?access-token=TOKEN执行此操作)。

What I am wondering is what part of the system I have misunderstood.我想知道的是我误解了系统的哪一部分。 I do not wish to use the firebase/database -package in npm, as I do not understand it too well, and I'd like to learn one step at the time.我不希望使用 npm 中的 firebase firebase/database包,因为我不太了解它,我当时想学习一步。

If I have to update the rules-file, what should it say?如果我必须更新规则文件,它应该说什么?
If the token/url is wrong, what should it be?如果令牌/网址错误,应该是什么?

Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

I solved it, for my own use:我解决了它,供我自己使用:

In Firebase Console, under the Authentication-tab, I found I could get the UID of a user.在 Firebase 控制台中,在身份验证选项卡下,我发现我可以获得用户的 UID。

By changing the fetch-URL to URL.firebaseio.com/.json?auth=TOKEN) and the write-rule to ".write": "auth.uid == '[UID]'" where [UID] is the previously mentioned UID, it allows post-requests only from users that are logged in through firebase.auth() .通过将 fetch-URL 更改为URL.firebaseio.com/.json?auth=TOKEN)并将写入规则更改为".write": "auth.uid == '[UID]'"其中[UID]是以前的提到 UID,它只允许来自通过firebase.auth()登录的用户的后请求。

Cheers.干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM