Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Admin + Firestore Client API in a Cloud Function #612

Closed
lookfirst opened this issue Aug 7, 2019 · 4 comments
Closed

Cannot use Admin + Firestore Client API in a Cloud Function #612

lookfirst opened this issue Aug 7, 2019 · 4 comments

Comments

@lookfirst
Copy link

Related to: firebase/firebase-js-sdk#2054

I just went through my entire codebase and carefully made sure that I'm at the latest versions of all my google dependencies.

I fixed every single one of my imports to only import exactly what was needed. No more * as admin or * as firebase. Also no more import admin from 'firebase-admin'

All my imports look like this now:

import {https} from 'firebase-functions';
import {initializeApp as adminInitializeApp, credential, ServiceAccount} from 'firebase-admin'
import {initializeApp as firebaseInitializeApp} from "firebase";

I'm referencing database from my initialized app:

const FIREBASE_CONFIG = process.env.FIREBASE_CONFIG || {} as any;
const adminConfig = JSON.parse(FIREBASE_CONFIG);
adminConfig.credential = credential.cert(serviceAccount as ServiceAccount);
const firebaseClientApp = firebaseInitializeApp(clientAccount);
const firebaseAdminApp = adminInitializeApp(adminConfig);

firebaseAdminApp.database()...

I'm still getting this error...

>  FirebaseError: Firebase: Firebase service named 'database' already registered (app/duplicate-service).
>      at Object.registerService (/functions/node_modules/@firebase/app/dist/index.node.cjs.js:375:33)
>      at registerDatabase (/functions/node_modules/@firebase/database/dist/index.node.cjs.js:15248:39)
>      at Object.<anonymous> (/functions/node_modules/@firebase/database/dist/index.node.cjs.js:15271:5)
>      at Module._compile (internal/modules/cjs/loader.js:776:30)
>      at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
>      at Module.load (internal/modules/cjs/loader.js:643:32)
>      at Function.Module._load (internal/modules/cjs/loader.js:556:12)
>      at Module.require (internal/modules/cjs/loader.js:683:19)
>      at require (internal/modules/cjs/helpers.js:16:16)
>      at DatabaseService.getDatabase (/functions/node_modules/firebase-admin/lib/database/database.js:64:24) {
>    code: 'app/duplicate-service',
>    name: 'FirebaseError',
>    appName: 'database'
>  }

There is no way to use both Admin SDK and Firebase JS SDK in firebase cloud functions with this error.

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@lookfirst
Copy link
Author

lookfirst commented Aug 7, 2019

Switching my code to use the firebaseClientApp.databse() works, however this doesn't allow me any way to set serviceAccount level permissions, which is what I need in this case. The reason is that when I talk to the database, I have it locked down so that the user can only modify their own data. A service account allows me to bypass that.

The problem here is:

I have an app that uses pretty much every Firebase tech out there.

  • I use custom claims and the realtime database to notify users of updates to those claims.
  • I use a http endpoint to update the claims (writing to the realtime database from the adminsdk).
  • I use the firebase sdk to allow me to pass in a generated custom claim token in order to bypass auth controls.

The combination of those 3 features requires:

  • Access to database via a service account.
  • Access to admin sdk
  • Access to firebase sdk

My temporary workaround is to use the client sdk and then added a .write where the comparison uid is my hardcoded id passed in via the generated custom claim token.

@youngvz
Copy link

youngvz commented Aug 7, 2019

@lookfirst We are also using Firebase in that same capacity. Specifically with the custom claims and we are unable to run the Firebase shell with both Admin SDK and Firebase SDK.

@lookfirst
Copy link
Author

Closing this, see the discussion here: firebase/firebase-js-sdk#2054

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants