[英]How to set firebase rules based on database user id (not uid)
Hi I have a list of users in my fb database. 嗨,我的fb数据库中有一个用户列表。 The user id is auto-generated after successful registration and each user entry has the name, email, and uid.
成功注册后,将自动生成用户标识,并且每个用户条目都有名称,电子邮件和uid。 Also, each of these properties is being indexed in /index so that I can easily look up a user's id given their uid.
另外,这些属性中的每一个都在/ index中建立索引,因此我可以轻松地根据用户的uid查找用户的id。
According to the docs, firebase says to use write: $user_id === auth.uid 根据文档,firebase说要使用write:$ user_id === auth.uid
How can I accomplish this using the generated ID instead of the uid? 如何使用生成的ID而不是uid完成此操作?
here's the fb db: https://medviz.firebaseio.com/ 这是FB数据库: https : //medviz.firebaseio.com/
In Your users object there will be users like this 在您的用户对象中,将有这样的用户
users : {
user1234abc:{
name: "abc"
},
user2434dbcc:{
name: "abc"
}
}
you will have many unique key/user nodes
in users object, eg user1234abc
, user2434dbcc
您将在users对象中拥有许多
unique key/user nodes
,例如user1234abc
, user2434dbcc
That unique key can be named any variable
in rules it not necessary to call it $user_id
, you can call it anything you want for example you can name the variable as $any_variable
for unique user keys 可以将唯一键命名为规则中的任何
variable
,而不必将其命名为$user_id
,您可以随意命名,例如,可以将唯一用户键的变量命名为$any_variable
。
and in auth.id whenever user successfully authenticate that same unique id is in auth.uid
so you can any where math that auth.uid
when you are editing the info related to that user 并在auth.id中每当用户成功验证同一唯一ID时,都在
auth.uid
因此在编辑与该用户相关的信息时,您可以在任何地方对该auth.uid
进行数学运算
For example if auth.uid
contains user1234abc
and if user1234abc
is editing this object user1234abc:{name: "abc" }
thn accordind to .write rule he will be able to 例如,如果
auth.uid
包含user1234abc
并且如果user1234abc
正在编辑该对象user1234abc:{name: "abc" }
,则user1234abc
规则将使他能够
See the updated view below 请参阅下面的更新视图
Updated Visual Hierarchy: 更新的视觉层次结构:
--schema--
users : {
________________
|
user1234abc:{ |
name: "abc" |
}, |
user2434dbcc:{ |
name: "abc" |
} |
} key can be name as any variable in rules
|
-- rules--
|
{ |
"rules": { |
"users": { |
".read": true, |
________________|___________
| |
\|/ |
"$any_variable": { \|/
".write": "$any_variable === auth.uid"
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.