Monday, 15 June 2015

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



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

how pass environment variables bashrc ember cli. imagine situation need stripe api keys or pusher api-keys , have them in environment variables in bashrc. how pass api-keys ember cli.

i tried using node.js process.env in both brocfile.js , environment.js, when seek access in ember js controller, property null.

in environment.js file added,

app: { apikey: process.env.key }

in ember js controller tried accessing with:

import config '../config/environment';

and setting controller property lkey shown below, didn't work:

lkey: config.app.key

next in brocfile.js, added:

var limakey = process.env.key; var app = new emberapp({key: limakey});

this still didn't work.

i resolved issue. faced 2 options. alternative 1 utilize xhr fetch api-keys end-point on server. alternative 2 api-key straight environment variables using nodejs process.env. prefer alternative 2 because saves me doing xhr request.

you can alternative 2 using ember-cli-addon depends on nodejs dotenv project

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

in case take without addon.

first add together api-key .bashrc if ubuntu or approapriate place own linux distro. export api_key=nwpyhl5 reload .bashrc file, setting picked up: source ~/.bashrc in ember cli add together property env object in config/environment.js. default looks this module.exports = function(environment) { var env = { moduleprefix: 'rails-em-cli', environment: environment, baseurl: '/', locationtype: 'auto', emberenv: { } }

now env object, can add together new property myapikey this:

module.exports = function(environment) { var env = { moduleprefix: 'rails-em-cli', environment: environment, baseurl: '/', locationtype: 'auto', myapikey: null, emberenv: { } //assign value myapikey if (environment === 'development') { // env.app.log_resolver = true; env.myapikey = process.env.api_key; } }

note process.env.api_key fetching setting added .bashrc , assigning myapikey. need have nodejs installed on server process.env work.

finally access variable in controller do

import config '../config/environment'; import ember 'ember'; export default ember.controller.extend({ yourkey: config.myapikey, });

that's it.

ember.js ember-cli ember-router

No comments:

Post a Comment