Back to results for “Security”
Flutter › Firebase Authentication in Flutter

How does Firebase Authentication integrate together with Firestore security rules to properly control exactly which specific data a given authenticated user is actually allowed to access?

Intermediate

Answer

Once a user has successfully authenticated using Firebase Authentication, their unique user identifier automatically becomes available within Firestore security rules through the request.auth.uid expression, letting you write security rules that specifically compare that authenticated user's identifier against a corresponding field stored within a given document, such as ensuring a user can only ever read or write documents where a stored userId field genuinely matches their own specific authenticated identifier, tightly and reliably tying together who a user actually is with precisely what specific data they are genuinely allowed to access.
match /profiles/{userId} {
  allow read, write: if request.auth != null && request.auth.uid == userId;
}
Real-world example A personal journaling app configures Firestore security rules ensuring a user can only ever read or write their own specific journal entries, correctly and reliably tying access directly to their authenticated Firebase user identifier rather than relying on any potentially bypassable client side check alone.

Common follow-ups: What happens to a user's Firestore access if their authentication token happens to expire while the app is actively running?;How do you properly test these combined authentication and security rules together during development?