How to receive data using Firebase

4

I made a small test app using FireBase.

Everything works fine 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 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 writing in the simulator of the firebase itself the rules work perfectly. So I guess I'm doing something wrong with reading.

Login is working perfectly using FirebaseAuth , I get confirmation by FirebaseAuth.AuthStateListener , but I do not know how to "connect" 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 get 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 should be letting something extremely simple go unnoticed, but I have no idea how to get the data now that I'm requiring a login, since the Firebase reference firebase.getAuth() is always null, and the data arrives by

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'
}
    
asked by anonymous 13.07.2016 / 23:18

2 answers

1

The problem was trying to use an instance of the old Firebase library to try to receive server notifications. It is still functional on the Firebase server, but only applications that already existed before the last firebase update. New applications should use the new DatabaseReference library, which works 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+"");
            }
        }
    });
    
17.01.2017 / 19:41
0

I think you're not using the new android sdk as the error suggests.

If so, you should upgrade your gradient view the documentation

To use each feature of the new firebase you must add each dependency separately, here's 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 (samples firebase) you can find firebase examples to log in with the new SDK

    
15.07.2016 / 15:24