Firebase - 获取用户时权限被拒绝

2024-02-19

我正在尝试使用此代码从 Firebase 数据库中获取用户,但出现此错误

取消错误错误域=com.firebase代码=1“权限被拒绝”UserInfo={NSLocalizedDescription=权限被拒绝}

我的规则应该如何设置?

这是代码:

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 中的规则:

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

根据您当前的安全规则,您仅授予当前用户访问其自己的节点的权限。

如果这就是您想要的动态,请尝试创建另一个父节点,其中包含您想要与其他用户共享的详细信息。

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*/},
  ....
  }

并将您的安全规则更新为:-

 {
 "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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Firebase - 获取用户时权限被拒绝 的相关文章

随机推荐