firebase - 使用 auth.uid 限制对 Firebase-Database 的访问不起作用
问题描述
我想限制对我的 Firebase 数据库的访问,以便只有我授权的用户才能对其进行写入。但是几乎所有地方提出的解决方案似乎都对我不起作用。
我的控制台中总是出现 401(未经授权)错误。
我尝试了两种检查正确用户是否登录的方法,但它们都不适合我:
1. uid 硬编码在规则中:
{
"rules": {
".read": true,
".write": "auth.uid === 'UID'")",
}
}
2. 数据库中的uid
{
"rules": {
".read": true,
".write": "root.child('users').hasChild(auth.uid)",
}
}
在这两种方式中,我都使用了 Firebase-Authentication 概述中提供的 uid。我使用 Google 作为登录提供商。
解决方案
从文档中:
下面是一个规则示例,该规则授予经过身份验证的用户对 的写入权限
/users/<uid>/
,其中<uid>
是通过 Firebase 身份验证获得的用户 ID。
编辑:
对于通过 Firebase 身份验证的特定路径和当前获得的用户,这应该会有所帮助:
{
"rules": {
"YourSpecificPath": {
"$uid": { // where <uid> is the ID of the user obtained through Firebase Authentication
".write": "$uid === auth.uid"
".read": true,
}
}
}
}
或者,uid
直接给出:
{
"rules": {
".read": true,
".write": "auth.uid === 'dJrGShfgfd2'"
}
}
推荐阅读
- xml - 捕获不同的 Sharepoint GUID
- ruby-on-rails - 为什么 ActiveRecord 对象返回 DateTime.now 与 DateTime.now 不同
- unit-testing - 有没有办法编译在 gcc 上使用 -Weffc++ 标志编译时使用模拟的谷歌测试
- postgresql - 用于向 Postgres DB 批量插入的 Azure 持久功能
- google-schemas - 电子邮件标记在被 Google 列入白名单后不起作用(已经 17 天)
- python - 将 Selenium exe(用 Python 编码)转换回 Python 源代码的方法?
- python - 可以在 python 中使用 matplot lib 在两行中创建 3 个图形吗?
- java - java.lang.UnsatisfiedLinkError: Test.sayHello()[Ljava/util/Properties;
- cytoscape.js - Cytoscape-popper 和 Tippy:交互内容失败
- python - 获取numpy数组元素的所有可能组合