You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Firebase Database规则仅允许两个指定用户读写?

Fixing Firebase Realtime Database Rule Error for Multiple Users

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 returns true if 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 .write and .read rules, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:58:13