How do I allow a user to only fetch their own data in Firebase?

I've already implemented Firebase in my app. I'm able to press a button and send the desired data to Firebase. I also set up a table view, so that the desired data is then added to the table view. However, when any user presses the butting to send data and update the table, all of the data is pulled, even from other users. It seems the entire database is being pulled, rather than the one specific to the user. I want a user to be able to get the hand drying options that they entered/saved based on their uid, not those of other users. I'm very new to all of this so any help would be very much appreciated. Here is the code:

import UIKit
import Firebase
import FirebaseDatabase

class HandDrySaveViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var refHandrylocations: DatabaseReference!
    
    @IBOutlet weak var BestOption: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(didGetNotification4(_:)), name: Notification.Name("text4"), object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(didGetNotification5(_:)), name: Notification.Name("text5"), object: nil)
        
        if FirebaseApp.app() == nil {
            FirebaseApp.configure()
        }
        
        refHandrylocations = Database.database().reference().child("users");
        
        refHandrylocations.observe(DataEventType.value, with:{(snapshot) in
            if snapshot.childrenCount>0{
                self.handDryList.removeAll()
                
                for handDryOptions in snapshot.children.allObjects as![DataSnapshot]{
                    let handDryObject = handDryOptions.value as? [String: AnyObject]
                    let handDryLocation = handDryObject?["handrylocation"]
                    let handDryBest = handDryObject?["handdrybest"]
                    let handDryString = handDryObject?["handdryoption"]
                    let handDryId = handDryObject?["id"]
                    
                    let handDry = HandDryModel(id: handDryId as! String?, location: handDryLocation as! String?, best: handDryBest as! String?, options: handDryString as! String?)
                    
                    self.handDryList.append(handDry)
                }
                
                self.handDryTable.reloadData()
            }
        })
        // Do any additional setup after loading the view.
    }
    
    @IBAction func BacktoHDCalcButton(_ sender: UIButton) {
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(identifier: "TowelVDryer")
        vc.modalPresentationStyle = .overFullScreen
        present(vc, animated: true)
    }
    
    func addHandDryLocation(){
        let key = refHandrylocations.childByAutoId().key
        
        let handDryLocations = ["id":key,
                                "handrylocation": HandDryLocation.text! as String,
                                "handdrybest" : BestOption.text! as String,
                                "handdryoption" : HandDryOptionsString.text! as String
                                    ]
        refHandrylocations.child(key!).setValue(handDryLocations)
        
        OptionSavedMessage.text = "Location Saved"        
    }    
    
    @IBOutlet weak var HandDryOptionsString: UILabel!
    @IBOutlet weak var HandDryLocation: UITextField!
    
    @IBOutlet weak var OptionSavedMessage: UILabel!
    @IBAction func BackButton(_ sender: UIButton) {
    }
    
    
    @objc func didGetNotification4(_ notification: Notification){
        let text = notification.object as! String?
        HandDryOptionsString.text = text
    }
    
    @objc func didGetNotification5(_ notification: Notification){
        let text = notification.object as! String?
        BestOption.text = text
    }
    
    @IBAction func SaveHandDryButton(_ sender: UIButton) {
        addHandDryLocation()
    }
    
    @IBOutlet weak var handDryTable: UITableView!
    
    var handDryList = [HandDryModel]()
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return handDryList.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HandDrySavedTableViewCell
        
        let handDry: HandDryModel
        
        handDry = handDryList[indexPath.row]
        
        cell.locationOfOption.text = handDry.location
        cell.bestOption.text = handDry.best
        cell.optionString.text = handDry.options
        
        return cell
    }
}

Hi Logan,
This forum is mainly about the swift language itself, and your question is very Firebase-specific, so it may be better to in another forum or on StackOverflow.

The Firebase documentation also has a section about structuring your data.

I’ll still give a shot at answering your question:

If you have some data that is completely separate for each user, you could use the user’s uid as part of the path to their data - and create a security rule that only allows those paths to be accessed if the uid component of the path matches the authenticated user’s uid.

Terms of Service

Privacy Policy

Cookie Policy