【问题标题】:Firebase auth != null I can write but can't readFirebase auth != null 我可以写但不能读
【发布时间】:2018-02-12 07:07:19
【问题描述】:

我已经在 Firebase 中处理了两天的授权,但我无法让它工作。大量阅读文档和 stackoverflow 中的内容,但仍然不明白。这就是我的规则的样子...

{
  "rules": {
    ".read": false,
    ".write": false,
      "notes": {
        ".read": "auth != null",
        ".write": "auth != null",
        ".indexOn": "uid"
      }
    }
  }

我的数据看起来像这样......

user-notes-very-upper-root
|_notes
  |_-ugly_id_for_this_record
  | |--date: "Sat Sep 02 2017"
  | |--note: "new note from me"
  | |__uid: "firebase_ugly_id"
  |_-ugly_id_for_this_record

就像我之前说的,我会写但我不会读。我正在为此使用 angularjs 1.6.6。我在.run 部分有一个firebase onAuthStateChange 来监听身份验证更改,并且我可以在应用程序周围移动用户obj。我的角度代码看起来像这样......

angular.module('userNotes').run(function($rootScope, $window) {
  $rootScope.currentUser = null;
  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      $rootScope.currentUser = user.uid;
      console.log('User logged in from Run: ', user);
    } else {
      $rootScope.currentUser = null;
      console.warn('User logged out from Run: ', user);
      $window.location.href = '#!/';
    }
  });
});

如果我将rules 节点之后的.read.write 规则更改为true,我可以毫无问题地读取数据,但我不想那样做。我只想读取经过身份验证的用户的数据。

我如何调整规则以使经过身份验证的用户能够读取和写入数据。

2017 年 9 月 4 日更新

我把规则改成……

{
  "rules": {
    "notes": {
      ".read": "auth != null",
      ".write": "auth != null",
      ".indexOn": "uid"
    }
  }
}

这些规则不能解决问题。我会写但不会读。这是控制台中的错误...

Possibly unhandled rejection: {"data":{"error":"Permission denied"},"status":401

【问题讨论】:

    标签: angularjs authentication firebase rules


    【解决方案1】:

    来自Understanding Firebase Realtime Database Rules 文档(重点是我的),

    .read.write 规则级联,因此此规则集授予对路径 /foo/ 以及任何更深路径(例如 /foo/bar/baz)的任何数据的读取访问权限。 请注意,数据库中较浅的 .read.write 规则会覆盖较深的规则,因此在此示例中仍将授予对 /foo/bar/baz 的读取访问权限,即使路径 /foo/bar/baz 上的规则已评估为假。

    您无法阅读,因为您有".read": false,它覆盖了"notes" 中的.read 规则。这并不能解释为什么你仍然可以在不应该写作的时候写作,因为".write": false

    无论如何,请尝试删除".read": false".write": false,这样它就不会覆盖"notes" 下的规则。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多