Firebase Functions: React users need to stop storing sensitive API keys in .env files!
Tags: react, api, security, keys
Published: 2019-02-23 00:37:34

---
title: Firebase Functions: React users need to stop storing sensitive API keys in .env files!
published: true
description: I show you how to see your .env variables when you deploy your React app.
tags: #React #API #security #keys
---
<p>This is a tutorial from my <a href="https://andersjr1984.github.io/portfolio">digital portfolio/blog</a>.  Please stop by and take a look, then criticize me in the comments.  Haha.</p>
<p>Many of you are reading this because you are an awesome Front-End Developer with a sick idea, but don’t quite know how to secure some of the necessary API calls without a full back-end environment.  You hop online and everyone is telling you to put your API keys in an .env file then blah blah blah and rest easy.  Well, if you have sensitive API keys and you follow the .env advice, you are putting yourself at a high risk.<br><br><b>Here is why a .env file doesn’t protect you in production:</b><br>The background is that when React creates the production build, it jumbles everything up into a chunk.js file.  If you have a react project with some API keys, do me a favor and go to the page, then inspect it using the DEV tools.  Go to <b>Sources</b>, then go to the <b>static</b> folder, then go to the <b>js</b> folder.  There is a weird filename that you probably don’t recognize that ends with <b>chunk.js</b>.  Open that file up and copy the contents to a word processor (I tried searching in the inspect tool, but it was suuuuper slow).  Go to your sacred .env file and copy one of your “secure” API keys, then jump back into your word processor.  Try a find on your API key.  What is that you say, your key is in this <b>PUBLIC</b> file?<br><br>Well, you have some work ahead of you, but fret not, you can use Google Firebase to act as a back-end server and save your back-end from API fraud and ending up like this <a href="https://www.quora.com/My-AWS-account-was-hacked-and-I-have-a-50-000-bill-how-can-I-reduce-the-amount-I-need-to-pay" rel="noopener noreferrer" target="_blank">guy</a>.  I’ll line out how I did it and if I can do it, you should be able to.  You do need to be on the Blaze plan for this to work.  If you make a lot of external API calls in a month, you could end up paying.<br><br></p><ol><li>Start a Firebase account, then create a project for this little activity.  I trust you can figure this out using google.</li><li>Install Firebase tools (npm install -g firebase-tools), login (firebase login) and initiate the functions while you are in your project's folder (firebase init functions).  Follow the instructions.</li><li>Now you have a nifty little folder in your app that holds your functions and an index.js file that holds the first function you will be using.  Just uncomment the helloWorld function!  But ugh, what kind of weird language is that written in?</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/helloWorldFun.1f111f9e.png" alt="Hello World"><li>We are going to have to make this work in React.  First off, you are going to have to make it an onCall function if you are using React and you can't send your response back using response.send. . .  This needs a return:</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/helloWorldReact.c4ca1b27.png" alt="Hello World React"><li>Create a firebase.js file in your src directory.  Secure the config information in your precious .env file so it provides a little difficulty to steal.  And now you are thinking that I just told you that your API Keys need to be secure.  <a href="https://www.youtube.com/watch?v=0NaEmhh9Dzg" rel="noopener noreferrer" target="_blank">The Firebase API is slightly different.</a>  If you are using secure API Keys, listen to this man on how to secure them!</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/firebaseFile.96705300.png" alt="Firebase File"><li>Find an inconspicuous place in one of your exported components and create a variable that points to your firebase function.  You can then call your function using this variable, then log the value to the console.  The data comes in as an object, so you will have to dig one step deeper, into the 'data' key.</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/callFunction.3d2e93d0.png" alt="Calling the Function"><li>Push your functions to firebase using the command <i>firebase deploy --only functions</i>.</li><li>The results are in!</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/hello.3e766c0f.png" alt="Hello!"><li>Firebase currently uses Node.js 6, which is a pain when trying to write async/await functions.  These are typically much easier to understand than promise.then functions for many of the junior developers I talk to.  Fortunately, you can make firebase accept Node.js 8 as its master opening your package.json file in your <b>Functions</b> folder and adding the following line:</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/node8.1b19c032.png" alt="Gimmee async/await"><li><b>Write your API function in the index.js of your Function folder: </b>I'm not going to go through all the trials I had in writing my API call to Yelp.  I'm just going to give you the code and you can reapply it to most API calls that use Headers.  If you are a master at React API calls, you'll notice Authorization is in quotes.  Took me a long time to figure that out.  You'll see that my API call only returns tacos, that is because I think tacos are the only thing that are important.  You'll have to adjust your req call to add additional variables.</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/yelp.a0d18708.png" alt="I only want tacos"><li>What does the following line mean? <i>functions.config().yelp.key</i> calls your API key from the config file of your functions library.  You save it by using your command prompt in the functions folder and typing <a href="https://firebase.google.com/docs/functions/config-env" rel="noopener noreferrer" target="_blank">firebase functions:config:set someservice.key="THE API KEY" someservice.id="THE CLIENT ID"</a>.</li><li>Get out your console.log hat and test stuff out till you get something similar to the following code to work.  Also, aren't async/await functions beautiful.</li><br><img src="https://andersjr1984.github.io/portfolio/static/media/passTacos.0d71a35e.png" alt="I still only want tacos"></ol><p>I know, you are blown away and hungry for tacos.  Feel free to comment or send me an e-mail or give me a job.</p>