且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将EditText字符串与Firebase数据库的子值进行比较

更新时间:2023-01-21 13:24:01

好的伙伴.未调用您的 ValueEventListener onDataChange 的原因是,您无权编辑或读取数据库.发生这种情况是因为Firebase实时数据库的默认安全设置不允许未经身份验证的用户进行任何读取或写入.为了证明这一点,请在Android Monitor中观察您的 warning 日志.(蓝色的.)单击提交后,它将显示捕获的异常,提及Firebase拒绝的权限.

从.

这仍然是一个安全漏洞,因此要在您的应用中设置正确的身份验证,请按照以下步骤操作. https://firebase.google.com/docs/auth/android/start/

此外,像这样将toString()添加到 dataSnapshot.child("passcode").getValue(),以将其转换为String.

字符串passc = dataSnapshot.child("passcode").getValue().toString();

此外,请确保.如果尚未添加此行 FirebaseApp.initializeApp(this); 在你之上 mpasscode = FirebaseDatabase.getInstance().getReference().child("Councilpasscodes");

希望有帮助!

My app has a registration page wherein only those users who enter the right passcode are allowed to register. This passcode is stored inside firebase database inside a child called "Councilpasscodes". After entering the passcode the user should press the submit button, after comparing the two passcodes the user should be allowed to register if the passcodes are same. This is the code for comparing the edit text string and the child value:

mpasscode=FirebaseDatabase.getInstance().getReference().child("Councilpasscodes"); 
submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String passcode1=editText.getText().toString().trim();
                mpasscode.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String passc= (String) dataSnapshot.child("passcode").getValue();
                        if (passcode1.equals(passc)){
                            //do something


                        }else if(!passcode1.equals(passc)){
                            //do something

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

                    }
                });
            }
        });

The problem is after pressing the submit button it is not really checking the two strings i.e. if (passcode1.equals(passc)) and if(!passcode1.equals(passc)) is not being called even after entering the right passcode. I am not sure what is wrong with the code as I am new to app development.Any help is appreciated.Thank you.

EDIT: I think it is not executing addListenerForSingleValue at all. I am not sure why is this happening.

Alright mate. The reason why your ValueEventListener's onDataChange isn't getting called is that you're not authorized to edit or read your database. This happens because the default security settings for a firebase real-time database disallows any read or write without an authenticated user. To prove this observe your warning logs inside Android Monitor. (The blue ones.) It will show a caught exception mentioning permission denied from Firebase as soon as you click submit.

Read about Firebase security from here.

For a quick solution what you can do is, you can allow read or writes without any sort of authentication. To do that edit your Realtime database rules and add this instead of what's written there.

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

I'm also gonna attach a screenshot for reference. You'll find this screen inside the firebase console. Inside the console click on your project and then on Database to the left as shown.

.

This is a security lapse none the less, so to set up proper Authentication from your app, follow these steps. https://firebase.google.com/docs/auth/android/start/

Also, add a toString() to dataSnapshot.child("passcode").getValue() like this to convert it to String as it's probably long.

String passc = dataSnapshot.child("passcode").getValue().toString();

Also, just to be sure. Add this line if you haven't already FirebaseApp.initializeApp(this); above your mpasscode=FirebaseDatabase.getInstance().getReference().child("Councilpasscodes");

Hope it helps!