Access heroku config vars
emarchak opened this issue · comments
Firstly, thanks for making a great, simple server! I'm really enjoying using Zero.
I'm hosting my React app on Heroku and hoping to store some API tokens in their config vars. The Zero documentation recommends that I use a .env
file, which works fine locally but I wouldn't want to deploy it my server.
Is there a recommended way to access the config vars on the front end? It seems like I should be able to extend setupEnvVariables()
if you'll accept a PR.
You should be able to access all your env variables (set by heroku, or any other host) as you would in nodejs .js
files:
process.env.GITHUB_USERNAME
.
These aren't exposed to React .jsx
files because that would expose them to your users. You can however pass them as props to your component like this:
// index.jsx
import React from "react";
export default class extends React.Component {
static async getInitialProps() {
return {
"username" : process.env.GITHUB_USERNAME
}
}
render() {
return <p>My github username: {this.props.username}</p>;
}
}
Does that solve the problem?
That helped a lot!
It ended up being that Heroku didn't provide any access to the env variables at all to the .jsx files. I ended up creating an express .js endpoint and handling it there. Which is more secure anyway.
Thanks!