Firebase - How to write/read data per user after a

2020-05-23 08:31发布

I have tried to understand but not able to see how and where might be the data I am storing after login is going.

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) {
    }
}

At this point I am successfully authenticated and landed onto MainActivity. Next in onCreate of MainActivity. I initialize 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
}

There is a add button that I used to push new records from Android to 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);
    }
});

But above code doesn't add any record (evident from ListView that is not updated) or atleast I might not know where to look for the data. I checked opening Firebase in browser, but I am not sure how to check for user specific data?

I modified my Firebase Rules like

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

I tried to open URLs such as https://xyz.firebaseio.com/xxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxx but it wouldn't show any data.

I would like to have some information on:

  1. How to add user specific data after authentication. Can't it be seamless like when we don't have any restriction on read/write on user basis, because I could easily read/write data.

  2. Is there any Firebase web view to visualize the database or see JSON data, where I can see/modify the data to/from Android device?

2条回答
乱世女痞
2楼-- · 2020-05-23 09:07

For web end

Javascript code:

var user = firebase.auth().currentUser;

if (user != null) {
   uid = user.uid;
   firebase.database().ref('/users/'+uid).push({
      foo: 'abc',
      bar: 'pqr'
   });
}

Read https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user for more information.

查看更多
家丑人穷心不美
3楼-- · 2020-05-23 09:28

First, modify the Firebase rules as follows:

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

Then, in 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, the data would look something like this:

Firebase Data

查看更多
登录 后发表回答