我有一个页面正在调用控制器内部的
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.
这就是我想要实现的目标-
转到您应用的 Firebase 控制台
从侧面菜单中选择数据库 --> 从上面的选项卡中选择规则 --> 像这样更新您的规则
{
"rules": {
".read": true,
".write": true
}
}
希望它能解决您的问题。谢谢:)
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
}
}
希望有帮助!
由于所有提供的答案都包含安全问题,每个人都可以在数据库中写入/删除条目,例如,如果使用不当,可能会导致大量成本和/或完全丢失数据。
当然,您需要使用 Firebase 身份验证功能来使用这些规则,但默认情况下应阻止匿名的写入访问。以下规则为每个人提供读取权限,同时保持安全性。
{
"rules": {
".read": true,
".write": "auth.uid != null"
}
}
这些答案都没有提供设置实时数据库的最安全方法。
这条规则应该解决所有情况:
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
请使用以下代码在 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()
为了向所有经过身份验证的用户授予访问权限,请转到firebase Web控制台中的数据库规则选项卡并复制/粘贴以下规则:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
/users/{uid} 路由似乎可以写入,但无法读取。我将 /user 更改为 /userx,它立即开始工作。
我希望这可以帮助你。
{
".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"
结果是一样的。
您还可以指定时间,直到您想要允许为止:
从侧面菜单中选择数据库 --> 从上面的选项卡中选择规则 --> 像这样更新您的规则
{
"rules": {
".read": "now < 1672384350000", // 2022-12-30
".write": "now < 1672384350000", // 2022-12-30
}
}
在我的例子中,这只是在 flutter 中未配置的 firebase 选项,所以我只需手动进行 flutterfire 配置
如果您还没有安装 flutterfire cli,请从此处
进行设置"rules": {".read": "auth != null", ".write": "auth != null"}
如果有人在允许正确的读取权限后仍然得到
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,如此问题中所述。