简体   繁体   English

在Firebase上创建/添加用户

[英]Creating/Adding user on Firebase

I have been playing with Firebase and I got stuck with a question, I tried to look some docs but I couldn't find a proper answer. 我一直在玩Firebase,我遇到了一个问题,我试着查看一些文档,但我找不到合适的答案。

The thing is that Firebase provides a user authentication, that's good, but can I set some limits to this user? 问题是Firebase提供了用户身份验证,这很好,但是我可以为这个用户设置一些限制吗? So it could have access just to one part of my database scheme? 那么它只能访问我的数据库方案的一部分? eg: have two grupos on my database: 例如:我的数据库上有两个grupos:

https://MYFIREBASE.firebaseio.com/group1/posts/ https://MYFIREBASE.firebaseio.com/group2/posts/ https://MYFIREBASE.firebaseio.com/group1/posts/ https://MYFIREBASE.firebaseio.com/group2/posts/

and which one has different users, different posts. 哪个有不同的用户,不同的帖子。

Thanks!! 谢谢!!

You're looking for Security Rules. 您正在寻找安全规则。

Security Rules are server side validations that restrict access to your Firebase database. 安全规则是服务器端验证,用于限制对Firebase数据库的访问。

Below are the rules for a read-only database. 以下是只读数据库的规则。

{
  "rules": {
     ".read": true,
     ".false": false
  }
}

Security Rules provide server variables that hold important server-side information. 安全规则提供保存重要服务器端信息的服务器变量。 The variable you'll be interested in is the auth variable. 您感兴趣的变量是auth变量。

The auth variable allows you to check if the user trying to access the database is authenticated. auth变量允许您检查尝试访问数据库的用户是否已通过身份验证。

{
  "rules": {
    "group1": {
       ".read": "auth !== null",
       ".write": "auth !== null"
    }
  }
}

According the rules above only authenticated users can access the /group1 location. 根据上述规则,只有经过身份验证的用户才能访问/group1位置。

But for user based security, you'll need to index using the user's uid . 但是对于基于用户的安全性,您需要使用用户的uid进行索引。

{
  "rules": {
    "group1": {
       "posts": {
         "$uid": {
           ".read": "auth.uid == $uid",
           ".write": "auth.uid == $uid"
         }
       }
    }
  }
}

And using wildcards (which are basically route parameters) , you can check to see if the user trying to access the data owns the data. 使用通配符(基本上是路由参数) ,您可以检查用户是否尝试访问数据是否拥有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM