what I want to do
- I have a firebase node as shown here:
- my user will input a string(name), after that I will check if the (name)or(userinput) is available in the firebase by this code
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("users");
ref.orderByChild("username").equalTo(userinputname).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exists()) {
//username exist
Toast.makeText(LoginActivity.this, "exists", Toast.LENGTH_SHORT).show();
}
}
If it is available then I want to get other data from that specific node in which the (name) is present..
simplified - my user will input a username and pass, I have multiple username and pass in my DB so i want to first check if the username exists in DB if yes then i want the Pass which is in the DB to match it with the Password which is entered by the user to login
eg - if my user inputs username "Admin" which is avaiable in my DB, so I want to get the pass which is in that node which is "Admin123" and later I will check if the pass from DB matches the pass which is entered by the user to log him in the app
CodePudding user response:
If my user inputs username "Admin" which is available in my DB, I want to get the pass that is in that node which is "Admin123" and later I will check if the pass from DB matches the pass which is entered by the user to log him in the app.
To be able to solve this problem, first, you have to perform a query that gets all the users where the value of the username
field matches what the user types in the userinputname
EditText. If the query returns at least a record, then you have to check the value of the pass
field against the value userinputpass
EditText. If there is a match, you can go ahead with the authentication, otherwise display a message, or do whatever you want.
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = db.child("users");
Query queryByUserName = usersRef.orderByChild("username").equalTo(userinputname);
queryByUserName.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot userSnapshot : task.getResult().getChildren()) {
String pass = userSnapshot.child("pass").getValue(String.class);
if(pass.equals(userinputpass)) {
Log.d("TAG", "Username and password are correct.");
} else {
Log.d("TAG", "The password is incorrect");
}
}
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});
In the solution above, I have used Query#get() and not Query()#addListenerForSingleValueEvent(). Why? Is because of this.
While the above solution will work, please note that you should never store the user credentials in the way you do. Never! Storing passwords in cleartext is one of the worst security risks you can inflict on your users. So for avoiding that, I highly recommend you implement Firebase Authentication.
Since you're using Java, I recommend you read the following article:
If you consider at some point in time learning Kotlin, which I personally recommend, then please also check the following resources:
And:
Edit:
The above query requires an index. So in order to make it work, please add the following rules inside your Firebase console:
{
"rules": {
"users": {
".indexOn": "username"
}
}
}