Firebase Storage rules to lookup data on Firestore document? [duplicate] - firebase

This question already has answers here:
Can Firebase Cloud Storage rules validate against Firestore data?
(3 answers)
Closed 2 years ago.
Is it possible to have a Firebase Storage rule that is similar to the get() function of Firestore?
Basically I would like to check the user document in Firestore to allow writes in Storage.
I guess working with claims would be the best solution, but I'm not sure I can use that.
I would like something like this:
allow create: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.canDoSomeStuff == true;

It's currently not possible for security rules for any Firebase product to reach outside of their own product. Feel free to file a feature request for that.

Related

Does firestore save the change history on the documents? [duplicate]

This question already has answers here:
Firestore: Version history of documents
(3 answers)
Firebase Firestore Documents changes history (like Activity log/ History for changes in each Doc)
(3 answers)
Closed 1 year ago.
I had an issue, where I accidentally deleted some data saved on a property of a document of FireStore, and I'm trying to get back the data.
If FireStore saves the change history, it would be easy to retrieve the data, but I don't see if they do.
Thanks
Firestore does not store any sort of change history, you can create this behavior yourself using Cloud Functions with an Update trigger
The only downside is that Cloud Functions does require a Billing account attached and the project upgraded to Blaze Plan.
if that is not an option for you, you will have to create new documents for each revision made to the previous document and update the relative paths to the latest document

Get list of all the collections inside the doc Firestore [duplicate]

This question already has answers here:
How to list subcollections in a Cloud Firestore document
(4 answers)
Closed 2 years ago.
I am looking towards listing all the collections under doc of firestore. Is there any way to achieve this?
listCollections ad other stuffs are not working.
It is not possible, with the Client SDKs to list the sub collections of a Firestore document (nor the root collections of the Firestore database).
On the other hand, this is possible with the Admin SDKs. You may be interested by this article which shows how to use the listCollections() method of the Admin Node.js SDK in a Cloud Function in order to get the sub collections of a Firestore document.

How to create a firebase cloud function that could trigger when database is read [duplicate]

This question already has an answer here:
how to trigger firebase cloud function on read event
(1 answer)
Closed 3 years ago.
Is it possible to have a firebase cloud function that could trigger when sone one read the realtime database.
As per this document https://firebase.google.com/docs/functions/database-events , We can have cloud function to trigger when data is created, updated, or deleted on Realtime Database. But i want to do some external network call if some one is reading the data.
Right now there is no OnRead trigger or a way to achieve this behavior, however Here is a feature request on Google to support this in the future.
I recommend you to follow up over there to see any progress made on this.

Firestore security rules to count documents [duplicate]

This question already has answers here:
How do you force a Firestore client app to maintain a correct document count for a collection?
(2 answers)
Closed 3 years ago.
I need to limit the number of documents a user can have in a collection.
I expect that having a limit of let's say 100 documents when a user tries to create the document 101 gets an error.
Is there a way of doing this using firestore security rules ?
Security rules don't have the capability to count the number of documents in a collection. In fact, counting documents in Firestore is, in general, is kind of a difficult problem that typically requires some support from a product like Cloud Functions.
If you want to get something like this to work, you will have to write some Firestore triggers in Cloud Functions that manages the count of documents by triggering some code when a document is created or deleted. This count would have to be stored in another document in another collection. Then, the contents of that document could be used in security rules to limit client access.

Firestore sub collection querying data [duplicate]

This question already has answers here:
How to list subcollections in a Cloud Firestore document
(4 answers)
Closed 5 years ago.
I am reading the docs and see that FireStore allows sub-collection. Which is great. Consider the following example as mentioned here int he docs.
As shown in the docs I can get the reference to the last doc as follows
var messageRef = db.collection('rooms').doc('roomA')
.collection('messages').doc('message1');
In the above example, the id's for docs and collections are typed in.
There are cases when id's are dynamically generated. In such a case how can I know how many collections a doc has? Or whether a doc has any sub-collections. How can I do that?
On mobile clients, you can't know how many collections or what collections a document has. There is just no API for that. You have to know ahead of time what collections may exist for a document, or accept that a query on an unknown subcollection may return no documents.

Resources