permission_denied at/:客户端无权访问所需数据 [英] permission_denied at /: Client doesn't have permission to access the desired data

查看:28
本文介绍了permission_denied at/:客户端无权访问所需数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 firebase,当我尝试检索数据时,我无法让它工作.

This is my first time using firebase, and when I try to retrieve data I can't get it to work.

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

I've implemented the firebase's authorisation and used it and can now see the user's data from any controller (Check controller below), but when I try to retrieve data from the database I get this error:

permission_denied at/:客户端无权访问所需数据.

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

My database rules regarding the feed are:

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

如果我将它们公之于众,它就可以工作并获得数据,但我不想这样做,而且我无法在线找到任何有关如何发送用户 ID 以从 firebase 获取数据的文档.

If I make them public it works and I get data, but I don't want to do that, and I can't find any documentation online about how do I send the user id to get the data from 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);
}]);

感谢任何帮助,谢谢.

推荐答案

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

It looks like the user isn't authenticated when you attach the listener. Logging the user like you do is not a good indicator for this, since the dev tools of your browser will show you the updated value of currentAuth as it changes. If you instead log console.info('uid',currentAuth.uid) you will likely see that it's null.

您应该只在用户通过身份验证后附加您的侦听器.来自 AngularFire 文档:

You should only attach your listeners once the user is authenticated. From the 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");
  }
});

这篇关于permission_denied at/:客户端无权访问所需数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆