I am pretty new to Firebase and have a question.
I'm looking to link my new authenticated user to a database that I have working.
I have class named addChild which contains an editText for add children's name, and need this to be linked to the authenticated user on a database. So when that user logs in, they can see their children's names that they have inputted.
Does anybody know how I could do this? I have set up the real-time database connection Im just not sure where to go from here.
Thanks This is how I created user.
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(CreateUserAccount.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(CreateUserAccount.this, "User Account Created" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(CreateUserAccount.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(CreateUserAccount.this, LandingPage.class));
finish();
}
}
});
After you authenticate a user using Firebase Authentication, then also you can send his uid
to the database:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
then in the database you would have this:
Users
useruid
name: userx
email: userx@gmail.com
String name = editText.getText().toString();
String email = editTxt.getText().toString();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(useruid);
ref.child("name").setValue(name);
ref.child("email").setValue(email);
Edit:
} else {
FirebaseUser user= task.getResult().getUser();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid());
ref.child("name").setValue(name);
ref.child("email").setValue(email);
startActivity(new Intent(CreateUserAccount.this, LandingPage.class));
finish();
}
to add the children in the other activity:
then add them like this:
Children
useruid <--- the id that is in the usernode above
childname: john
useruid
childname: james bond
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.