I am developing an application for conducting transactions, and I've implemented a password validation feature before proceeding with any transactions. Within the app, there's a function called passCheck, which retrieves a value from Firebase Realtime Database and compares it with the password entered by the user.
However, I've encountered an issue where the app functions correctly the first time around. But subsequent attempts to perform transactions before closing the app result in an "Incorrect password" message, even when the correct password is entered.
private void passCheck(final String enteredPassword, final PassCheckCallback callback) {
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");
Query query = usersRef.child("Reg_Users").orderByChild("userId").equalTo(userId);
Log.d("passCheck ", "Checking...."+enteredPassword.toString());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String upiPass = snapshot.child("upi_pass").getValue(String.class);
// Compare entered password with retrieved upi_pass
Log.d("passCheck",upiPass.toString());
if (upiPass != null && upiPass.equals(enteredPassword)) {
callback.onPassChecked(true); // Password matches
upiPass="";
return;
}
}
// Password doesn't match or upi_pass not found
callback.onPassChecked(false);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// Handle potential errors here
callback.onPassChecked(false); // Notify callback about failure
}
});
}
The Function is then used here
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
String enteredPassword = password.getText().toString();
Log.d("Password",enteredPassword.toString());
// Check the entered password
passCheck(enteredPassword, new PassCheckCallback() {
@Override
public void onPassChecked(boolean isCorrect) {
if (isCorrect) {
// Password is correct, proceed with updating balance and transferring money
updateBalance();
usersTransaction();
progressBar.setVisibility(View.INVISIBLE);
Intent intent = new Intent(password_validate.this,MainActivity.class);
startActivity(intent);
finish();
} else {
progressBar.setVisibility(View.INVISIBLE);
// Password is incorrect, show error message
Toast.makeText(password_validate.this, "Incorrect password", Toast.LENGTH_SHORT).show();
}
}
});
}
});
To troubleshoot this issue, I've added logging to track the execution flow. It appears that on the second attempt, the passCheck function only executes up until the query.addListenerForSingleValueEvent stage.