简体   繁体   English

如何根据数据库用户ID(而非uid)设置Firebase规则

[英]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 ,例如user1234abcuser2434dbcc

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.

相关问题 $uid 节点如何处理 firebase-realtime-database 规则? - How does $uid node works on firebase-realtime-database rules? Firebase 数据库规则如何检查节点属性值是否与 uid 进行比较 - Firebase database rules how to check if node property value for comparison to uid 如何将 user.uid 作为 id 的文档添加到 Firebase 中的集合中? - How to add a document with user.uid as id to a collection in Firebase? 如何在Firebase数据库HTTP触发器中为每个用户访问uid - How to access uid for each user in Firebase database HTTP triggers 如何从Firebase中的实时数据库中删除具有UID的用户? - How to delete a user with UID from Real Time Database in Firebase? 如何设置Firebase数据库安全规则? - How to set firebase database security rules? 使用规则在Firebase数据库中强制字段的用户ID - Enforce user id for a field in Firebase Database using rules Firebase 实时数据库规则限制用户 id 在模拟器中运行但不在 AngularFire 中 - Firebase RealTime database rules to restrict on user id runs in simulator but not in AngularFire Firebase 数据库安全规则 - 根据动态 CustomClaim 名称设置访问权限 - Firebase Database Security rules - set access based on dynamic CustomClaim name 如何在Firebase中使用用户uid授予用户读取数据库中写入数据的权限 - How to use user uid in firebase to give user permission to read write data in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM