firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.97k stars 916 forks source link

Hide the implied firebase admin-app created by Web Frameworks or allow this feature to be disabled #5874

Open Vochsel opened 1 year ago

Vochsel commented 1 year ago

Hey all, I appreciate that this isnt the most useful of repros, or context, I'll try add something later. But I wanted to post as this caused be a days worth of head scratching (partially due to some misleading firebase-admin reporting of token sources, but also due to accidentally updating a -g install of firebase-tools.

I believe the root cause to be some pseudo or automatic firebase-adming app being initialized which threw out my firebase admin initializeApp logic. But I'm still investigating. Reverting back to firebase-tools@11.28.0 seems to have helped.

[REQUIRED] Environment info

firebase-tools: firebase-tools@12.1.0 (Last known working version firebase-tools@11.28.0)

Platform: Windows

[REQUIRED] Test case

Any basic nextjs project with firebase and webFrameworks on.

[REQUIRED] Steps to reproduce

Basic firebase nextjs13 project with firebase auth and firebase-admin.

if (!firebaseAdmin.apps.length) {
    firebaseAdmin.initializeApp({
        credential: firebaseAdmin.credential.cert({
            privateKey: process.env.NEXT_PUBLIC_PRIVATE_KEY,
            clientEmail: process.env.NEXT_PUBLIC_CLIENT_EMAIL,
            projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
        }),
        databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
    });
}

[REQUIRED] Expected behavior

Initialize the default firebase app on the firebase function.

[REQUIRED] Actual behavior

Some pseudo firebase project named "firebase-frameworks" is already initialized, breaking the above. This initialized project also doesnt seem to work with firebase auth, and produced red-herring messages about token source.

MeTaNoV commented 1 year ago

I have the same issue when migrating firebase-tools, pretty annoying...

jamesdaniels commented 1 year ago

The web framework's integration leverages the firebase admin SDK in an attempt to keep your auth state in sync between the browser and the server. To work around this, look for your own admin app:

getApps().find(it => it.name === "[DEFAULT]" /* or your app's name */) || firebaseAdmin.initializeApp(...)

rather than checking just the length. I'll investigate if we could instead leverage a private API so it's not exposed to developers.

MeTaNoV commented 1 year ago

downgrading, but didn'T fixe the issue, @Vochsel did you remove any config generated in the way?

Vochsel commented 1 year ago

No, i did a clean wipe of temp dirs, downgraded, and seems to be happy again... Not confident it'll stay that way 😅

DhruvRaiPuri commented 1 year ago

IMHO, this is not a feature request. But is a bug.

I ran into this issue today and kept scratching my head but couldn't figure out what was initializing the firebase admin app before my code did.

Firebase admin is an important library for backend of any app built using Firebase. It is expected to work smoothly with Nextjs apps deployed on Firebase hosting as well. But, it doesn't. And this is a bug rather than a feature request.