Firestore规则认证验证失效问题求助
Hey, let's figure out why your Firestore auth validation isn't working for bill creation. I'll walk through the most common issues and fixes to get this sorted:
First, Clarify the Exact Problem
Are you seeing that unauthenticated users can still create bill documents (which shouldn't happen with your rules), or are logged-in users getting blocked when they try to add a bill? Knowing this will narrow down the root cause right away.
1. Check if Firebase Auth is Actually Initialized & User is Logged In
Your code calls firebase.firestore() directly, but you need to make sure Firebase Auth is set up correctly and the user is authenticated when the create request is sent:
- Add a quick check right before calling
db.add()to confirm the current user state:const currentUser = firebase.auth().currentUser; console.log("Current authenticated user:", currentUser); // If this logs null, the user isn't logged in when the request is sent - In React Native, auth state can take a moment to sync. It's better to wrap your Firestore operation in an auth state listener to ensure you only run it when a user is logged in:
firebase.auth().onAuthStateChanged(user => { if (user) { // Only run the create operation if a user is logged in const db = firebase.firestore().collection('bills'); const data = { year: year, month: month, description: description, dateCreated: new Date() }; db.add(data) .then(ref => console.log('Added record with id:', ref.id)) .catch(error => console.error('Error adding bill:', error)); } else { console.log('No user is logged in - cannot create bill'); } });
2. Verify Your Firestore Rules Are Deployed
It's super easy to forget to save and deploy updated rules! Head to your Firebase Console, go to Firestore > Rules, and make sure the rule you wrote is live and active:
service cloud.firestore { match /databases/{database}/documents { match /bills/{billid} { allow create: if request.auth.uid != null; } } }
As a quick test, you can temporarily set allow create: if true; (don't leave this in production!)—if that works, the issue is definitely with the auth check in your original rule.
3. Check Rule Enforcement Logs
Firebase has built-in logs to show exactly why a rule passed or failed. Go to Firestore > Monitoring > Rules, and look at the recent rule evaluations. You'll see the request.auth value for each request—this will tell you if the auth context is being passed correctly with your create request.
4. Ensure React Native Firebase Version Compatibility
Outdated or mismatched versions of react-native-firebase can cause integration issues between Auth and Firestore. Check your package.json for the version, and make sure it's up to date (or matches the compatibility guidelines from the official docs).
5. Rule Nesting & Overrides
Double-check if you have any top-level rules that might be overriding your /bills rule. For example, if you have something like this:
match /databases/{database}/documents { allow read, write: if false; // This blocks all operations, overriding lower rules match /bills/{billid} { allow create: if request.auth.uid != null; } }
Top-level rules take precedence, so make sure you're not accidentally blocking all writes above your specific /bills rule.
内容的提问来源于stack exchange,提问作者Mark Thien

