Emil David Emil David - 3 months ago 30
Swift Question

Firebase Database - Check for Existing User

I dummy users added to my Firebase DB and for testing purposes I am trying to see if I can check for a user in the DB.

I first wrote the

createUserToDatabase()
method which works perfectly. For existence querying purposes, I wrote the
doesUserExist()
method but after running it multiple times, no
snapshot
is returned. The method is called but the actual Firebase query is not performed.

Then I decided to make a simple query for all the data, hence
test()
. To my surprise, this also doesn't work even though I have used this similar querying approach on another app and it works just fine. None of the three approaches in
test()
print anything.

import UIKit
import Firebase
import FirebaseDatabase

struct FirebaseNode
{
static let USERS: String = "Users"
}

class MainViewVC: UIViewController
{
static var userName: String?
var databaseRootRef: FIRDatabaseReference!
let currentUser: User = User.sharedInstance

@IBOutlet weak var userNameLabel: UILabel!

override func viewDidLoad()
{
super.viewDidLoad()

databaseRootRef = FIRDatabase.database().reference()

if let userTitle = MainViewVC.userName
{
userNameLabel.text = userTitle
}
else
{
userNameLabel.text = ""
}
}

override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}

@IBAction func createButtonAction(sender: UIButton)
{
test()
}

func doesUserExist()
{
databaseRootRef.child(FirebaseNode.USERS).queryEqualToValue(currentUser.uid).observeSingleEventOfType(.Value) { (snapshot: FIRDataSnapshot) in
if snapshot.exists()
{
print("User exists")
}
else
{
print("User doesn't exist")
}
}
}

func test()
{
databaseRootRef.observeSingleEventOfType(FIRDataEventType.ChildAdded) { (snapshot: FIRDataSnapshot) in
print(snapshot)
}

databaseRootRef.observeEventType(.ChildAdded) { (snapshot: FIRDataSnapshot) in
print(snapshot)
}

databaseRootRef.observeSingleEventOfType(.Value) { (snapshot: FIRDataSnapshot) in

print(snapshot)
}
}

func createUserToDatabase()
{
databaseRootRef.child(FirebaseNode.USERS).child(self.currentUser.uid).setValue(self.currentUser.toAnyObject())
}
}


This is the structure of my Firebase DB

{
"Users":
{
"JglJnGDXcqLq6m844pZ": ---> $uid
{
"userName":"Hello John",
"firstName": "Johnita",
"lastName":"Hernandez",
"email":"help@mail.fabolous",
"profileImage": "%uid%.jpeg",
}
}
}


And this is my current set of rules

{
"rules": {
"Users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid",
".validate": "newData.hasChildren(['email', 'firstName', 'lastName', 'userName'])"
}
}
}
}


No error is thrown whatsoever or any reported auth problems.

Any help will be greatly appreciated.

Answer

I set FIRDatabase.setLoggingEnabled(true) inside viewDidLoad which helped log all the occurring events as the query was made. At the end of it, the database returned the following JSON

[FirebaseDatabase] Got data message: {
    b =     {
        d = "Permission denied"; |-----> Root of ALL EVIL :)
        s = "permission_denied"; |
    };
    r = 4;
}