Home > Back-end >  Fail to edit the email and password fields of user model
Fail to edit the email and password fields of user model

Time:04-02

I have a user model with some fields which I try to update. However, when I edit a user's password or email, my user model in firebase storage remains the same, whereas the firebase auth updates correctly.

here is my update user method

private void updateUser() {
        String mFullName = editFullName.getText().toString().trim();
        String mEmail = editEmail.getText().toString().trim();
        String mPhone = editPhone.getText().toString().trim();
        String mPassword = editPassword.getText().toString().trim();

        if(isFullNameChanged(mFullName)){
            user.setFullName(mFullName);
        }
        if(isPhoneChanged(mPhone)){
            user.setPhone(mPhone);
        }
        if(isEmailChanged(mEmail)){
            currentUser.updateEmail(mEmail)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                System.out.println("Email successful "   mEmail);
                                user.setEmail(mEmail);
                                System.out.println("Email successful "   mEmail   " "   user.getEmail());
                            }
                            else{
                                Toast.makeText(editProfile.this, "Email update error, re-authentication required", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
        if(isPasswordChanged(mPassword)){
            currentUser.updatePassword(mPassword)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                System.out.println("Password successful");
                                user.setPassword(mPassword);
                            }
                            else{
                                Toast.makeText(editProfile.this, "Password update error, re-authentication required", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }

        userReference.child(currentUser.getUid()).setValue(user);
        System.out.println("My current email: "   currentUser.getEmail()   " "   user.getEmail());
        Toast.makeText(editProfile.this, "Profile update successful", Toast.LENGTH_SHORT).show();
    }

user model

public class User {

    String fullName, password, email, date, phone, imageUri;

    public User(){

    }

    public User(String mFullName, String mEmail, String mPassword, String mPhone, String mDate, String mImageUri){
        this.fullName = mFullName;
        this.email = mEmail;
        this.password = mPassword;
        this.phone = mPhone;
        this.date = mDate;
        this.imageUri = mImageUri;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getImageUri() {
        return imageUri;
    }

    public void setImageUri(String imageUri) {
        this.imageUri = imageUri;
    }
}

I don't know why, but in completeListener functions, my user changes the field, but when I get out of it the user has no changes at all. I tried to figure it out but didn't find anything

CodePudding user response:

This is because you are not updating the email and password in the model when updating in Auth. Try this code:

private void updateUser() {
        String mFullName = editFullName.getText().toString().trim();
        String mEmail = editEmail.getText().toString().trim();
        String mPhone = editPhone.getText().toString().trim();
        String mPassword = editPassword.getText().toString().trim();

        if(isFullNameChanged(mFullName)){
            user.setFullName(mFullName);
        }
        if(isPhoneChanged(mPhone)){
            user.setPhone(mPhone);
        }
        if(isEmailChanged(mEmail)){
            user.setEmail(mEmail);
            currentUser.updateEmail(mEmail)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                System.out.println("Email successful "   mEmail);
                                user.setEmail(mEmail);
                                System.out.println("Email successful "   mEmail   " "   user.getEmail());
                            }
                            else{
                                Toast.makeText(editProfile.this, "Email update error, re-authentication required", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
        if(isPasswordChanged(mPassword)){
            user.setPassword(mPassword);
            currentUser.updatePassword(mPassword)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if(task.isSuccessful()){
                                System.out.println("Password successful");
                                user.setEmail(mPassword);
                            }
                            else{
                                Toast.makeText(editProfile.this, "Password update error, re-authentication required", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }

        userReference.child(currentUser.getUid()).setValue(user);
        System.out.println("My current email: "   currentUser.getEmail()   " "   user.getEmail());
        Toast.makeText(editProfile.this, "Profile update successful", Toast.LENGTH_SHORT).show();
    }
  • Related