0

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

enter image description here

  • 1
    what do you mean "it's name"? Seeing as you're the one who writes the code, set whatever name you need set to whatever value you need it to be set to – Stultuske Apr 08 '22 at 07:06
  • I am new to firebase and i take this code from youtube tutorial. –  Apr 08 '22 at 07:42
  • You have to explain what you want to have changed. Because "name" in your snippet is "Tom" while the UID is used as a key for your value in Firebase Realtime Database – b2m9 Apr 08 '22 at 08:31
  • Sorry didnt realize, i want to change key that way so it can function as UID. –  Apr 08 '22 at 08:48
  • There is no way you can change the key. Please check the duplicate to see a workaround. – Alex Mamo Apr 08 '22 at 09:21
  • 1
    @DaniilNedbaylo You specify the key as UID in your code yourself on this line: `child(FirebaseAuth.getInstance().getCurrentUser().getUid())`. If you want to use (for example) the user's email address instead, then do it there. – b2m9 Apr 08 '22 at 09:36
  • 1
    That sounds like an answer @b2m9. --- Wanna post it below now that the question is reopened? – Frank van Puffelen Apr 08 '22 at 13:59

1 Answers1

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.

b2m9
  • 591
  • 2
  • 9
  • 1
    One slight note: you'll need to encode the email address as the `.` in there is not allowed in a Firebase key. The common encoding is to replace the `.` with a `,` (comma) as *that* is conveniently allowed in Firebase, but cannot occur in an email addrss. – Frank van Puffelen Apr 08 '22 at 15:02