【问题标题】:permission_denied at /: Client doesn't have permission to access the desired datapermission_denied at /:客户端无权访问所需数据
【发布时间】:2017-03-07 04:05:05
【问题描述】:

这是我第一次使用 firebase,当我尝试检索数据时却无法正常工作。

我已经实现了 firebase 的授权并使用了它,现在可以从任何控制器(检查下面的控制器)查看用户的数据,但是当我尝试从数据库中检索数据时出现此错误:

permission_denied at /: Client doesn't have permission to access the desired data.

我关于提要的数据库规则是:

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

如果我将它们公开,它会起作用并且我会获取数据,但我不想这样做,而且我在网上找不到任何关于如何发送用户 ID 以从 firebase 获取数据的文档。

我的控制器:

.controller('homeCtrl', ['$scope','$firebaseObject','currentAuth',
    function($scope,$firebaseObject,currentAuth) {

        console.info('currentAuth',currentAuth); // This returns the needed info about the current user including the uid

        // This is where I get the permission denied error.
        var ref = firebase.database().ref();
        // download the data into a local object
        $scope.data = $firebaseObject(ref);
}]);

感谢您的帮助。

【问题讨论】:

    标签: angularjs firebase angularfire


    【解决方案1】:

    在您附加侦听器时,用户似乎未经过身份验证。像你这样记录用户并不是一个很好的指标,因为浏览器的开发工具会在 currentAuth 发生变化时向你显示它的更新值。如果您改为登录 console.info('uid',currentAuth.uid),您可能会看到它是 null

    您应该仅在用户通过身份验证后附加您的侦听器。来自AngularFire documentation

    $scope.authObj.$onAuthStateChanged(function(firebaseUser) {
      if (firebaseUser) {
        console.log("Signed in as:", firebaseUser.uid);
        var ref = firebase.database().ref();
        $scope.data = $firebaseObject(ref);
      } else {
        console.log("Signed out");
      }
    });
    

    【讨论】:

    • 感谢弗兰克,当我打印出 currentAuth.uid 时,它不为空,它为我提供了用户的价值,并且在错误出现之前出现在日志中。我现在得到的 uid 的值是 wJMkHNMDcmMz2syU9gakwYevC7J2
    • 在发布此问题之前,我还从文档中添加了 onAuthStateChanged,并尝试在此处添加 firebaseObject,但仍然遇到相同的错误。
    • 嗯....我不确定是什么导致了错误。但这很明确:当您无权访问数据时,您正在尝试读取/写入数据。我已经很久没有看到这个消息是错误的了。你能在 jsbin 中重现它吗?
    • 弗兰克我试试看。
    • 弗兰克,我尝试了另一种方法并遇到了同样的问题,你能看看这篇文章吗? stackoverflow.com/questions/42690615/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2021-08-10
    • 2020-11-03
    • 2019-03-20
    • 2017-02-12
    • 2022-01-15
    相关资源
    最近更新 更多