更改 Firebase 中的规则后 Firebase 权限被拒绝错误

时间:2021-06-09 11:30:28

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

这是我更新的 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);

1 个答案:

答案 0 :(得分:0)

根据 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

相关问题