2

I've connected two Databases from the same firebase project like below.

    //init the default db;
    FirebaseApp.initializeApp(this);

    //init the second db;
    FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
    builder.setApplicationId("id"); //Same as default
    builder.setApiKey("key"); //Same as default 
    builder.setDatabaseUrl("https://second-db.firebaseio.com");

    try {
        FirebaseApp.initializeApp(this, builder.build(), "second-db");
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

Getting Reference.

public static DatabaseReference getSecondDbRef(String child) {
    FirebaseApp app = FirebaseApp.getInstance("second-db");
    return FirebaseDatabase.getInstance(app).getReference(child);
}

With the above, it works, but the rules in the second database don't seem to be getting auth. if I do.

 {
   "rules":{
          "users":{
                 "$uid":{
                       ".read":"auth.uid !== null",
                       // ".read":true // This works, so the issue is auth
                  }
          }
   }
}

The read fails, even though the user is authenticated, also, this problem isn't there with the default database. Is there anything I'm missing with the init?

Ok, because of highlights below, here's the code as it is on the app.

    private static final String  TEAMS_BD_ID   = "second-db"

    public void initFirebase() {
        FirebaseApp.initializeApp(this);

        FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
        builder.setApplicationId("appId"); // //Same as default
        builder.setApiKey("key"); // //Same as default
        builder.setDatabaseUrl("https://teams-db.firebaseio.com");//Second Db

        try {
            FirebaseApp.initializeApp(this, builder.build(), TEAMS_BD_ID);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public static FirebaseApp getTeamsApp() {
        return FirebaseApp.getInstance(TEAMS_BD_ID);
    }

    public static String getTeamsUserUid() {
        return FirebaseAuth.getInstance(getTeamsApp()).getCurrentUser().getUid();
    }

    public static DatabaseReference getTeamsRef(String child) {
        return FirebaseDatabase.getInstance(getTeamsApp()).getReference(child);
    }

    public static DatabaseReference getTeamsRef() {
        return FirebaseDatabase.getInstance(getTeamsApp()).getReference();
    }

Then trying to read.

DatabaseReference r = getTeamsRef().child("users/" + getTeamsUserUid() + "/teams") 
// getTeamsUserUid()  This is null.
Relm
  • 7,923
  • 18
  • 66
  • 113

1 Answers1

1

It is only a guess.

If you are using FirebaseAuth#getInstance() for getting auth instance, change it to FirebaseAuth#getInstance(FirebaseApp firebaseApp) with the second app instance.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
  • Second app is doing exactly that on the question. `FirebaseDatabase.getInstance(app)` – Relm Aug 26 '18 at 14:28
  • Hi Relm, I am talking about the auth instance you access when the time of authentication. Not about the app instance. Did you check it? – Bertram Gilfoyle Aug 26 '18 at 16:42
  • Ok, thanks for Answering, You might be up to something. So `FirebaseAuth.getInstance('second-app').getUid()` and FirebaseAuth.getInstance('second-app').getCurrentUser()` are `null`, any ideas why this might be? – Relm Aug 26 '18 at 17:22
  • You are welcome. But there were no `second-app` before. It was `second-db`, right? – Bertram Gilfoyle Aug 26 '18 at 17:26
  • Also how could you make `FirebaseAuth.getInstance('second-app').getUid()` run? That method accepts parameter of type `FirebaseApp`. So it should be `FirebaseAuth.getInstance(FirebaseApp.getInstance("second-db")).getUid()` or something – Bertram Gilfoyle Aug 26 '18 at 17:32
  • I assume you changed `second-db` to `second-app`. If so, it was a good move. Also the wrong type of argument may be only in the comment and you may have done it correctly on code. – Bertram Gilfoyle Aug 26 '18 at 17:35
  • That's a typo, "second-app", should be `app`, as in `FirebaseAuth.getInstance(FirebaseApp.getInstance("second-db"))`. – Relm Aug 26 '18 at 17:39
  • Finally, for `FirebaseAuth.getInstance(FirebaseApp.getInstance("second-db")).getCurrentUser()` to return a non-null object, you need to use that instance for the authentication process. I think you are still using the first instance there. – Bertram Gilfoyle Aug 26 '18 at 17:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178807/discussion-between-anees-and-relm). – Bertram Gilfoyle Aug 26 '18 at 17:41
  • Ok, also I edited my question to reflect the changes after you suggestion. – Relm Aug 26 '18 at 17:46