Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

357
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda