0

I'm implementing an app which uses Firebase for authentication and that works fine. What I want to do now is to add some more data in the "users" node in the real time database. Because of that I changed the database roles and now they look like this.

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

Right now I have some code that works for saving the data in the "users" JSON node and this is that code:

 ` mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {


                    DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
                    dbRef.child("users").child(task.getResult().getUser().getUid()).push().setValue"jovan").addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Log.i("info", "createUserInDatabase:success");
                            } else {
                                Log.i("info", "createUserInDatabase:failure");
                            }
                        }
                    });

                    Log.i("Info", "" + task.getResult().getUser().getUid());
                    Log.i("info", "createUserWithEmail:success");
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(RegisterActivity.this, "Registered and logged in", Toast.LENGTH_SHORT).show();
                } else {
                    Log.i("info", "createUserWithEmail:failure");
                }
            }
        });`

As I said this code works but when I took a look in the Firebase I found out that my data need to be more structured and easy to read.

enter image description here

Here is a link of what I want to do, the content is on the second answer. Stack Overflow link.

Thanks in advance.

1 Answer 1

1

Instead of pushing, create the user child in users node with the uid of that user. Then set user details to that node.

bRef.child("users").child(uid).setValueAsync(otherUserData)
// otherUserData is a map or an object containing other user data

Your data structure will look like this.

users

--> -L3NpAd......

----> name: "jovan"

----> age: 20

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.