簡體   English   中英

Firebase - 獲取用戶時權限被拒絕

[英]Firebase - permission denied when fetching users

我正在嘗試使用此代碼從Firebase數據庫中獲取用戶,但我收到此錯誤

取消錯誤錯誤Domain = com.firebase Code = 1“Permission Denied”UserInfo = {NSLocalizedDescription = Permission Denied}

我的規則應該如何建立?

這是代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM