Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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("[email protected]", "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?

like image 307
Mithun Avatar asked Mar 06 '16 02:03

Mithun


Video Answer


1 Answers

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

like image 145
Xi Wei Avatar answered Oct 26 '22 05:10

Xi Wei