Storing API Keys in react-native app with expo library

Issue

I have a question, please, if possible explain in simple terms. Im new to react native, what’s the best way to store API keys in a secure way where they can’t reverse engineer it and get the keys right away. Can I just retrieve it from the server side using a restapi to get the apikey only if user is signed in? I’m trying to upload pictures to aws storage, but I want to store the APIKey somewhere where it’s difficult to retrieve at least by hackers. Also, is there a way to send images trough the server express.js(from react native to express app) how can I do it, so I can upload it to aws storage or even if it is possible on mongodb instead of aws storage.

For example:

const express = require("express");
const requireAuth = require("../middlewares/requireAuth");
const router = express.Router();
router.use(requireAuth); //make sure they are signed in

/**
 * * GET: Api key for the amazon s3 bucket storage
 */
router.get("/apikey/amazonstorage", (req, res) => {
  const APIKEY = process.env.APIKEY;
  if (APIKEY) {
    res.status(200).send(APIKEY);
  } else {
    res.status(502).send(null);
  }
});

Thank you in advance

Solution

In general, the safest way to handle API secret keys is to store them on your backend server and have the server make those requests to the third party APIs for the client (and send the results back to the client if necessary).

From the React Native docs:

If you must have an API key or a secret to access some resource from your app, the most secure way to handle this would be to build an orchestration layer between your app and the resource. This could be a serverless function (e.g. using AWS Lambda or Google Cloud Functions) which can forward the request with the required API key or secret. Secrets in server side code cannot be accessed by the API consumers the same way secrets in your app code can.

Answered By – Bryce

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.