英文:
How to print `auth` from the Firebase Security Rules?
问题
在安全规则中,在调用 Firebase 实时数据库时,有没有办法打印/返回auth
的有效载荷?我在尝试使规则生效时遇到了问题,似乎是因为auth.uid
不包含我期望的值。有没有办法打印它的值?
更新:
在安全规则中,以下第一行转换为true
,而第二行转换为false
。我期望auth.uid
是22
,因此也期望第二行转换为true
。
root.child('projects').child($project_uuid).child('person').child('22').exists() // 是true
root.child('projects').child($project_uuid).child('person').child(auth.uid).exists() // 是false
在探索 Network > ws 下发送的数据后(请参见下面的图像),我仍然期望auth.uid
是22
,因此第二行转换为true
。有可能导致这种情况吗?
最小示例:
数据库内容:
projects: {
"abcd": {
person: {
22: {
permissions: {
read: true
}
}
},
data: {
allKindOfData: "abc"
}
}
}
安全规则:
{
"rules": {
"projects": {
"$project_uuid": {
"person": {
".read": "true"
},
".read": "root.child('projects').child($project_uuid).child('person').child('22').exists()
&& root.child('projects').child($project_uuid).child('person').child(auth.uid).exists()"
}
}
}
}
英文:
Is there a way to print/return the payload of auth
inside the Security Rules, when making a call to a Firebase Realtime database?
I'm struggling to get a rule to work and it seems like it's because auth.uid
doesn't contain the value I expect it to have. Is there a way to print the value it has?
Update:
In the security rules the first line below converts to true
while the second line converts to false
. I expect auth.uid
to be 22
, so would also expect the second line to convert to true
.
root.child('projects').child($project_uuid).child('person').child('22').exists() // is true
root.child('projects').child($project_uuid).child('person').child(auth.uid).exists() // is false
After exploring the sent data under Network > ws (see image below), I still expect auth.uid
to be 22
and therefore the second line to convert to true
. Any reason why this might not be the case?
Minimal case:
Database content:
projects: {
"abcd": {
person: {
22: {
permissions: {
read: true
}
}
},
data: {
allKindOfData: "abc"
}
}
}
Security Rules:
{
"rules": {
"projects": {
"$project_uuid": {
"person": {
".read": "true"
},
".read": "root.child('projects').child($project_uuid).child('person').child('22').exists()
&& root.child('projects').child($project_uuid).child('person').child(auth.uid).exists()"
}
}
}
}
答案1
得分: 1
打印/记录安全规则中似乎是不可能的。如果您想检查发送到Firebase的auth.uid
的值,您可以在前端中记录它:如果用户已登录,其uid
将与查询一起发送到Firebase后端服务,例如RTDB。
另一方面,您可以使用“规则游乐场”模块来调试您的规则。
英文:
Printing/logging in the security rules is not possible AFAIK. If you want to check the value of auth.uid
that is sent to Firebase you can log it in your front end: If the user is signed-in, its uid
will be sent together with the queries to the Firebase back-end services, e.g. the RTDB.
On the other hand you can use the "Rules Playground" module to debug your rules.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论