Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up dotenv in firebase functions

I am trying to move the tiny node-express app I made into firebase functions.

The file have dotenv variables. Earlier I thought If I just deploy and put dotenv in dependency, It will work but that didn't happen so..

So, I went to environment configuration article of firebase to understand how I can set .env

Which states to set things by doing something like this

firebase functions:config:set someservice.key="THE API KEY" someservice.id="THE CLIENT ID"

But I have so many environment configuration and doing that some what seems to be cumbersome task.

So let's say this is environment file

# App port Address
PORT = 8080

# Google Secret 
GOOGLE_CALLBACK_URL =   http://localhost:8080/auth/google/callback
GOOGLE_CLIENT_ID = 4048108-bssbfjohpu69vl6jhpgs1ne0.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET = lTQHpjzY57oQpO

# Client Address 
CLIENT_ADDRESS = http://localhost:3000/


# Meetup Secret 
MEETUP_CALLBACK_URL = http://localhost:8080/auth/meetup/callback
MEETUP_CLIENT_ID = ef6i9f7m6k0jp33m9olgt
MEETUP_CLIENT_SECRET = sk3t5lnss2sdl1kgnt

#EventBrite Secret 
EVENTBRITE_CALLBACK_URL = http://localhost:8080/auth/eventbrite/callback
EVENTBRITE_CLIENT_ID = UU2FXKGYHJRNHLN
EVENTBRITE_CLIENT_SECRET = NA55QG52FAOF6GDMLKSJBKYOPIGQU4R46HHEU4 

How Can I best set up so that when I do firebase firebase serve --only functions,hosting it doesn't throw any errors such as

OAuth2Strategy requires a clientID option

like image 858
iRohitBhatia Avatar asked Jan 22 '19 17:01

iRohitBhatia


2 Answers

As of Feb 16, 2022 Firebase now supports .env, .env.prod, .env.dev files natively!

https://firebase.google.com/docs/functions/config-env

Set your variables in the corresponding environment, and then run firebase use dev or firebase use prod before you deploy.

Your variables can be accessed via process.env.VARIABLE_NAME

like image 183
d-_-b Avatar answered Sep 20 '22 00:09

d-_-b


UPDATED 2019-06-04

I'm very sorry. This solution is wrong.

I found the correct way.

https://stackoverflow.com/a/45064266/1872674

You should put a .runtimeconfig.json into the functions directory. Your dotenv variables move to .runtimeconfig.json with json format.


This is my solution.

const functionConfig = () => {
    if (process.env.RUN_LOCALLY) {
        const fs = require('fs');
        return JSON.parse(fs.readFileSync('.env.json'));
    } else {
      return functions.config();
    }
};

The functionConfig() was called by your Firebase Function.

exports.helloWorld = functions.https.onRequest((request, response) => {
    response.send("someservice id is: " + functionConfig().someservice.id);
});

.env.json is like:

{
  "someservice": {
    "key":"THE API KEY",
    "id":"THE CLIENT ID"
  }
}

Finally, run the command with the RUN_LOCALLY variable.

RUN_LOCALLY=1 firebase serve

When we will deploy functions, don't forget to update the environment configuration in Firebase using the .env.json.

like image 30
komiyak Avatar answered Sep 22 '22 00:09

komiyak