Why doesn't Firebase admin auth in node.js use ADC?
Asked Answered
M

0

6

Does anyone know why Firebase admin auth in node.js doesn't use ADC (Application Default Credentials)? I always have to set GOOGLE_APPLICATION_CREDENTIALS to a credentials file to get auth to work. Everything else (firestore, compute, storage etc.) works fine with ADC.

For instance, this code works only when GOOGLE_APPLICATION_CREDENTIALS is set to a valid credentials file, even though I'm logged into my Firebase project and my gcloud project:

import * as admin from 'firebase-admin'
admin.initializeApp()

async function listAllUsers(users: any[], matchRegex: RegExp, nextPageToken?: string) {
  // List batch of users, 1000 at a time.
  const listUsersResult = await admin.auth().listUsers(1000, nextPageToken)
    .catch(function (error) {
      console.log('Error listing users:', error);
    });
  if (listUsersResult) {
    listUsersResult.users.forEach(function (userRecord) {
      if (matchRegex.test(userRecord.email || '<none>') ||
          matchRegex.test(userRecord.displayName || '<none>') ||
          matchRegex.test(userRecord.uid))
        users.push(userRecord.toJSON())
    });
    if (listUsersResult.pageToken) {
      // List next batch of users.
      console.log(`next batch...`)
      listAllUsers(users, matchRegex, listUsersResult.pageToken);
    }
  }
}

If that env var is not set, I get this error:

Error listing users: FirebaseAuthError: Failed to determine project ID for Auth. 
Initialize the SDK with service account credentials or set project ID as an app option. 
Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.

But setting GOOGLE_CLOUD_PROJECT is not enough either. When I do that, I get:

Error listing users: FirebaseAuthError: //cloud.google.com/docs/authentication/. Raw server response: "{"error":{"code":403,"message":"Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the identitytoolkit.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.","errors":[{"message":"Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the identitytoolkit.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.","domain":"usageLimits","reason":"accessNotConfigured","extendedHelp":"https://console.developers.google.com"}],"status":"PERMISSION_DENIED"}}"
    at FirebaseAuthError.FirebaseError [as constructor] (/c/dss/Product/Horizon/horizon/packages/renderer/node_modules/firebase-admin/lib/utils/error.js:43:28)
    at FirebaseAuthError.PrefixedFirebaseError [as constructor] (/c/dss/Product/Horizon/horizon/packages/renderer/node_modules/firebase-admin/lib/utils/error.js:89:28)
    at new FirebaseAuthError (/c/dss/Product/Horizon/horizon/packages/renderer/node_modules/firebase-admin/lib/utils/error.js:148:16)
    at Function.FirebaseAuthError.fromServerError (/c/dss/Product/Horizon/horizon/packages/renderer/node_modules/firebase-admin/lib/utils/error.js:187:16)
    at /c/dss/Product/Horizon/horizon/packages/renderer/node_modules/firebase-admin/lib/auth/auth-api-request.js:1490:49

As I said though, all other Firebase admin features seem to work fine with ADC; they automatically pick up the current project and my logged in account.

Mariehamn answered 9/2, 2021 at 15:31 Comment(2)
This is on my local dev machine, by the way, logged into gcloud and firebase.Mariehamn
any progress with this? 😅 I'm having the same issue here...Yore

© 2022 - 2024 — McMap. All rights reserved.