I'm trying to know if the player is already in a game or not but I'm completely new to the database programming. I keep getting this error
2022-02-07 17:32:51.787097+0100 Troeven2.0[64029:2674017] 7.10.0 - [Firebase/Database][I-RDB038012] Listener at / (and here the correct! user id of the current user) failed: permission_denied
update** and the error handler returning:
Error Domain=com.firebase Code=1 "Permission Denied" UserInfo={NSLocalizedDescription=Permission Denied}
**
I find this rather weird as the user has full rights how I see it to write and read his own data, rules:
{
"rules": {
"users": {
".read": "auth.uid != null",
".write": "auth.uid != null",
"$uid": {
".write": "$uid == auth.uid"
}
}
}
}
my data structure is as follows:
and my code that I use to acces is this:
func checkIfInGame(completionHandler: @escaping (_ completionHandler: Bool) -> Void) {
let currentUserID = Auth.auth().currentUser?.uid
let ref = Database.database().reference().child(currentUserID!).child("gameInfo")
ref.observe(.value , with: { (snapshot) in
if let directory = snapshot.value as? [String: Any] {
let gameID = directory["gameID"] as! String
let isInGame = directory["inGame"] as! String
var flag = false
if isInGame == "yes" {
flag = true
}
completionHandler(flag)
}
}) { (error) in
print("error checking in game")
completionHandler(false)
}
}
it's also maybe handy to know that my code immediately prints the error "error checking in game" any help and time is really appreciated!
Silly mistake that wasted my day: I did not go into child("users") hence no permission was granted. I also switched to getData since I only needed the data once when asked....
func checkIfInGame(completionHandler: @escaping (_ completionHandler: Bool) -> Void) {
guard let currentUserID = currentUser?.uid else {return}
let ref = Database.database().reference().child("users").child(currentUserID)
ref.child("gameInfo/inGame").getData(completion: { error, snapshot in
guard error == nil else {
print(error!.localizedDescription)
return;
}
let test = snapshot.value as? String ?? "no";
print(test)
})
}