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.