如何配置Firebase Database规则仅允许两个指定用户读写?
The Problem
Your current rule uses a comma to list multiple UIDs, which isn't valid syntax for Firebase security rules. The error Rule expressions may not contain ',' operator is Firebase's way of telling you that commas can't be used in these boolean check expressions. Your original code also had minor JSON formatting issues (like missing a comma between .write and .read rules) that would cause additional problems.
The Solution
Instead of commas, use the logical OR operator (||) to check if the authenticated user's UID matches either of your two allowed accounts. Here's the corrected, properly formatted rule set:
{ "rules": { ".write": "auth.uid === 'yoz4SkfUu3gv1QZt8dVFsF4jj9J2' || auth.uid === '第二个用户的实际UID'", ".read": "auth.uid === 'yoz4SkfUu3gv1QZt8dVFsF4jj9J2' || auth.uid === '第二个用户的实际UID'" } }
Explanation
||acts as a logical "or" in Firebase's rule syntax (which mirrors JavaScript). It returnstrueif either of the UID match conditions is satisfied, granting the user write/read access.- Replace
'第二个用户的实际UID'with the real UID of your second allowed user. - I've fixed the JSON structure too—note the comma separating the
.writeand.readrules, which was missing in your original code.
Bonus Cleanup for More Users
If you ever need to allow more than two users, you can use an array with the in operator for cleaner, more scalable code:
{ "rules": { ".write": "auth.uid in ['yoz4SkfUu3gv1QZt8dVFsF4jj9J2', '第二个用户UID', '第三个用户UID']", ".read": "auth.uid in ['yoz4SkfUu3gv1QZt8dVFsF4jj9J2', '第二个用户UID', '第三个用户UID']" } }
内容的提问来源于stack exchange,提问作者nikhil arora

