Firebase - 如何在身份验证后为每个用户写入/读取数据

Mit*_*hun 12 android firebase firebase-security

我试图理解但是无法看到登录后我存储的数据的方式和位置.

public static final String BASE_URL = "https://xyz.firebaseio.com";

Firebase ref = new Firebase(FirebaseUtils.BASE_URL);
    ref.authWithPassword("xyz@foo.com", "some_password", new Firebase.AuthResultHandler() {

    @Override
    public void onAuthenticated(AuthData authData) {
        Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
    }
    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
    }
}
Run Code Online (Sandbox Code Playgroud)

此时,我已成功通过身份验证并登陆MainActivity.接下来onCreateMainActivity.我初始化了Firebase

firebase = new Firebase(FirebaseUtils.BASE_URL).child("box");

// adapter below is an ArrayAdapter feeding ListView
firebase.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        if (dataSnapshot.getValue(Box.class) instanceof Box)
            adapter.add(dataSnapshot.getValue(Box.class).getName());
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        adapter.remove(dataSnapshot.getValue(Box.class).getName());
    }

    // other callbacks
}
Run Code Online (Sandbox Code Playgroud)

我有一个添加按钮,用于将新记录从Android推送到Firebase.

final Button button = (Button) findViewById(R.id.addButton);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Box box = new Box("Box " + System.currentTimeMillis(), "Location " + System.currentTimeMillis());
        Firebase fbBox = firebase.child("" + System.currentTimeMillis());
        fbBox.setValue(box);
    }
});
Run Code Online (Sandbox Code Playgroud)

但是上面的代码没有添加任何记录(显而易见的ListView是没有更新)或至少我可能不知道在哪里查找数据.我Firebase在浏览器中检查了打开,但我不确定如何检查用户特定数据?

我修改了我的Firebase Rules喜欢

{
    "rules": {
      "users": {
        "$uid": {
          ".write": "auth != null && auth.uid == $uid",
          ".read": "auth != null && auth.uid == $uid"
        }
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试打开https://xyz.firebaseio.com/xxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxx网址,但不会显示任何数据.

我想了解一些信息:

  1. 如何在身份验证后添加用户特定数据.当我们在用户的基础上对读/写没有任何限制时,它不能是无缝的,因为我可以轻松地读/写数据.

  2. 是否有Firebase可视化数据库或查看JSON数据的Web视图,我可以在哪里查看/修改Android设备的数据?

Xi *_*Wei 15

首先,修改Firebase规则,如下所示:

{
  "rules": {
    "users": {
      "$uid": {
        ".write": "$uid === auth.uid",
        ".read": "$uid === auth.uid"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,在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");
Run Code Online (Sandbox Code Playgroud)

最后,数据看起来像这样:

Firebase数据

  • 哇这个SO帖子或多或少向我解释了Firebase是如何工作的,没有意识到它不是一个SQL表而是一个层次结构 (2认同)