reactjsreact-nativefirebase-authenticationexpoeas

Expo build crash. FirebaseError: Firebase: Error (auth/invalid-api-key)


Expo apk build crash after show splashscreen. Connecting my device to logcat of android studio i see the log that say
"Fatal Exeption: mqt_native_modules"
com.facebook.react.common.JavascriptException: FirebaseError: Firebase: Error (auth/invalid-api-key).
Im using "firebase": "^9.9.3" (sdk)

The values of api-key are in .env file, then in mi app.config.js expo -> extra ->

      "apiKey": process.env.APIKEY,
      "authDomain": process.env.AUTHDOMAIN,
      "projectId": process.env.PROJECTID,
      "storageBucket": process.env.STORAGEBUCKET,
      "messagingSenderId": process.env.MESSAGINGSENDERID,
      "appId": process.env.APPID,
      "measurementId": process.env.MEASUREMENTID

When i run with expo go all works nice, no crash. I made a development build and test with emulator and works too.

When crash logcat dont execute the console.log(firebaseConfig) so i cant see the values.
I also thought if the problem was the credentials in expo.dev (FCM V1 service account key) but I tried modifying and updating the values ​​in the .env but it doesn't work either.

My firebase.js

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

import Constants from "expo-constants";

// https://firebase.google.com/docs/web/setup#available-libraries

// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: Constants.expoConfig.extra.apiKey || '',
  authDomain: Constants.expoConfig.extra.authDomain || '',
  projectId: Constants.expoConfig.extra.projectId || '',
  storageBucket: Constants.expoConfig.extra.storageBucket || '',
  messagingSenderId: Constants.expoConfig.extra.messagingSenderId || '',
  appId: Constants.expoConfig.extra.appId || '',
  measurementId: Constants.expoConfig.extra.measurementId || ''
};

// Initialize Firebase
console.log(firebaseConfig);
const app = initializeApp(firebaseConfig);

// Initialize Firebase services
const database = getFirestore(app);
const auth = getAuth(app);

const firebaseServices = { database, auth };

export default firebaseServices;

My .env (is in root)

URL_BACK=xxx
APIKEY=xxx
AUTHDOMAIN=xxx
PROJECTID=xxx
STORAGEBUCKET=xxx
MESSAGINGSENDERID=xxx
APPID=xxx
MEASUREMENTID=xx

Here is my eas.json

{
  "build": {
    "development": {
      "android": {
        "buildType": "apk",
        "developmentClient": true,
        "gradleCommand": ":app:assembleDebug"
      }
    },
    "preview": {
      "android": {
        "buildType": "apk"
      }
    },
    "preview2": {
      "android": {
        "gradleCommand": ":app:assembleRelease"
      }
    },
    "production": {
      "android": {
        "buildType": "apk"
      }
    }
  }
}

Solution

  • I finally deleted the web app in Firebase and created a new one and Instead of taking the data from the .env, I hardcoded it in the firebase.js, and it worked.