我正在开发聊天插件,有两种类型的用户:操作员和访客。但是我无法成功添加用户。
这是我的安全规则:
{
"rules": {
// By default, make all data private unless specified otherwise.
".read": "auth != null",
".write": "auth != null",
// Only operators or user itself can update their data
"users": {
"$user_id": {
".read": "auth != null && ($user_id === auth.user_id || auth.operator === true)",
".write": "auth != null && ($user_id === auth.user_id || auth.operator === true)",
".validate": "($user_id === newData.child('user_id').val() && newData.hasChildren(['name', 'type', 'status']))"
}
}
}
}
我在聊天控制台中将用户添加到 Firebase DB 时收到了这些日志消息:
Authenticated successfully with payload: Object {user_id: 112, is_operator: true}
Auth expires at: Thu Aug 15 2013 20:12:22 GMT+0300 (GTB Daylight Time)
FIREBASE: Attempt to write {"type":"operator","name":"op","user_id":112} to /users/operators/112 with auth={"user_id":112,"is_operator":true}
FIREBASE: /:.write: "auth != null"
FIREBASE: => true
FIREBASE: /users/operators:.validate: "($user_id === newData.child('user_id').val() && newData.hasChildren(['name', 'type', 'status']))"
FIREBASE: => false
FIREBASE: Validation failed.
FIREBASE: Write was denied.
FIREBASE WARNING: set at /users/operators/112 failed: permission_denied
(!) ERROR: Error: PERMISSION_DENIED :: auth() function while creating user
我该如何解决这个问题?
解决方案
我错了$user_id
。它应该auth.user_id
在“.validate”部分:
".validate": "(auth.user_id === newData.child('user_id').val() && newData.hasChildren(['name', 'type', 'status']))"