首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Pyrebase和Firebase数据库规则,如何用Python处理?

Pyrebase和Firebase数据库规则,如何用Python处理?
EN

Stack Overflow用户
提问于 2020-06-10 18:28:32
回答 1查看 2.4K关注 0票数 0

我是Firebase世界的新手,我正在使用Pyrebase作为Python3.7的库。为了测试这个库,我创建了一个带有公共规则的实时数据库:

代码语言:javascript
运行
复制
// These rules give anyone, even people who are not users of your app,
// read and write access to your database
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

测试API很容易。现在,我想在我的案例中引入更多的限制性规则:

代码语言:javascript
运行
复制
// These rules grant access to a node matching the authenticated
// user ID from the Firebase auth token
{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

但我遇到了很多问题和疑虑。我的身份验证Python代码是这样的:

代码语言:javascript
运行
复制
        firebase = pyrebase.initialize_app(self.config)
        self.username = email
        self.password = password
        self.db = firebase.database()
        self.auth = firebase.auth()
        # authenticate a user
        self.user = self.auth.sign_in_with_email_and_password(self.username, self.password)

现在,当我尝试阅读一些内容时,例如:

代码语言:javascript
运行
复制
#user_id is the name  of the child of main_document 
db.child(self.main_document).child(user_id).get(self.user['idToken']).val() 

我的Db结构如下:

从文档中读取并尝试从规则语法中理解,我发现我需要可以从self.user变量获得的用户的UID。但我如何才能“发送”到Firebase,以便让它有可能进行匹配呢?我希望我说得很清楚:)

EN

回答 1

Stack Overflow用户

发布于 2020-06-10 22:05:27

您需要使用Firebase身份验证登录用户,如authenticationfirebase.auth().sign_in_with_email_and_password(email, password)上的Pyrebase文档所示。

一旦用户登录,他们的凭据将在每次请求时自动发送到Firebase,Firebase将使这些凭据在数据库的安全规则中作为auth可用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62301320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档