如何确保用户只能在Firebase中查看和访问他们自己的数据? [英] How to make sure a user can only see and access their own data in firebase?

查看:140
本文介绍了如何确保用户只能在Firebase中查看和访问他们自己的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是firebase的新手。在我的Android应用程序中,我希望每个用户只能访问他创建的数据。我正在阅读 https://www.firebase.com/docs/security/ guide / secured-data.html ,但没有多少意义。假设我已经使用电子邮件和密码登录,那么我应该如何编写Firebase规则?


  1. b'LI>如何写信给我的火力地堡数据在我的Android的Java代码?



谢谢!



火力地堡规则

  {
的规则:{
的.read :真,
的.WRITE:真
}
}

Java代码的

  myFirebaseRef.child( 消息)的setValue( 这是消息)。 


解决方案

由于乔伊Roosing,我能够找出其余的。我需要根据用户的uid对数据进行分组。

首先,修改Firstbase规则,如Joey所说:

  {
的规则:{
的用户:{
$ UID:{
.write:$ uid === auth.uid,
.read:$ uid === auth.uid
}
}





$ b

然后,在我的Java代码中:

  Firebase rootRef =新的Firebase(https://user-account.firebaseio.com/); 
//假设用户已经登录。
Firebase userRef = rootRef.child(users /+ rootRef.getAuth()。getUid());
userRef.child(message1)。setValue(Hello World);



最后,我的数据会是这个样子:




I am new to firebase. In my Android app, I want each user to only access the data he created. I was reading https://www.firebase.com/docs/security/guide/securing-data.html, but didn't make much sense out of it. Assuming I am already logged in with an email and password,

  1. How can should I write the Firebase Rules?
  2. How do write to my Firebase data in my Android Java code?

Thanks!

Firebase Rules

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

Java Code

myFirebaseRef.child("message").setValue("This is the message");

解决方案

Thanks to Joey Roosing, I was able to figure out the rest of it. I need to group the data based on the uid of the user.

First, modify the Firstbase rules as Joey mentioned:

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

Then, in my Java Code:

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");

In the end, my data would look something like this:

这篇关于如何确保用户只能在Firebase中查看和访问他们自己的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆