Cameron Henige
Cameron Henige

Reputation: 416

How can I setup Firebase Storage rules to check parameters in Firebase Database?

I have the following data structures in Firebase

Firebase Database

House/(id)/Viewers/(UID)/{
    startdate = “Dec 1, 2019”
    endData = “Dec 8, 2019”
}

Firebase Storage

House/(id)/SensitiveImages/sensitiveImage.png

I want to write a rule in Firebase storage that only allows access to the SensitiveImages folder if the users UID is inside of the list of Viewers and the current time is between the startDate and endDate. However, there is no way to access this information from inside the Firebase Storage rules. How can I do this?

Upvotes: 0

Views: 343

Answers (1)

Kolban
Kolban

Reputation: 15256

One possibility is to write a Cloud Function that serves as the "processor" for image requests. Rather than access the image directly, we could expose a Cloud Function as the proxy for the image. The Cloud Function could then evaluate an expression using current date and requestor identity in conjunction with the data stored in the database. If allowed, then the Cloud Function could return the raw data which the Cloud Function is authorized to access.

As an alternative to the Cloud Function returning the data, the Cloud Function could return a URL that could be used to access the data. This could be a signed url from Google Cloud Storage that would give only the possessor of that URL access and would also be time bound to prevent access after expiration.

Upvotes: 2

Related Questions