Skip to content

firebase invaild apii key #7

@shadil-rayyan

Description

@shadil-rayyan

Title:* App crashes on npm run dev if Firebase API key is missing – Firebase should only initialize when needed

Description:

When cloning the project and running npm run dev without a .env file or Firebase API key, the app immediately crashes due to an unconditional call to getAuth() from Firebase. This occurs even in parts of the app that do not require Firebase, which should not happen.

Expected Behavior:

Firebase should only be initialized when explicitly required. The app should not crash during local development just because the .env is missing or Firebase is not needed for the current route/component.

Steps to Reproduce:

  1. Clone the project.

  2. Do not create a .env file or add Firebase credentials.

  3. Run:

    npm install
    npm run dev
  4. Observe the crash caused by getAuth() being invoked without a valid Firebase config.

Environment:

  • Next.js: 15.1.4
  • Firebase SDK: 11.3.1
  • Node.js: 23.10.0

Error Cause:
Calling getAuth() or initializing Firebase without checking for required config values like apiKey leads to a crash.

Suggested Fix:
Ensure Firebase is only initialized when its config is present. For example:

let auth;

if (process.env.NEXT_PUBLIC_FIREBASE_API_KEY) {
  const app = initializeApp(firebaseConfig);
  auth = getAuth(app);
}

Or lazy-load Firebase only where it’s actually needed, rather than globally or unconditionally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions