![](/img/trans.png)
[英]How to get providers access tokens from Firebase authenticated user?
[英]Firebase - how to restrict authenticated user's access to certain paths?
足够简单的问题:
我如何限制路径,例如...
/orders/<userid>
/cart/<userid>
/transactions/<userid>/txid
...仅适用于其userid
与路径中的userid
匹配的用户?
我已经设置了身份验证,并且需要在firebase.auth().onAuthStateChanged
之后在Vue中订阅其中的一些身份验证
根据此处的文档,似乎在以下规则示例中,用户令牌必须与密钥完全匹配:
{
"rules": {
"users": {
"$user_id": {
// grants write access to the owner of this user account
// whose uid must exactly match the key ($user_id)
".write": "$user_id === auth.uid"
}
}
}
}
这是否意味着/orders/123456/order123478
将受到限制,仅对123456
用户可用?
对于实时数据库,将规则更新为以下内容:
{
"rules": {
"orders": {
"$uid": {
".read": "auth.uid == $uid",
".write": "auth.uid == $uid"
}
}
}
}
$uid
键表示嵌套在该级别的任何键,并允许您在规则中引用它。 auth
变量由Firebase提供,包含有关发出请求的经过身份验证的用户的详细信息,因此您可以将请求用户的uid
与数据库的uid
密钥进行比较,并在匹配时授予权限(嵌套的".read"
和".write"
值)。
因此,对于uid
为user1
,他们将有权访问以下数据:
{
"orders": {
"user1": {
"order1": read/write,
"order2": read/write
},
"user2": {
"order1": no access,
"order2": no access
},
"user3": {
"order1": no access,
"order2": no access
},
}
阅读文档,并试用Firebase仪表板“规则”部分中的模拟器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.