Firebase provides a bunch of features to use together in your app, provided by a project that you create at the Firebase console. Normally, it's sufficient to have all your app's resources provided by a single project, but there are times when you want a single app to be able to access data from multiple projects. For example, you may need to access data from two different databases, and be able to authenticate users to access each one. I'll show you how that's done in this post.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build();
google-services.json
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseOptions options = new FirebaseOptions.Builder() .setApplicationId("1:530266078999:android:481c4ecf3253701e") // Required for Analytics. .setApiKey("AIzaSyBRxOyIj5dJkKgAVPXRLYFkdZwh2Xxq51k") // Required for Auth. .setDatabaseUrl("https://project-1765055333176374514.firebaseio.com/") // Required for RTDB. .build(); FirebaseApp.initializeApp(this /* Context */, options, "secondary");
FirebaseApp
FirebaseDatabase.getInstance()
// Retrieve my other app. FirebaseApp app = FirebaseApp.getInstance("secondary"); // Get the database for the other app. FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);
.requestIdToken(getString(R.string.default_web_client_id))
{ "client_id": "56865680640-e8mr503bun5eaevqctn4u807q4hpi44s.apps.googleusercontent.com", "client_type": 3 },
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); FirebaseAuth.getInstance().signInWithCredential(credential); FirebaseApp app = FirebaseApp.getInstance("secondary"); FirebaseAuth.getInstance(app).signInWithCredential(credential);
Default Auth UID: 0960868722032022577213DA4EA8B7A1683D92B405DD Secondary Auth UID: 7h6XOeSxmkNsSseFJ1jU31WZHDP2
firebaseAuth.getCurrentUser().getToken(false /* forceRefresh */) .addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { String token = task.getResult().getToken(); // Send this to the server. } });
FirebaseOptions options = new FirebaseOptions.Builder() .setServiceAccount(new FileInputStream("default-service-account.json")) .build(); FirebaseApp.initializeApp(options); FirebaseOptions secondaryOptions = new FirebaseOptions.Builder() .setServiceAccount(new FileInputStream("secondary-service-account.json")) .build(); FirebaseApp.initializeApp(secondaryOptions, "secondary");
// Verify the ID token using the default app. FirebaseAuth.getInstance().verifyIdToken(idToken) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(FirebaseToken decodedToken) { String uid = decodedToken.getUid(); System.out.println("User " + uid + " verified"); FirebaseApp app = FirebaseApp.getInstance("secondary"); String customToken = FirebaseAuth.getInstance(app).createCustomToken(uid); // TODO: Send the token back to the client! } });
FirebaseApp app = FirebaseApp.getInstance("secondary"); FirebaseAuth.getInstance(app).signInWithCustomToken(token);
Default Auth UID: 0960868722032022577213DA4EA8B7A1683D92B405DD Secondary Auth UID: 0960868722032022577213DA4EA8B7A1683D92B405DD
var config = { apiKey: "", authDomain: ".firebaseapp.com", databaseURL: "https://.firebaseio.com", storageBucket: ".appspot.com", messagingSenderId: "", }; var secondary = firebase.initializeApp(otherAppConfig, "secondary"); var secondaryDatabase = secondary.database();
// Alt: load from plist using |FIROptions(contentsOfFile:)| let options = FIROptions(googleAppID: googleAppID, bundleID: bundleID, GCMSenderID: GCMSenderID, APIKey: nil, clientID: nil, trackingID: nil, androidClientID: nil, databaseURL: databaseURL, storageBucket: nil, deepLinkURLScheme: nil) FIRApp.configure(withName: "secondary", options: fileopts) guard let secondary = FIRApp.init(named: "secondary") else { assert(false, "Could not retrieve secondary app") } let secondaryDatabase = FIRDatabase.database(app: secondary);