So I want to log in with a certain role where I try to login with the user role, but it always goes into the else condition, because snapshot.child(uid).child("role").equals("user")
this condition always false. Is there any solution to this problem?
and this is my code :
package com.example.finalproject2;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class LoginScreenUser extends AppCompatActivity implements View.OnClickListener{
private TextView phone,pass;
private Button button;
private ProgressBar progressBar;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
phone = (TextView) findViewById(R.id.nomorTelp);
pass = (TextView) findViewById(R.id.passUser);
button = (Button) findViewById(R.id.loginUser);
progressBar = (ProgressBar) findViewById(R.id.loads);
button.setOnClickListener(this);
progressBar.setVisibility(View.GONE);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.loginUser:
userlogin();
}
}
private void userlogin() {
String nomor = phone.getText().toString().trim();
String password = pass.getText().toString().trim();
// progressBar.setVisibility(View.VISIBLE);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference DatabaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference.child("Users").orderByChild("role").equalTo("user").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.child(nomor).exists() && snapshot.child(password).exists() && snapshot.child(uid).child("role").equals("user")){
Toast.makeText(LoginScreenUser.this,"Login Succes",Toast.LENGTH_SHORT).show();
back();
}else{
Toast.makeText(LoginScreenUser.this,"Data Belum Terdaftar",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void back() {
startActivity(new Intent(this,MainActivity.class));
}
}
2
Answers
I have done this. I think this helps you.
When you’re using:
It basically means that you try to check if the full URL for the above reference is equal to "user". Which obviously will always return false since that URL can never be equal to the "user" String. The toString() method of the DatabaseReference class returns:
To solve this problem, you have to get the value that corresponds to the
role
field as a String: