How to compare between string that i create on android studio with variable that saved on Firebase

78 views Asked by At

My Firebase Realtime Database saves each user with 4 features: name, email, password, and confirm password. The class of users:

    public class Users
{
    String Username,Email,Password,ConfirmPassword;

    public Users() {
    }
    public Users(String username, String email, String password, String confirmPassword) {
        Username = username;
        Email = email;
        Password = password;
        ConfirmPassword = confirmPassword;
    }

    public String getUsername() {
        return Username;
    }

    public void setUsername(String username) {
        Username = username;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }

    public String getConfirmPassword() {
        return ConfirmPassword;
    }

    public void setConformPassword(String conformPassword) {
        ConfirmPassword = conformPassword;
    }


}

The Firebase data that i saved:

    {
  "user": {
    "user22": {
      "confirmPassword": "12345678",
      "email": "[email protected]",
      "password": "12345678",
      "username": "user22"
    }
  }
}

In my app I have the option to change the current password to a new password, it requires you to put in your previous password(the current one that the Firebase saved) and then put new password(the user puts the data about the passwords on UI 'EditText'). See here how it looks: enter image description here

Then the user has to click on 'confirm button' to change the old password to a new one. But before its changes,the computer need to check if the old password compares to the current password that is saved on Firebase . I don't know how to do that, please help me!!

I tried to do that code but not sure if I get the access to the variable that I want:

public void showUserName()
{
    FirebaseDatabase Data= FirebaseDatabase.getInstance();
    DatabaseReference myRef=Data.getReference("user");
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                username.setText(snapshot.child("username").getValue(String.class));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

}

It always changed even when the previous pass don't compare to the current..

1

There are 1 answers

7
Alex Mamo On

To check a field for a particular value, there is no need to attach a persistent listener, you can only perform a Query#get() call.

So assuming that you have two EditText objects:

EditText previousPasswordEditText = findViewById(R.id.previous_password_edit_text);
EditText newPasswordEditText = findViewById(new_password_edit_text);

To check the value of the password field that exists in the database against the value that is introduced by the user inside the previousPasswordEditText and only then perform the update with the new password that was typed inside the newPasswordEditText, please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = db.child("user").child("user22");
userRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot userSnapshot = task.getResult();
            String oldPassword = userSnapshot.child("password").getValue(String.class);
             String previousPassword = previousPasswordEditText.getText().toString();
             String newPassword = newPasswordEditText.getText().toString();
             if (oldPassword.equals(previousPassword)) {
                 Map<String, Object> updatePassword = new HashMap<>();
                 updatePassword.put("password", newPassword);
                 userSnapshot.getRef().updateChildren(updatePassword).addOnCompleteListener(new OnCompleteListener<Void>() {
                     @Override
                     public void onComplete(@NonNull Task<Void> updateTask) {
                         if (updateTask.isSuccessful()) {
                             Log.d("TAG", "Update successful!");
                         } else {
                             Log.d("TAG", "Failed with: " + updateTask.getException().getMessage());
                         }
                     }
                 });
             } else {
                 Log.d("TAG", "The oldPassword and the previousPassword don't match!");
             }
        }
    }
});

While the above code will certainly work, please note that it's very important not to store sensitive data like passwords in plain text in the database. Malicious users might take advantage of that. I recommend you use Firebase Authentication and right after that secure the database using Firebase Realtime Database Security Rules.