firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

Top-level service accessors are broken (TypeError: this.ensureApp(...).auth is not a function)

Open CodingDoug opened this issue 2 years ago • 4 comments
trafficstars

[READ] Step 1: Are you in the right place?

This is a bug report.

[REQUIRED] Step 2: Describe your environment

  • Operating System version: macOS Sonoma
  • Firebase SDK version: 11.11.0
  • Firebase Product: any
  • Node.js version: 18.18.0

[REQUIRED] Step 3: Describe the problem

The top-level service accessors (auth(), firestore()) provided by the firebase-admin don't work when provided an App object. However, the per-product accessors (getAuth(), getFirestore()) work fine.

Doesn't work:

import { auth, firestore } from "firebase-admin"
import { initializeApp } from "firebase-admin/app"

const firebase = initializeApp()
const authx = auth(firebase) // this fails
const firestorex = firestore(firebase) // also fails

// These do not fail when passed no args - they properly get the default app.

Full error when using accessors with an App instance looks like this:

/path/to/node_modules/firebase-admin/lib/app/firebase-namespace.js:136
            return this.ensureApp(app).firestore();
                                       ^
TypeError: this.ensureApp(...).firestore is not a function
    at fn (/path/to/scripts/node_modules/firebase-admin/lib/app/firebase-namespace.js:136:40)
    at <anonymous> (/path/to/scripts/src/bootstrap_emulator.ts:8:20)
    at Object.<anonymous> (path/to/scripts/src/bootstrap_emulator.ts:19:6)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Object.S (/path/to/scripts/node_modules/tsx/dist/cjs/index.cjs:1:1250)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v18.18.0

However, these do work using per-product accessors:

import { initializeApp } from "firebase-admin/app"
import { getAuth } from "firebase-admin/auth"
import { getFirestore } from "firebase-admin/firestore"

const firebase = initializeApp()
const authx = getAuth(firebase)
const firestorex = getFirestore(firebase)

CodingDoug avatar Oct 03 '23 17:10 CodingDoug

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Oct 03 '23 17:10 google-oss-bot

Having the same exact issue

skapoor8 avatar Jan 03 '24 20:01 skapoor8

@CodingDoug thanks for providing workaround. The code you provided seems to be namespace API (like in the docs). Judging by client library transitioning to modular api, the same accessors are used there. Supposing what we see is a half-done migration without the proper update of docs.

GubanovSergey avatar Jan 20 '24 11:01 GubanovSergey

` require("dotenv").config(); const admin = require("firebase-admin"); const { initializeApp } = require("firebase/app"); const { getAuth } = require("firebase/auth") const { onRequest } = require("firebase-functions/v2/https"); const { onObjectFinalized, onObjectMetadataUpdated } = require("firebase-functions/v2/storage"); const { getStorage, getMetadata, getDownloadURL, listAll, ref, updateMetadata, uploadBytes, uploadString, uploadBytesResumable } = require("firebase/storage");

let firebaseConfig = { apiKey: process.env.API_KEY, authDomain: process.env.AUTH_DOMAIN, projectId: process.env.PROJECT_ID, storageBucket: process.env.STORAGE_BUCKET, messagingSenderId: process.env.MESSAGING_SENDER_ID, appId: process.env.APP_ID, measurementId: process.env.MEASUREMENT_ID, auth: true, credential: admin.credential.applicationDefault(), };

initializeApp(firebaseConfig); // Initialize the Firebase app

let app = initializeApp(firebaseConfig); let storage = getStorage(app); let auth = getAuth(app);

module.exports = { app, auth, admin, ref, storage, listAll, getMetadata, getDownloadURL, uploadBytes, uploadString, updateMetadata, onRequest, onObjectFinalized, onObjectMetadataUpdated, uploadBytesResumable }; ` Error thrown:

TypeError: this.ensureApp(...).auth is not a function at FirebaseNamespace.fn (C:\Users\...\firebase-admin\lib\app\firebase-namespace.js:92:40) at new Auth (C:\Users\...\functions\controllers\AuthCT.js:15:28) at new Functions (C:\Users\...\functions\functions.js:16:24) at Object.<anonymous> (C:\Users\...\functions\index.js:2:27) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Module.require (node:internal/modules/cjs/loader:1235:19) at require (node:internal/modules/helpers:176:18)

Mod5ied avatar May 11 '24 17:05 Mod5ied