Reputation: 306
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.
Here is a link of what I want to do, the content is on the second answer. Stack Overflow link.
Thanks in advance.
Upvotes: 0
Views: 590
Reputation: 972
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
Upvotes: 1