0

I have two Firebase projects, A and B. When a user follows another user on Project A, data is written to /followers/{followedUid}/{followerUid}. I would like to perform this same update to Project B.

Users of Project A aren't (can't be) authorized to write to Project B, so I need to use a cloud function. I already do things like this with Node.js functions and firebase-admin using service accounts, but here I want to use a function deployed to Firebase.

How can I add or reference a service account, or otherwise authorize a second project in a Firebase cloud function?

Ken
  • 212
  • 3
  • 13

1 Answers1

3

If you've already done this with a local Node.js process and service account, the process within Cloud Functions is going to be pretty similar.

You'll need to initialize the Admin SDK with the credentials (in the form of a JSON file that you deploy with the code) for project B, and then use that for writing the data.

Also see the Firebase documentation on initializing the Admin SDK.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • yes, but how? In my Node functions I can put something like, const secondaryServiceAccount = require('/path/to/service/account') Where would the service account be located in this case? Thanks. – Ken Nov 29 '21 at 18:38
  • 2
    You'd deploy that credentials file with your Cloud Functions, by just putting the file in the same directory. Then you can load it with `require(./projectB-svcact.json'). – Frank van Puffelen Nov 29 '21 at 18:39
  • Sounds good, I'll try it! Obvious, I guess... thanks again – Ken Nov 29 '21 at 18:40