我是Firebase世界的新手,我正在使用Pyrebase作为Python3.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()
我的Db结构如下:
从文档中读取并尝试从规则语法中理解,我发现我需要可以从self.user
变量获得的用户的UID。但我如何才能“发送”到Firebase,以便让它有可能进行匹配呢?我希望我说得很清楚:)
发布于 2020-06-10 14:05:27
您需要使用Firebase身份验证登录用户,如authentication:firebase.auth().sign_in_with_email_and_password(email, password)
上的Pyrebase文档所示。
一旦用户登录,他们的凭据将在每次请求时自动发送到Firebase,Firebase将使这些凭据在数据库的安全规则中作为auth
可用。
https://stackoverflow.com/questions/62301320
复制相似问题