12

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?

Mithun
  • 2,075
  • 3
  • 19
  • 26
  • Traditionally you would have have a /users node in firebase where user data would be stored. If it's a new user, you add their uid to the users node as the key and the children would be the key: value pairs of the data you was to store; name: "bob" location: "florida" etc. If the user is authenticating and exists, you will know their uid so you can read in their node from /users. There is a Firebase web view for your data, it's called the Firebase Dashboard and can be accessed once you log into your account on the Firebase website. – Jay Mar 06 '16 at 14:59
  • 1
    I had the same question and resolved it here: http://stackoverflow.com/questions/35820836/how-to-make-sure-a-user-can-only-see-and-access-their-own-data-in-firebase – Xi Wei Mar 18 '16 at 16:05

2 Answers2

16

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

Mithun
  • 2,075
  • 3
  • 19
  • 26
Xi Wei
  • 1,669
  • 1
  • 21
  • 33
  • 2
    Wow this single SO post more or less explained to me how Firebase works, didn't realize that it's not so much a SQL table but a hierarchy – Allison Oct 21 '17 at 04:45
  • 1
    these rules belong to "RealTime Database", how can I match the same rules for their new 'Cloud Firestore' ? because their JSON is different ? https://firebase.google.com/docs/firestore/security/rules-structure?authuser=0#basic_readwrite_rules – Ricky Levi Oct 01 '18 at 22:11
3

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.

shriyog
  • 938
  • 1
  • 13
  • 26