expoeas

What can go wrong with Expo Secrets?


In my Expo account, I have a secret called EXPO_BUILD_FIREBASE_API_KEY. My code to access the secret

const firebaseConfig = {
  apiKey: "hard-coded-api-key", //works in testflight (obviously)
  // process.env.EXPO_BUILD_FIREBASE_API_KEY ?? //doesn't work in testflight
  // process.env.EXPO_PUBLIC_FIREBASE_API_KEY, //works locally and comes .env

In my Expo Build I see

Environment secrets:

  EXPO_BUILD_FIREBASE_API_KEY=********

I double-checked that EXPO_BUILD_FIREBASE_API_KEY contains the correct value, however, in testflight firebase claims that the API key is invalid unless I use the hard-coded key. I am using it as mentioned in the docs.

What could be the reason for this? In .js files, I place the API key in quotation marks ("") while in .env I don't. My API key contains at least one "-". Do I need to add escape signs for these? Do I need quotation marks?


Solution

  • This took me quite a while to figure it out, because it was not clearly enough mentioned in the docs for my taste. I added a app.config.js with this content:

    //Takes all other values from app.json
    module.exports = ({ config }) => {
      const fullConfig = {
        ...config,
        extra: {
          ...config.extra, //add this to not overwrite values from app.json
          firebaseApiKey:
            process.env.EXPO_BUILD_FIREBASE_API_KEY ?? //from expo build secrets
            process.env.EXPO_FIREBASE_API_KEY, //from my local .env file
      };
    
      return {
        ...fullConfig,
      };
    };
    

    I am accessing the api key files now via

    import Constants from "expo-constants";
    ...
    
    const firebaseConfig = {
      apiKey: Constants.expoConfig.extra.firebaseApiKey,
    ...
    

    What I learned: