How do I turn my Firebase Firestore project from test mode into Production mode for my iOS project?

Viewed 9576

I started an iOS app using Firebase's Firestore database in test mode, but now that I am done using test mode, how do I convert the database into Production mode?

4 Answers

There is not really a "mode" that you can switch to. If you're referring to the dialog that had you choose your initial security rules, all that did was pre-populate some security rules for you to get started. After that, it's up to you to come up with security rules that suit your application. I strongly suggest reading through the linked documentation to understand what you should do. Whatever you do, it will be highly customized to you specific data and access patterns.

When you make a new Firestore it says if you want to set it to production mode or test mode. The only difference between the two is that test mode allows read and write capabilities while production does not. To change from Test mode into Production mode all you do is go to Firebase > Firestore > Rules > Edit Rules. Then change the code from:

rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if
             request.time < timestamp.date(2021, 6, 6);
           }
         }
       }

To this:

rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if false;
        }
      }
    }

Allow only authenticated users to read/write to Firestore in Production

We can set our security rules in such a way that only authenticated users can read/write to firestore which is a common scenario for an app in production.

Use a whole separate collection like public which contains data that doesn't need any authentication if you choose to have some data for everyone.

Change the rules here Firebase > Firestore > Rules > Edit Rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
   match /public/{publicDoc}{
   allow read: if true;
   }
   match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

Note: You need to set up the authentication for your project to have the above code working.

If your app doesn't have authentication check for other possibilities here

There are some basic rules configurations given by firebase to set up rules for production-ready apps and we can set rules according to our use case. This is the link where all rules are given for test and production mode apps. To edit rules go to Firestore Datbase > Rules.

Firebase Firestore Rules

Related