python - Pyrebase 和 Firebase 数据库规则,如何用 Python 处理?
问题描述
我是 Firebase 的新手,我使用 Pyrebase 作为 Python 3.7 的库。为了测试这个库,我创建了一个带有公共规则的实时数据库:
// 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 很容易。现在,我想在我的案例中引入更多限制性规则:
// 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 代码是这样的:
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)
现在,当我尝试阅读某些内容时:
#user_id is the name of the child of main_document
db.child(self.main_document).child(user_id).get(self.user['idToken']).val()
从文档中阅读并尝试从规则语法中理解,我发现我需要可以从self.user
变量中获取的用户 UID。但是我怎样才能“发送”到 Firebase 以使其有可能进行匹配?我希望我清楚:)
解决方案
您将需要使用 Firebase 身份验证来登录用户,如 Pyrebase 关于身份验证的文档中所示:firebase.auth().sign_in_with_email_and_password(email, password)
。
用户登录后,他们的凭据将随每个请求自动发送到 Firebase,Firebase 将按照auth
您数据库的安全规则提供这些凭据。
推荐阅读
- node.js - 将服务器部署到heroku dev和prod
- ios - 如何弹出导航视图并返回上一个视图而不是根视图 SwiftUI?
- c# - 自动按照约定映射与拥有的实体相结合
- amazon-web-services - Docker-compose 开始失败,没有对代码库进行任何更改
- javascript - Flex 不会扩展 div 的内容以适应 div 的全宽
- python - 在python中的多个列上循环和融化?
- asp.net-core - 试图强制 ASP.NET 核心 MVC 使用 HTTPS
- mongodb - 将 ISO 格式日期时间转换为 mongoDB 3.6 中的日期
- tensorflow - tensorflow 与 anaconda 和 python 版本冲突
- user-interface - 光子碰撞问题