In Firebase, a user does not have permission to read or write documents within its own Firestore database

Currently, I have a user who is logged in with a Google account, and their user id in Firebase is '123'.

In Firestore data, there is a collection named 'users' with a single document labeled '123' containing the field 'name: "test"'

The Firestore rules are set as follows:

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, update, write, delete: if request.auth.uid == userId;
    }
  }
}

In my component, the code snippet looks like this:

  users: Observable<User[]>;

  ngOnInit()
  {
    this.users = this.firestore.collection<User>('users'); 
  }  

  constructor(public auth: AngularFireAuth, private firestore : AngularFirestore) {}

HTML:

<ul>
   <li *ngFor='user of angObs | async>
    {{ user.name }}
   <li>
<ul>
<div *ngIf='auth.user | async as user'>
    <p>User id:</p>
    {{ user.uid }}
</div>

The HTML displays:

User id:

123

When the rule is request.auth.uid != null;, the user can see the data.

On the other hand, when the rule is request.auth.uid == userId;, the user cannot view the data.

What could be going wrong here?

Answer №1

Your method of querying the entire user collection will not work for this test because your userId would have to match every document id.

To successfully pass the test, you must specify the path to the document like this:

this.users = this.firestore.collection('users').doc<User>(userId).get()

This will only return a single User document and not an array of Users.

If you are attempting to query the entire user collection as indicated by your code snippet:

  users: Observable<User[]>;

You will need to add a custom property to each user object that determines their privileges.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to get spacing correct on loading page

My attempt at creating a loading page using CSS and HTML has hit a roadblock. I'm trying to show a loading bar that ranges from 0% to 100%. Despite my use of justify-content: space-between, I can't seem to get it right. I've searched through ...

Tips on extracting data from an API using jQuery

Struggling with parsing data and feeling a bit stuck. That's why I'm reaching out for assistance. This is where I need help: How do I specifically retrieve market_cap values? Each value needs to be accessed individually for a specific string I ...

Validation in AngularJS is limited to only accepting integers with the use of the

Can you help me with validating positive integer numbers using the ng-pattern attribute? Currently, I have this pattern: ^[0-9]{1,7}(\.[0-9]+)?$/, but it also allows decimal values. I want to restrict it to only accept whole numbers. ...

Is there a way to execute two files concurrently in JavaScript using node.js?

I'm a beginner in the world of Javascript and Node.js, and I've encountered some issues while trying to test code I recently wrote. Specifically, I am attempting to test the code within a file named "compareCrowe.js" using another file named "tes ...

Error occurs when attempting to call JavaScript from the server-side code

When utilizing Page.ClientScript.RegisterStartupScript to invoke a JavaScript function from the code behind, everything works smoothly for simple functions in JavaScript. However, issues arise when the JavaScript function includes an object. For instance: ...

Utilizing VueJS and Lodash: The technique for extracting an array of objects exclusively featuring a specific key string

I am attempting to extract certain data from an Object that has a string _new attached to it. Explore the code on Codesandbox: https://codesandbox.io/s/vibrant-bardeen-77so1u?file=/src/components/Lodash.vue:0-353 This is what my data looks like: data.j ...

Utilizing Jquery for ASP.NET, an AJAX call dynamically populates a list

My user interface is designed to populate a select dropdown menu using data retrieved from a database through an AJAX call. The C# web method responsible for this operation is structured as follows: private static List<List<string>> componentT ...

Breaking down a multidimensional array into individual arrays using Javascript

Having a serious problem here, I have an array structured like this: [[0,50],[0,68],[1,26],[2,9],[2,32]] I am looking to split this array into two separate arrays like so: array1 = [[0,50][1,0][2,9]] array2 = [[0,68][1,26][2,32]] Yes, you are correct g ...

JavaScript Evaluation Error

try { eval(somejavascript); } catch(e) { console.log(e); } When I encounter runtime errors like: TypeError: Cannot call method 'leftPad' of undefined I wonder if there is any way to debug this error. Specifically, I'm looking for ...

Showing a div element when the submit button is clicked

I am working on a form with a radio button and a submit button. When the submit button is clicked, I want to show a hidden div. Below is my current code: <form action="" method="GET" name="myform"> <input type="radio" name="ID" value="total" ...

Enhance your AJAX calls with jQuery by confidently specifying the data type of successful responses using TypeScript

In our development process, we implement TypeScript for type hinting in our JavaScript code. Type hinting is utilized for Ajax calls as well to define the response data format within the success callback. This exemplifies how it could be structured: inter ...

Strive to discover the ideal solution for capturing a screenshot of an OpenLayers map using html2canvas. Currently, the map elements are losing their CSS classes and images are not

Seeking advice on the best solution for working with html2canvas and css. I'm trying to take a screenshot of a map that includes various elements, but after capturing the image, all the css classes are lost and the images are not rendered properly. S ...

jQuery scripts were not loaded, resulting in an uncaught type error

Hey there! I am currently using jQuery Tablesorter to handle pagination on my website, but it seems to be throwing an error in the browser console. The specific error message reads: Uncaught TypeError: undefined is not a function viewTags:24 (anonymous fu ...

The callback function is triggered prior to the completion of the request.on function

A custom npm module was developed for downloading files and displaying progress bars using the request and progress libraries. https://github.com/MaxySpark/maxyspark-download However, during testing, the callback function is executing prematurely instead ...

Contrast between Server-side and Backend operations

Can you shed some light on the disparity between Server-side and Backend? As a coding novice looking to establish a website, I intend to leverage Node.js for the backend. However, I'm a bit confused about what server-side signifies. Can you provide a ...

How can I convert a string containing only the time in the format HH:MM to a Date object in Javascript?

I've encountered an issue with a business hours object in a mongoose schema that is meant to represent a date. When passing a JSON object and attempting to parse it into a Date as a string, I receive an error message stating: validation failed: busine ...

Error message: Attempting to access the 'props' property of an undefined variable

Trying to establish communication between a child and parent component using props has presented a slight issue. An error is encountered when attempting to utilize the StatusChanged() function. The parent file (App.JS) looks like this: class App extends ...

What is the best way to arrange the elements of a dropdown list in reverse order?

I have the following code snippet used to retrieve data from a database and display it in a dropdown list: @{ int m = 1; foreach (var item in Model.MessagesList) { if (@item.receiverUserId == userId) { <li> <a class=&qu ...

Creating materials in Three.js from Blender source files

After exporting a simple white material with my geometry from Blender, I noticed that the Three.js loader somehow created a MeshPhongMaterial "type" object from the source JSON file: ... "materials":[{ "colorEmissive":[0,0,0], "c ...

Regular expressions should be of a specific length and include both letters and numbers

I am trying to create a regular expression that can identify substrings of length 10-15 containing only [A-Z0-9], with at least one letter and one number (spaces are permissible but not other special characters). Here are some examples: ABABABABAB12345 sh ...