【问题标题】:Firebase read/write rules with Flutter not working?使用 Flutter 的 Firebase 读/写规则不起作用?
【发布时间】:2018-06-05 11:28:57
【问题描述】:

我的 Firebase 安全规则的权限有问题。我正在记录以下错误..

[Firebase/Database][I-RDB03812] Listener at /users/4eb8920a-e407-4488-bce4-c6f64f7b0891/Following/4eb8920a-e407-4488-bce4-c6f64f7b0891 failed: permission_denied
[Firebase/Database][I-RDB03812] Listener at /UserVideo/4eb8920a-e407-4488-bce4-c6f64f7b0891 failed: permission_denied
[Firebase/Database][I-RDB03428] Using an unspecified index. Consider adding ".indexOn": "displayName" at /users to your security rules for better performance

这是我的规则结构:

{
  "rules": {
    "$user_id": {".write": true, ".read": true}

  }
}

这是我构建数据的方式...

 UserVideo
     080d4874-47d9-4f4e-b815-9b8dc9c8a2ba
     13566fd4-047b-4d62-b2e5-e885e6667430
     2bd7038f-5490-470e-94eb-87695e4b1071
     2d84a15b-d0aa-4671-9f59-02f9d2ac5207
     3af71559-5c51-40c6-b2aa-8631671c2c25
     3fFH6evUANf3WDbDS3caiQu9crD3
     4eb8920a-e407-4488-bce4-c6f64f7b0891
        Vid1
        Vid10
        Vid2
        Vid3

users
 4eb8920a-e407-4488-bce4-c6f64f7b0891
  FollowedBy
    followedBy: 
      2
 Following
    4eb8920a-e407-4488-bce4-c6f64f7b0891
 displayName: 
   "Charles"
 photo: 
   "placeholder"

我的假设是我的规则将允许用户读取和写入任何可用路径,但我想我错过了一些东西。

【问题讨论】:

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


    【解决方案1】:

    我之前遇到过这个问题。我看起来您没有使用 uid,而是使用另一个 id 实例。从firebase实例获取uid的方法如下:

    final FirebaseAuth auth = FirebaseAuth.instance;
    
    var tempUser = await auth.currentUser();
    currentUser= tempUser.uid;
    

    确保您正在推送到根目录下的 uid 子级。

    reference.child(currentUser).push().set({                                        
      'video': 080d4874-47d9-4f4e-b815-9b8dc9c8a2ba,                                               
    });
    

    最后,像这样在 firebase 中设置你的读写规则。

    {
      "rules": {
        "$user_id": {
          ".write": "auth != null && auth.uid == $user_id",
          ".read": "auth != null && auth.uid == $user_id"
        }
      },
    }
    

    【讨论】:

    • 你说得对,由于某种原因,我没有在整个应用程序中正确地进行身份验证。我查看了我的 SWIFT 代码,FirebaseAuth.instance.currentUser().uid 是我使用的。在 Flutter 中 FirebaseAuth.instance.currentUser() 返回 currentUser 不是函数的错误。关于如何在整个应用中正确进行身份验证的任何想法?
    • 我从未真正使用过 swift,但请查看 Flutter SDK 网站上的此链接。 https://codelabs.developers.google.com/codelabs/flutter-firebase/#5 您应该关注的主要功能是_ensureLoggedIn()。此功能将允许您让用户通过 Google 登录,一旦调用 _ensureLoggedIn(),就可以从我上面的答案中看到获取 UID 的方式
    猜你喜欢
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多