无需身份验证即可将数据写入Firebase实时数据库中的字段

问题描述 投票:0回答:1

我正在制作音乐下载的反应应用程序。下载音乐时,我有一个表示下载次数的计数器,我使用firebase事务来更新下载的时间:

  static increaseDownload(id) {  
    firebaseDB.ref("musics/" + id).transaction(musique => {
      if (musique) {
        musique.downloaded++;
      }
      return musique;
    });
  }

enter image description here

我知道根据安全规则,用户必须被认证为wirte / read to db,但我不想注册和登录用户,而且我也不想放“.write”:true为我数据库中的每个数据。我希望用户能够只是去网站下载。

我的问题是,如何让“已下载”的计数器增加,允许未经验证的用户只能写入“已下载”字段?我希望非身份验证的用户只能写入“已下载”字段,其他所有字段,并且无需身份验证即可无法访问数据。

javascript firebase firebase-realtime-database firebase-authentication firebase-security-rules
1个回答
0
投票

[编辑]您可以允许仅在数据库中的其他信息保持私有且不可访问的情况下访问“已下载”的“音乐”节点的子节点。添加这个数据库规则

 
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
   "rules": {
      ".read": "false",
        ".write": "false",
    
     "musics":{
     "downloaded":{
       ".read": true,
         ".write": true
       }   
     } 
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.