'How to set users name in firebase realtime-database
When i am creating new account its name equals users UID is there anyway to change it to custom name?
My code
mAuth.createUserWithEmailAndPassword(Email, Password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
User user = new User(Name, LastName, Email);
curentUser = Name + " " + LastName;
FirebaseDatabase.getInstance().getReference("No server")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "User has been registered successfully!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"Failed to register! try again!", Toast.LENGTH_LONG).show();
}
}
});
}else{
Toast.makeText(MainActivity.this,"Failed to register! try again!", Toast.LENGTH_LONG).show();
}
}
});
How it looks like
Solution 1:[1]
You can't change the key of an existing value in Firebase Realtime Database without reading the value, write it under a new key, and delete the old one.
That being said, you chose the key of the user object yourself:
FirebaseDatabase
.getInstance().getReference()
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
The child method contains the key for the value you set in setValue. And you use the current user's UID for this.
You can change this to whatever value you like. For example, you could the user's email as a key (see reference for current user object):
.child(FirebaseAuth.getInstance().getCurrentUser().getEmail())
Note: See the comment from @puf below, you might want to encode an email address when using as a key.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

