我是 Firebase 的新手。在我的 Android 应用程序中,我希望每个用户只能访问他创建的数据。我在读https://www.firebase.com/docs/security/guide/securing-data.html ,但没有多大意义。假设我已经使用电子邮件和密码登录,
- 我应该如何编写 Firebase 规则?
- 如何在我的 Android Java 代码中写入我的 Firebase 数据?
谢谢!
Firebase 规则
{
"rules": {
".read": true,
".write": true
}
}
Java代码
myFirebaseRef.child("message").setValue("This is the message");
最佳答案
多亏了 Joey Rosing,我才能够弄清楚其余部分。我需要根据用户的 uid 对数据进行分组。
首先,按照 Joey 提到的修改 Firstbase 规则:
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
}
}
}
然后,在我的 Java 代码中:
Firebase rootRef = new Firebase("https://user-account.firebaseio.com/");
// Assuming the user is already logged in.
Firebase userRef = rootRef.child("users/" + rootRef.getAuth().getUid());
userRef.child("message1").setValue("Hello World");
最后,我的数据看起来像这样:
关于android - 如何确保用户只能在 firebase 中查看和访问自己的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820836/