Firebase-提取用户时权限被拒绝 [英] Firebase - permission denied when fetching users
问题描述
我正在尝试使用此代码从Firebase数据库中获取用户,但出现此错误
I'm trying to fetch users from a Firebase database with this code but I get this error
取消错误错误Domain = com.firebase代码= 1权限被拒绝" UserInfo = {NSLocalizedDescription =权限被拒绝}
cancel error Error Domain=com.firebase Code=1 "Permission Denied" UserInfo={NSLocalizedDescription=Permission Denied}
应该如何设置我的规则?
How should my rules be set up?
代码如下:
FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
print("snapshot \(snapshot)")
//all users right here n shyt
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
//class properties have to match up with firebase dictionary names
user.setValuesForKeys(dictionary)
self.users.append(user)
DispatchQueue.main.async {
self.messageTable.reloadData()
}
}
print(snapshot)
}, withCancel: { (error) in
print("cancel error \(error)")
})
这是我在Firebase中的规则:
This is my rules in Firebase:
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
推荐答案
鉴于您当前的安全规则,您只授予当前用户访问其自己节点的权限.
Given your current Security rules you are only giving permission to your current user to access only its own node.
如果要制作动态图片,请尝试制作另一个父节点,其中包含您想与其他用户共享的详细信息.
If thats the dynamic you want to go by try making another parent node which contains the details that you would wanna share with other users.
users:{
userID1 : {../*PERSONAL DETAILS*/},
userID2 : {../*PERSONAL DETAILS*/},
userID3 : {../*PERSONAL DETAILS*/},
userID4 : {../*PERSONAL DETAILS*/},
userID5 : {../*PERSONAL DETAILS*/},
....
},
USERS_INFO: {
userID1 : {../*Details to share*/},
userID2 : {../*Details to share*/},
userID3 : {../*Details to share*/},
userID4 : {../*Details to share*/},
userID5 : {../*Details to share*/},
....
}
并将您的安全规则更新为:-
And update your security rules to:-
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
},
"USERS_INFO":{
".read" : "auth != null",
".write" : "auth != null"
}
}
}
查询类似:-
FIRDatabase.database().reference().child("USERS_INFO").observe(.childAdded, with: { (snapshot) in
这篇关于Firebase-提取用户时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!