Security Rules
Firestore security rules and data protection
Firestore Security Rules
Because Firebase allows clients to communicate directly with the database, we rely on firestore.rules to enforce access control. Without these rules, any user could modify product prices or read another user's personal data.
Deployment
Rules are deployed using the Firebase CLI:
The Rules File
Explanation
- Helper Functions: We abstract
isAuthenticated(),isAdmin(), andisOwner()to make the rules readable. - Role-Based Access: The
isAdmin()function actually performs aget()request to read the user's document and check therolefield. - Data Isolation: Users can only access the
users/{uid}path that matches their ownrequest.auth.uid. They cannot snoop on others.