Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

346
Views
firebase realtime database, how to read only own data?

I have a realtime database in my nextjs webapp and I use it for notifications.

The schema of my db is the following:

enter image description here

I write data only from server side as admin, but read it from clientside in the following way:

  const fetchNotifications = () => {
    const starCountRef = ref(db, `notifications/${user?.id}`);
    const q = query(starCountRef, orderByChild('createdAt'), limitToLast(100));
    onValue(q, (snapshot) => {
      //Handle data
    });
  };

In this moment I have the following rules:

{
  "rules": {
    ".read": "auth != null",
    ".write": false,
    "notifications":{
      "$user_id":{".indexOn":"createdAt"}
    }
  }
}

As you can see everyone that is authenticated can read everything. I want the user to read only his own notifications. The ObjectID is a reference from my mongoDB.

For example if I have ID = 1 I want to access data only for path /notifications/1 and if I try to get data from /notifications/2 then I get nothing (or error).

First of all how can I see from firebase the ID I use in my local DB? Have I to let him know first? Second how can I write it in my rules?

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

If the keys directly under notifications are the Firebase Authentication UIDs (as your rules suggest), you can limit access to only the user's node with:

{
  "rules": {
    //".read": "auth != null", // ๐Ÿ‘ˆ Remove this line
    ".write": false,
    "notifications":{
      "$user_id":{
        ".read": "auth.uid === $user_id", // ๐Ÿ‘ˆ add this line
        ".indexOn":"createdAt"
      }
    }
  }
}

Also see the Firebase documentation on securing content-owner only access.

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!