How to receive data using Firebase

Asked

Viewed 1,854 times

4

I made a small test app using Firebase.

Everything works great when I leave the rules for anyone to have access to the database

".read":"true",
".write":"true"

But when I added permissions in the Rules to limit access from where each user can read, I can no longer read anything in the apk.

{
  "rules":
  {
    "comandos":
    {
      "$unit_id":
      {
        ".read":"root.child('unidades').child($unit_id).hasChild(auth.uid)",
        ".write":"root.child('unidades').child($unit_id).hasChild(auth.uid)"
      }
    }
  }
}

When I simulate reading and recording in the firebase simulator the rules work perfectly. So I guess I’m doing something wrong with the reading.

The login is working perfectly using the FirebaseAuth, I receive confirmation by FirebaseAuth.AuthStateListener, but I don’t know how to "turn on" this login to my Firebase reference. And when I try to login using

mRef = new Firebase(url);
mRef.authWithPassword(email, pass, new Firebase.AuthResultHandler(){...});

I receive the message that I should use FirebaseAuth,

Projects created at console.firebase.google.com must use the 
new Firebase Authentication SDKs available from firebase.google.com/docs/auth/

I must be letting something extremely simple go unnoticed, but I have no idea how to receive the data now that I am requiring a login, since the Firebase reference firebase.getAuth() always this as null, and the data arrive by the method

firebase.addChildEventListener(new ChildEventListener(){...});

@Edit

//build.gradle
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.firebase:firebase-client-android:2.3.1'
    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-messaging:9.0.2'
    compile 'com.google.firebase:firebase-auth:9.2.0'
}
  • Put your Radle on please

  • @christian-beregula its understood well, its problem is in knowing which is the Liener that is listening the status of the authentication?

2 answers

1


The problem was trying to use an instance of the old Firebase to try to receive notifications from the server. It is still functional on the Firebase server, but only applications that existed before the last firebase update. New applications should use the new library DatabaseReference, that work together with the new login library FirebaseAuth.

//declaração
FirebaseAuth login = FirebaseAuth.getInstance();
DatabaseReference db = FirebaseDatabase.getInstance().getReferenceFromUrl("url");

//função para realizar o login
login.signInWithEmailAndPassword("email", "senha").
    addOnCompleteListener(new OnCompleteListener<AuthResult>()
    {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task)
        {
            try
            {
                if (task.isSuccessful())
                {
                    Log.e("Firebase", "Conectado");

                    //receber atualizações push...
                    db.addChildEventListener(new ChildEventListener()
                    {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s)
                        {

                        }

                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s)
                        {

                        }

                        @Override
                        public void onChildRemoved(DataSnapshot dataSnapshot)
                        {

                        }

                        @Override
                        public void onChildMoved(DataSnapshot dataSnapshot, String s)
                        {

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError)
                        {

                        }
                    });

                }
                else
                {
                    Exception e = task.getException();
                    if(e != null)
                    {                       
                        Log.e("Erro login", e+"\n"+task.getResult().toString());
                    }
                    Log.e("Firebase", "Erro de autenticação");
                }
            }
            catch (Exception e)
            {
                Log.e("OnCoplete Erro", e+"");
            }
        }
    });

0

I believe you are not using the new android sdk as suggested error.

If so you should upgrade your Radle see the documentation

To use each new firebase feature you must add each department separately, here is the list:

com.google.firebase:firebase-core:9.0.0 Analytics
com.google.firebase:firebase-database:9.0.0 Realtime Database
com.google.firebase:firebase-storage:9.0.0  Storage
com.google.firebase:firebase-crash:9.0.0    Crash Reporting
com.google.firebase:firebase-auth:9.0.0 Authentication
com.google.firebase:firebase-messaging:9.0.0    Cloud Messaging / Notifications
com.google.firebase:firebase-config:9.0.0   Remote Config
com.google.firebase:firebase-invites:9.0.0  Invites / Dynamic Links
com.google.firebase:firebase-ads:9.0.0  AdMob
com.google.android.gms:play-services-appindexing:9.0.0  App Indexing

Here(firebase samples) you can find examples of firebase itself to log in with the new SDK

  • The login I can perform without problems, but I do not know how to receive the data from the server. I’m doing something wrong, I imagine it’s related to using a Firebase instance for this.

  • Use Firebaseuth as you commented on your question. your login method should look like on line 157: https://github.com/firebase/quickstart-android/blob/master/auth/app/src/main/java/com/google/firebase/quickstart/auth/mailPasswordActivity.java

  • So, as I said, the problem is not in the login, I have the confirmation that was made, both by the Switch and the firebase console. What I don’t know what to do is a method equivalent to firebase.addChildEventListener using the login of FirebaseAuth

Browser other questions tagged

You are not signed in. Login or sign up in order to post.