skip to Main Content

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?

this is my database structure

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


  1. loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                final String phoneTxt = phone.getText().toString();
                final String passwordTxt = password.getText().toString();
    
                if (phoneTxt.isEmpty() || passwordTxt.isEmpty()){
                    Toast.makeText(Login.this, "Please Enter Your Phone Number Or Password", Toast.LENGTH_SHORT).show();
                }else {
    
                    databaseReference.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            if (snapshot.hasChild(phoneTxt)){
                                final String getpassword = snapshot.child(phoneTxt).child("password").getValue(String.class);
    
                                if (getpassword.equals(passwordTxt)){
                                    Toast.makeText(Login.this, "Successfully login! ", Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(Login.this,HomeScreen.class));
                                    finish();
                                }
                                else{
                                    Toast.makeText(Login.this, "Wrong Password", Toast.LENGTH_SHORT).show();
                                }
                            }
                            else {
                                Toast.makeText(Login.this, "Wrong Phone Number!", Toast.LENGTH_SHORT).show();
                            }
                        }
    
                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {
    
                        }
                    });
                }
            }
        });
    

    I have done this. I think this helps you.

    Login or Signup to reply.
  2. When you’re using:

    snapshot.child(uid).child("role").equals("user")
    

    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:

    The full location URL for this reference.

    To solve this problem, you have to get the value that corresponds to the role field as a String:

    snapshot.child(uid).child("role").getValue(String.class).equals("user")
    //                                 👆
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search