Home > Enterprise >  Check if the user in the database firebase exists
Check if the user in the database firebase exists

Time:11-16

I would like to check if the user in the database exists. with this line of codes, it says that it exists and also that it does not exist. I want to make the code read-only if the name exists in one of the registers

enter image description here

Firebase database

private void Criar_Conta() {

        databaseReference_users.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {

                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                        if (snapshot.child("usuario").getValue().equals(Usuario.getText().toString()) && snapshot.getKey().equals(Usuario.getText().toString()) && snapshot.getKey().equals(snapshot.child("usuario").getValue())) {
                            Toast.makeText(Sign_Up.this, "Usuário Existente", Toast.LENGTH_SHORT).show();
                        } else {

                            Toast.makeText(Sign_Up.this, "Gravar ...", Toast.LENGTH_SHORT).show();
                            //Gravar_Dados();
                        }

                    }


                } else {

                    Gravar_Dados();

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

                Toast.makeText(Sign_Up.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
                Swipe.setRefreshing(true);
            }
        });

CodePudding user response:

This is my code sample I hope this helps you

  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) {

                    }
                });
            }
        }
    });

CodePudding user response:

To be able to check if a specific user exists in the database, you should only perform a get(), and not attach a ValueEventListener. Assuming that the users are direct children of your Firebase Realtime Database root, in code, will be as simple as:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
String usuario = Usuario.getText().toString();
DatabaseReference usuarioRef = db.child(usuario);
usuarioRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            if(snapshot.exists()) {
                Log.d("TAG", "User exists.");
            } else {
                Log.d("TAG", "User doesn't exist.");
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});
  • Related