检查数据库firebase中的用户是否存在

vq8itlhq  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(124)

我想检查数据库中的用户是否存在。对于这行代码,它表示它存在,也表示它不存在。如果名称存在于某个寄存器中,我想将代码设置为只读
enter image description here
Firebase数据库

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);
            }
        });
flvtvl50

flvtvl501#

这是我的代码示例,希望对您有所帮助

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

                    }
                });
            }
        }
    });
r6vfmomb

r6vfmomb2#

为了能够检查数据库中是否存在特定用户,您应该只执行get(),而不附加ValueEventListener。假设用户是Firebase实时数据库根目录的直接子级,在代码中,将非常简单:

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!
        }
    }
});

相关问题