How to pass API keys in environment variables to Ember CLI using process.env?

落花浮王杯 提交于 2020-01-09 12:50:14

问题


How do I pass environment variables from bashrc to Ember CLI. I imagine a situation where you need stripe api keys or pusher api-keys and you have them in your environment variables in bashrc. How do you pass the api-keys to Ember CLI.

I tried using Node.js process.env in both the brocfile.js and environment.js, but when I try to access it in the Ember JS controller, the property is null.

In my environment.js file I added,

APP: { apiKey: process.env.KEY }

In My Ember JS controller I tried accessing it with:

import config from  '../config/environment'; 

And setting the controller property lkey as shown below, which didn't work:

lkey: config.App.KEY

Next in my brocfile.js, I added:

var limaKey = process.env.Key; 
var app = new EmberApp({key: limaKey});

This still didn't work.


回答1:


I finally resolved this issue. I was faced with two options. Option 1 was to use XHR to fetch the api-keys from an end-point on the server. Option 2 is get the api-key directly from environment variables using Nodejs process.env. I prefer option 2 because it saves me from doing XHR request.

You can get option 2 by using this ember-cli-addOn which depends on Nodejs Dotenv project

  • https://github.com/fivetanley/ember-cli-dotenv
  • https://github.com/motdotla/dotenv

In my case I choose to do it without any addOn.

  1. First add the api-key to your .bashrc if you are Ubuntu or the approapriate place for your own linux distro.
export API_KEY=NwPyhL5
  1. Reload the .bashrc file, so your setting are picked up:
source ~/.bashrc
  1. In Ember CLI add a property to the ENV object in config/environment.js. The default looks like this
module.exports = function(environment) {
  var ENV = {
     modulePrefix: 'rails-em-cli',
     environment: environment,
     baseURL: '/',
     locationType: 'auto',
     EmberENV: {

      }
   }

Now to that ENV object, we can add a new property myApiKey like this:

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'rails-em-cli',
    environment: environment,
    baseURL: '/',
    locationType: 'auto',
    myApikey: null,
    EmberENV: {

     }

   //assign a value to the myApiKey

     if (environment === 'development') {
        // ENV.APP.LOG_RESOLVER = true;

        ENV.myApiKey = process.env.API_KEY;
      }              

   }

Note that process.env.API_KEY is fetching the setting we added to .bashrc and assigning it to myApiKey. You will need to have Nodejs installed on your server for process.env to work.

Finally to access that variable in your controller you do

import config from '../config/environment';
import Ember from 'ember';

export default Ember.Controller.extend({

  yourKey: config.myApikey,

});

That's it.




回答2:


You can also set the variables on the ENV.APP object: they will be carried by the application instance.

You can then reuse them inside initializer & so on.

This way, you won't have to import config/environment into application's code, which seems a little weird to me.




回答3:


I want to make sure that my API keys are not checked in. As part of the build process, I copy a local config file to the config directory and load it into environment.js

In environment.js

try {
  var local = require('./local_environment');
  Object.keys(local.config).forEach(function(key) {
    ENV[key] = local.config[key];
  });
} catch(err) {
  console.log("config/local_environment.js not found");
}

In local_environment.js (not checked in, copied by build process)

exports.config = {
  SOME_API_KEY: 'key_here'
};



回答4:


The key is to define the ENV variables in config/environment.js and when you need to access them somewhere (i.e. adapter, controller, etc), you import config/environment.js first.

For an Ember CLI app, https://ember-cli.com/user-guide/#Environments documents this, for your reference.

Sample logic:

# app/controllers/foobar.js
import DS from 'ember-data';
import ENV from 'nameOfApp/config/environment';

export default Ember.Controller.extend({
  actions: {
    click: function() {
      console.log(ENV.SOME_ENVIRONMENT_KEY);
    }
  }
});

# config/environment.js
module.exports = function(environment) {
  ....

  if (environment === 'development') {
    ENV.SOME_ENVIRONMENT_KEY = 'asdf1234';
  }

  ...
};


来源:https://stackoverflow.com/questions/26403334/how-to-pass-api-keys-in-environment-variables-to-ember-cli-using-process-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!