[go: nahoru, domu]

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

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

Open
CodingDoug opened this issue Oct 3, 2023 · 4 comments

Comments

@CodingDoug
Copy link
CodingDoug commented Oct 3, 2023

[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)
@google-oss-bot
Copy link

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

@skapoor8
Copy link
skapoor8 commented Jan 3, 2024

Having the same exact issue

@GubanovSergey
Copy link

@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
Copy link
Mod5ied commented May 11, 2024

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

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

6 participants