Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...