firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.62k stars 369 forks source link

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

Open CodingDoug opened 1 year ago

CodingDoug commented 1 year ago

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

This is a bug report.

[REQUIRED] Step 2: Describe your environment

[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)
google-oss-bot commented 1 year ago

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

skapoor8 commented 9 months ago

Having the same exact issue

GubanovSergey commented 8 months ago

@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.

Mod5ied commented 4 months ago

` 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)