firebase


Firebase Cloud Function config to access other project db


I need to create a Cloud Function that will access the Firebase DB that is running in another project. If it was accessing the db in the current project, I could use code such as
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
however, what I want is for functions.config().firebase to return the information (and, most importantly, credentials) for the other project. Is there any easy way to do this?
functions.config().firebase is a reserved namespace and you won't be able to override it. However, you can do cross-project initialization yourself. Here's how I would do it:
First, download a service account for your other project into your functions directory. Name it <project_id>-sa.json. Next, set up some environment config (app.fb_project_id is just an example name, not a requirement):
firebase functions:config:set app.fb_project_id="<the_project_id>"
Now in your code, you can initialize the Admin SDK like so:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
if (!functions.config().app || !functions.config().app.project_id) {
throw new Error('Cannot start app without app.fb_project_id config.');
}
const FB_PROJECT_ID = functions.config().app.fb_project_id;
const SERVICE_ACCOUNT = require(`./${FB_PROJECT_ID}-sa.json`);
admin.initializeApp({
databaseURL: `https://${FB_PROJECT_ID}.firebaseio.com`,
credential: admin.credential.cert(SERVICE_ACCOUNT)
});
This will have initialized the Admin SDK for the other project.

Related Links

Calling sendPasswordResetEmail() within Cloud Functions for Firebase
Firebase hosting Access-Control-Allow-Origin for app engine URL
Firebase Web Authentication - Administrator Approval for New Accounts
Amazon Alexa Error “The response is invalid”
Firebase Hosting Deleted Deployments Taking Up Space
Can Firebase be used without clients logging in?
Firebase promise returns nonsense object
Register users in Firebase with more than 2 credentials
Get Uri of my Image bitmap in imageview container 1
Firebase rules read/write and security
firebase storage cors strange Behaviour
Do I need a paid plan for using recaptcha on firebase function?
Database rule in Firebase: Can we setting each rule for each application in the same project? [duplicate]
Cloud Functions for Firebase - Is this similar to “Cloud Code” in parse?
Is there a way to get a webhook called on every Firebase Realtime DB transaction?
cannot set google-service.json's build action in xamarin studio [duplicate]

Categories

HOME
rust
weblogic12c
blast
xcode8.1
softlayer
swi-prolog
activemq
tfs2015
material-components
linker
impala
angular-meteor
gtk
scrape
fireloop
slide
documentum
bnf
pywinauto
http-authentication
jest
angular2-highcharts
neo4j.rb
quantitative-finance
high-availability
strophe.js
marquee
tortoisehg
jsonresult
credit-card
google-play-developer-api
john-the-ripper
seafile-server
quickcheck
cloveretl
payeezy
spring-rabbitmq
testlink
spinner
nsuserdefaults
love2d
mars-simulator
best-buy-api
common-table-expression
deployd
roracle
ol3-google-maps
algebra
dtd
rustdoc
fitbit
nclam
jtag
perforce-integrate
node-glob
fdt
accessibilityservice
currency-exchange-rates
wepay
msgpack
opencyc
mousehover
pythonxy
gql
supersocket.net
artisan
phpredis
subfolder
mathematical-expressions
mxe
maven-shade-plugin
thoughtworks-go
vs-unit-testing-framework
visual-c++-2012
xulrunner
iosched
xs
unicoins
2d-vector
dojo-1.9
jsplitpane
gitx
border-box
msinfo32
opends
snackjs
tablet-pc
mongrel2
principles
raw-data
jquery-effects
stretchblt
prism-2
suggestbox
remote-working

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App