Is there a way to edit username and email via code in android studio?

Viewed 145

I was wondering if there was a way to edit the name and email via code in Android Studio, to change it in Cloud Firestore. I made a program where it only changes the name in real-time and when logged off, and logged in again it changes back to the previous one which is in Cloud Firestore.

vardas is an EditText field in the app design.

My code:

public void updateProfile(final View view) {

    view.setEnabled(false);

    vardas1 = vardas.getText().toString();

    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    UserProfileChangeRequest request = new UserProfileChangeRequest.Builder()
            .setDisplayName(vardas1)
            .build();

    firebaseUser.updateProfile(request)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    view.setEnabled(true);
                    Toast.makeText(Profile.this, "SÄ—kmingai atnaujintas profilis", Toast.LENGTH_SHORT).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    view.setEnabled(true);
                    Log.e(TAG, "onFailure: ", e.getCause());
                }
            });
}
1 Answers

I was wondering if there was a way to edit the name and email via code in Android Studio, to change it in Cloud Firestore.

Yes, there is. According to the official documentation regarding how to update a document in Cloud Firestore:

To update some fields of a document without overwriting the entire document, use the update() method.

Assuming you want to update the name and email of the authenticated user that exists at the following reference:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
DocumentReference uidRef = usersRef.document(uid);

Try the following lines of code:

uidRef.update(
              "name", "John",
              "email", "john@email.com"
             ).addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        Log.d(TAG, "DocumentSnapshot successfully updated!");
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.w(TAG, "Error updating document", e);
    }
});

The result of using this is code, is the update of the name property with "John".

What you are doing in your code is nothing else than updating the name in the FirebaseUser object. That operation is not related in any way with Firestore. So updating the FirebaseUser it doesn't mean that the user will be also updated in the Firestore database. There are two different separate operations that are not related.

Related