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..
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:
To check the value of the
passwordfield that exists in the database against the value that is introduced by the user inside thepreviousPasswordEditTextand only then perform the update with the new password that was typed inside thenewPasswordEditText, please use the following lines of code: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.