Firestore Database Structure and Security:
Tags: firebase, firestore, react, security
Published: 2019-03-20 16:26:52

---
title: Firestore Database Structure and Security:
published: true
description: Firestore and My App - Database Structure and Security:
tags: #Firebase #Firestore #react #security
---
<div class="tutorial"><p>This is a quick explanation on how I structured my NoSQL database in Firebase's Firestore in a way that allowed me to secure my user's data and not allow the user to change certain data in a simple photography commerce site. I am a newbie programmer, so Firebase's Firestore was a great solution for me due to its simplicity and the quality of documentation and YouTube tutorials. Speaking of YouTube tutorials, this is just scratching the surface of what Mr. Todd Kerpelman discusses in the YouTube series <a href="https://www.youtube.com/watch?v=v_hR4K4auoQ&amp;list=PLl-K7zZEsYLluG5MCVEzXAQ7ACZBCuZgZ" rel="noopener noreferrer" target="_blank">Get To Know Cloud Firestore</a>. If you want a description on how a person who has been programming for three months utilized the videos to secure their database, read on!  If you want a detailed discussion, view the videos because they rock.</p><p>For those of you who want to see the security rules in action, please visit <a href="https://green-owl-photography.firebaseapp.com/" rel="noopener noreferrer" target="_blank">Green Owl Photography</a>.<br>For those of you who want to give me a job, please visit my <a href="https://andersjr1984.github.io/portfolio/" rel="noopener noreferrer" target="_blank">Portfolio</a>.</p><ul>This simple discussion will hit the following topics:<li>Data Structure</li><li>Firebase Security Rules and Testing (Super Awesome Stuff!)</li></ul><h4>Data Structure:</h4><ol><li>Firestore allows you to store a Collection on the root level, then Documents in each sub level. A document can either store an object or a Collection. This means that each odd node on your database is a collection and each even node is a document. Does that confuse you?  If so, let me show you a simple get request that pulls from a User's collection, then checks the orderData subcollection.</li><img src="https://andersjr1984.github.io/portfolio/static/media/getRequest.2e97d203.png" alt="Sub-Collection Document Get Request"><li>After the first time you authenticate on my site, a Document is created under your specific User ID that is generated by the Firebase Auth system. I'll go ahead and show you what is created when an anonymous user logs in for the first time. The items that are in your cart are set in the "images" document and your "userInfo" is set with an empty object for previous orders and default address.</li><img src="https://andersjr1984.github.io/portfolio/static/media/initialStructure.b17fc338.png" alt="Initial DB Structure"><li>As you step through my site, you will be adding images, entering a shipping address (if you are following along, feel free to enter a fake shipping address, make me laugh), and creating an order. This is just a portfolio project, so it doesn't create an order, it just creates a random 5-digit order ID to simulate creating an order using the Pwinty API. All this data is stored in the CurrentOrder subcollection. There are a couple of documents that are created here, such as images (aka the cart), shipmentInfo (aka shipment information), and the orderData (aka where I store any returns from Firebase Functions or API call, like a pwintyId). Remember that orderData is only stored from Firebase Functions, that information will be important to remember when I get to security rules. Here is how the data structure looks when you are finished:</li><img src="https://andersjr1984.github.io/portfolio/static/media/inProcessOrder.6aea5d2d.png" alt="Order Structure"><li>Once you "Promise You'll Pay", the relevant data for your order is sent to the Previous Order's page via Firebase Functions and Firebase Functions resets the Current Order information, which includes deleting the shipmentInfo and images portion of the db. The 5-digit order ID is used as the key for this document object. I'll let you take a look at the db data of previousOrders after I complete an order:</li><img src="https://andersjr1984.github.io/portfolio/static/media/previousOrders.1d6bf95d.png" alt="DB Structure of a Previous Orders"><li>My app has a listener for previous orders that updates the state of the app's Account page on any update to a user's Previous Orders on the database. Here is the code and the Account page after completing this example order:</li><img src="https://andersjr1984.github.io/portfolio/static/media/prevOrderListener.a439a42a.png" alt="Previous Order Listener"><img src="https://andersjr1984.github.io/portfolio/static/media/reviewOrders.bc64f029.png" alt="Review Orders"><li>I hope this shows you how I structured my data. I will now be delving in how I secure this data!</li></ol><h4>Security Rules and rule testing:</h4><ol><li>Security rules are significantly more robust than what I present here. As I noted earlier, the Firestore Security Rules video is super rad and is a must view for any Firebase developer.</li><li>I'll start off by showing you all my security rules, then I will discuss each specific rule.</li><img src="https://andersjr1984.github.io/portfolio/static/media/firestoreRules.c69e8f99.png" alt="Firestore Rules"><li>Lets start at the top and discuss the prices and skus. The price data and sku data are required to create any order, so we need to be able to read this information before the user logs in. This data is updated through Firebase Functions, so we don't want to allow the client to do any writes or updates. This is why we only allow reads. To make sure we can't update the prices, lets enter the simulator and see what happens when we simulate an update on the prices. We get a friendly "Simulated Write Denied" message, phewwwwww. We also want to make sure we can run a get command, so lets test that!</li><img src="https://andersjr1984.github.io/portfolio/static/media/pricesUpdateTest.cf40dc1d.png" alt="Simulated Prices update command"><img src="https://andersjr1984.github.io/portfolio/static/media/pricesGetTest.ae3913ca.png" alt="Simulated Prices get command"><li>Lets think back to the PreviousOrders document we discussed earlier. Once a user is authenticated in our site, we create a blank previous order object for their account. As you see in the rules, we allow read and create, but all updating is done through Firebase Functions. Future versions of this site will likely move this to Firebase Functions, we don't want to give any leeway to the client to create a PreviousOrders object populated with fraudulent previous orders, but I wanted to keep it for this write-up. I have boxed the simulated userId, which I have also entered in the userId authenticate section.</li><img src="https://andersjr1984.github.io/portfolio/static/media/previousOrdersUpdateTest.e4b53ed6.png" alt="Previous Orders Update Test"><li>Now lets look at the CurrentOrders/orderData. As I noted earlier, the orderData is information we receive from Firebase Functions API calls, such as the Pwinty Order Id. We don't want the client to be able to do anything but read this data. If you look at the fifth rule, though, it allows any read or write to any document in the CurrentOrder collection. What do you think will happen if we try to update the orderData?  Let's run a quick update test to see.</li><img src="https://andersjr1984.github.io/portfolio/static/media/orderDataInitialUpdateTest.2c5969d8.png" alt="Order Data Initial Update Test"><li>Oh no!  The user is allowed to update the orderData information. How can we fix that?  By disallowing any writes on orderData. Lets look at it again after adjusting the rules. I have added line 14 to not allow any writes, as well as updated the wildcard in line 16 and boolean statement in line 17 to make sure that the orderData is not the document that is updated. Other orderData, such as shipmentInfo and images can still be created and updated in the client app. This still allows the client to read orderData.</li><img src="https://andersjr1984.github.io/portfolio/static/media/orderDataFinalUpdateTest.11c0aa53.png" alt="Order Data Final Update Test"><li>We removed the wildcard in line 16, but we want to make sure shipmentInfo and Images can be created and updated. Lets run a test to make sure. Wahoo!  It works.</li><img src="https://andersjr1984.github.io/portfolio/static/media/imagesWriteTest.3a285acd.png" alt="Images Write Test"><li>And lets look at the Default Address to make sure the user can read, create, and update their default address.</li><img src="https://andersjr1984.github.io/portfolio/static/media/defAddrTest.d62678ab.png" alt="Default Address Test"><li>Now we want to make sure mal1 can't access uid1's sweet data and lets use wildcards in the simulator!</li><img src="https://andersjr1984.github.io/portfolio/static/media/malUserGet.eb5dbc53.png" alt="Malicious User Test"><img src="https://andersjr1984.github.io/portfolio/static/media/malUserGet2.dcf80b7e.png" alt="Malicious User Test"></ol><p>That wraps up the simple security rules that I have created for my app. Thanks for stopping by!</p></div>