Cloud Firestore
Every database needs rules to give limited permissions to users and full control to the admin. So, we will add rules to Firestore.
Rules
- First, log in to your Firebase console and select your project.
- Navigate to the “Authentication” section from the left-hand menu.
- enable email and password authentication and google authentication from the sign-in method tab.
- Click on the “Users” tab and then click on the “Add User” button at the top of the page.
- add a user (it will be your admin user) and copy his uid.
- Go to Firebase → Cloud Firestore → Rules and paste these rules:
Important: Replace ADMIN_UID with your admin UID.
rules_version = '2';
ervice cloud.firestore {
match /databases/{database}/documents {
// TODO: Put your admin UID
function adminUid() {
return "Put-Admin-Uid-Here";
}
// allow read,write to admin
match /{document=**} {
allow read, write: if request.auth.uid == adminUid();
}
// Restaurants collection
// Allow read only to normal users
match /restaurants/{restaurantId} {
allow read: if request.auth != null;
}
//for collection group
match /{path=**}/products/{productId} {
allow read,write: if request.auth != null;
}
// users collection
// allow users to read and write their own data
match /users/{userId}{
allow read, write: if userId==request.auth.uid;
}
// allow users to read and write their orders
match /users/{userId}/orders/{ordersId}{
allow read, write: if userId==request.auth.uid;
}
// allow users to read and write their own favorites
match /users/{userId}/favorites/{favoriteId}{
allow read, write: if userId==request.auth.uid;
}
}}
Indexes
- Open the Firebase console and select your project.
- Click on the “Firestore Database” from the left-hand side menu.
- Select the “Indexes” tab from the top menu.
- Click on the “Single Field Indexes” dropdown and select “Add Index”.
- In the “Collection ID” field, enter “orders” (without quotes).
- In the “Field Path” field, enter “id” (without quotes).
- In the “Collection Group Scope” dropdown, select “Arrays”,“Ascending”, “Descending”.
- Click on the “Create” button to add the index.
- Repeat steps 4-8, but in step 5, enter “orders” and in step 6, enter “time”.
- Repeat steps 4-8, but in step 5, enter “products” and in step 6, enter “stars”.