Reputation: 457
I'm trying to update records in Firebase Realtime database using React Native via API.
During development I had Read/Write rules opened to everyone and used to update specific items using the following code
const response = await fetch(
`https://mydatabase.firebaseio.com/myTable/recordID/basic.json`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
field1: newValue1,
field2: newValue2,
field3: newValue3,
}),
}
);
Now I have restricted the "write" rule to a specific user that I have created. Basically only this user (the admin) can write into firebase
".write": "auth.uid === <User UID>"
I've been trying to pass the authenticated user details at the end of the json like this
https://mydatabase.firebaseio.com/myTable/recordID/basic.json?auth.uid === <User UID>
but it doesn't seem to work.
The documentation shows options to add ID Token or Google OAuth2.
Any suggestion would be very much appreciated
Upvotes: 0
Views: 51
Reputation: 598728
A user's UID is not enough to authenticate them with, as that would be a huge security risk. UIDs are instead user identifiers that you can share to identify a user. For more on this see: Firebase - Is auth.uid a shared secret?
You'll need to pass in the ID token of the user to authenticate them, which you can get from using the JavaScript SDK, or from the REST API.
Upvotes: 2