客户端无权访问 Firebase 中所需的数据

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

我有一个页面正在调用控制器内部的

addCheckin()
方法。在控制器中,我尝试创建一个引用,如下所示:

var ref = firebase.database().ref("users/" + $scope.whichuser + "/meetings/" +$scope.whichmeeting + "/checkins");

$scope.whichuser
$scope.whichmeeting
是我从另一条路线经过的
$routeParams
。 这是我的签到控制器 -

myApp.controller("CheckinsController",
    ['$scope','$rootScope','$firebaseArray','$routeParams','$firebaseObject',
    function($scope,$rootScope,$firebaseArray,$routeParams,$firebaseObject){

        $scope.whichuser = $routeParams.uid;
        $scope.whichmeeting = $routeParams.mid;

        var ref = firebase.database().ref("users/" + $scope.whichuser + "/meetings/" +$scope.whichmeeting + "/checkins");

        $scope.addCheckin = function(){
            var checkinInfo = $firebaseArray(ref);
            var data={
                firstname:$scope.firstname,
                lastname:$scope.lastname,
                email:$scope.email,
                date:firebase.database.ServerValue.TIMESTAMP
            }

            checkinInfo.$add(data);
        }

}]);/*controller*/

我遇到两个错误 -

错误1:

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

错误2:

Error: permission_denied at /users/Vp2P1MqKm7ckXqV2Uy3OzTnn6bB3/meetings/-KT5tqMYKXsFssmcRLm6/checkins: Client doesn't have permission to access the desired data.

这就是我想要实现的目标-

angularjs firebase firebase-realtime-database firebase-authentication angularfire
12个回答
44
投票

转到您应用的 Firebase 控制台

从侧面菜单中选择数据库 --> 从上面的选项卡中选择规则 --> 像这样更新您的规则

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

希望它能解决您的问题。谢谢:)


14
投票

Firebase 项目默认以 Firestore 作为数据库启动。

如果应用程序使用“Firebase 实时数据库”并且未配置其权限,则可能会发生此问题。应明确授予 Firebase 实时数据库的读写权限。

为此,请在 Firebase 控制台中,数据库 > 从数据库 > 规则 > 设置旁边的下拉列表中选择“实时数据库”而不是“Firestore Beta”

{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": true,
".write": true
}
}

希望有帮助!


10
投票

由于所有提供的答案都包含安全问题,每个人都可以在数据库中写入/删除条目,例如,如果使用不当,可能会导致大量成本和/或完全丢失数据。

当然,您需要使用 Firebase 身份验证功能来使用这些规则,但默认情况下应阻止匿名的写入访问。以下规则为每个人提供读取权限,同时保持安全性。

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

6
投票

这些答案都没有提供设置实时数据库的最安全方法。

  • 您不希望任何人随机访问或写入您的数据库
  • 即使用户通过了身份验证,您也不希望他们访问其他人的数据

这条规则应该解决所有情况:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

3
投票

请使用以下代码在 firebase 中注册您自己

Firebase.auth()
            .createUserWithEmailAndPassword(email, password)
            .then((data) => console.log(data))
            .catch(error => console.log(error))

并通过以下代码使用注册电子邮件和密码验证您的身份

Firebase.auth()
           .signInWithEmailAndPassword(email, password)
           .then((data) => console.log(data))
           .catch(error => console.log(error))

在您的实时数据库中使用以下规则

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

然后您将能够自动访问实时数据库中的数据

var ref = firebase.database().ref("users");
ref.on("value", function(snapshot) {
 snapshot.forEach(function(childSnapshot) {
  var childData = childSnapshot.val();
  var id=childData.id;
  console.log(childData);
 });
});

注销时添加以下命令以注销应用程序

Firebase.auth().signOut()

1
投票

为了向所有经过身份验证的用户授予访问权限,请转到firebase Web控制台中的数据库规则选项卡并复制/粘贴以下规则:

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

0
投票

/users/{uid} 路由似乎可以写入,但无法读取。我将 /user 更改为 /userx,它立即开始工作。


0
投票

我希望这可以帮助你。

{
    ".read": true,
    ".write": "auth !== null && root.child('users').child(auth.uid).child('meetings').val() ===  true"
}

您可以删除字符串

 && root.child('users').child(auth.uid).child('meetings').val() ===  true"

结果是一样的。


0
投票

您还可以指定时间,直到您想要允许为止:

从侧面菜单中选择数据库 --> 从上面的选项卡中选择规则 --> 像这样更新您的规则

{
  "rules": {
    ".read": "now < 1672384350000",  // 2022-12-30
    ".write": "now < 1672384350000",  // 2022-12-30
  }
}

0
投票

在我的例子中,这只是在 flutter 中未配置的 firebase 选项,所以我只需手动进行 flutterfire 配置

如果您还没有安装 flutterfire cli,请从此处

进行设置

0
投票

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


-1
投票

如果有人在允许正确的读取权限后仍然得到

Error: permission_denied
并使用某种 firebase npm 包获取数据;这可能是因为您尝试从实时数据库而不是 Cloud Firestore 读取数据。

我使用 react-redux-firebase npm 包来进行快速、简单的设置。默认情况下它使用实时数据库。如果您使用的是 Cloud Firestore,则需要在配置中将

useFirestoreForProfile
设置为
true

import { ReactReduxFirebaseProvider } from 'react-redux-firebase';

const store = configureStore();
const rrfProps = {
  firebase,
  config: { 
    userProfile: 'users', 
    useFirestoreForProfile: true  // Firestore for Profile instead of Realtime DB
  },
  dispatch: store.dispatch,
  createFirestoreInstance,
};

可能与其他 Firebase 软件包存在类似问题,例如支持实时数据库但不支持 Cloud Firestore 的 Flamelink,如此问题中所述。

© www.soinside.com 2019 - 2024. All rights reserved.