【问题标题】:Firebase permission-denied error after changing rule in firebase更改 Firebase 中的规则后,Firebase 权限被拒绝错误
【发布时间】:2021-08-26 09:45:21
【问题描述】:

这是我更新的 firebase 数据库规则。使用静态 uid 更新此规则后,现在错误显示 Firebase 权限被拒绝。

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

这是我的配置代码,现在我应该在代码中进行哪些更改,以便获得从数据库读取的权限。

const config = {
    apiKey: 'xxxxxxxxxx',
    databaseURL: "https://db_name.firebaseio.com/",
    storageBucket: "db_name.appspot.com",
    authDomain: "db_name.firebaseapp.com",
    projectId: "xxxxxxx",
    messagingSenderId: "xxxxxxxx",
    appId: "xxxxx"
};
firebase.initializeApp(config);

【问题讨论】:

  • 安全规则自己不会做任何事情。请编辑您的问题以包含生成此错误消息的最少代码。
  • 这就是问题所在,JavaScript 中允许我访问数据库中的数据的代码是什么。
  • 您的标题显示您收到了Firebase permission-denied error after changing rule in firebase,我想您可能没有想到。请显示意外引发该错误的代码。
  • 在此规则更改后,我没有添加任何相关代码来连接 firebase。这就是为什么会出现这个错误。所以我要求该代码或任何将连接到 Firebase 数据库的 JavaScript 函数..
  • 您的应用在哪里或如何向 Firebase 进行身份验证?你能包括那个代码吗?此外,auth.uid=='xxxxstatic_idxxxx'", 的目的是什么 - 您是否试图限制对经过身份验证的用户或其他用户的访问?例如硬编码的 uid 可能不是一个好的长期情况。

标签: javascript firebase firebase-realtime-database firebase-security


【解决方案1】:

根据documentation,你应该匹配这个模式:

{
  "rules": {
    "users": {
      "$userId": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($userId)
        ".write": "$userId === auth.uid"
      }
    }
  }
}

我了解您只想向一个特定用户授予权限,那么 $userId 应替换为 xxxxstatic_idxxxx

【讨论】:

  • 配置代码呢?需要从配置文件中传递这个 uid,然后它才会匹配并获得读取权限。如何从配置文件中传递它?
猜你喜欢
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 2016-09-25
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多