admin管理员组文章数量:1356842
I'd like to start by saying I followed all of the suggestions here How do I hide API key in create-react-app? but none of them worked. I've searched for over an hour trying to find an answer but nothing. Below is my explanation.
- I used
create-react-app
to scaffold my project - I'm using Google Maps and have not used any other API's yet
- I created a
.env
file in the root of my project - In that file I added
REACT_APP_GOOGLE_MAPS_API_KEY = api key
- In my ponent file
Map.js
I addedconst GM_API_KEY =
${process.env.REACT_APP_GOOGLE_MAPS_API_KEY};
(the tick marks are around this variable in my file but they won't show up here) - In my url for the map, I added the key like so
";callback=initMap"
- Also, at the top of my
Map.js
file I added aconsole.log(GM_API_KEY);
to ensure it was working. - I exported my Map ponent and then imported it in my
App.js
- I restarted my server
When the page loads, my key is output in the console, but I still get an error from Google saying my API key is invalid. Also, if I manually console.log(GM_API_KEY)
after the page has loaded I get a reference error saying that GM_API_KEY is undefined
If anyone has any suggestions or can provide any help I would greatly appreciate it! Thank you for taking time to check out my question.
I'd like to start by saying I followed all of the suggestions here How do I hide API key in create-react-app? but none of them worked. I've searched for over an hour trying to find an answer but nothing. Below is my explanation.
- I used
create-react-app
to scaffold my project - I'm using Google Maps and have not used any other API's yet
- I created a
.env
file in the root of my project - In that file I added
REACT_APP_GOOGLE_MAPS_API_KEY = api key
- In my ponent file
Map.js
I addedconst GM_API_KEY =
${process.env.REACT_APP_GOOGLE_MAPS_API_KEY};
(the tick marks are around this variable in my file but they won't show up here) - In my url for the map, I added the key like so
"https://maps.googleapis./maps/api/js?key=GM_API_KEY&callback=initMap"
- Also, at the top of my
Map.js
file I added aconsole.log(GM_API_KEY);
to ensure it was working. - I exported my Map ponent and then imported it in my
App.js
- I restarted my server
When the page loads, my key is output in the console, but I still get an error from Google saying my API key is invalid. Also, if I manually console.log(GM_API_KEY)
after the page has loaded I get a reference error saying that GM_API_KEY is undefined
If anyone has any suggestions or can provide any help I would greatly appreciate it! Thank you for taking time to check out my question.
Share Improve this question edited Oct 5, 2018 at 15:45 Jonathan Sexton asked Oct 5, 2018 at 15:28 Jonathan SextonJonathan Sexton 1393 silver badges12 bronze badges 3- 2 Please show a minimal reproducible example to go along with your English description. – Code-Apprentice Commented Oct 5, 2018 at 15:47
-
Thanks for sharing the guidelines and my apologies for not providing that. Here is my example that worked for others in the future: In
.env
file:REACT_APP_GOOGLE_MAPS_API_KEY = api key here
In myMap.js
ponent:const GM_API_KEY =
'${process.env.REACT_APP_GOOGLE_MAPS_API_KEY}';
(use back ticks instead of quotes to surround all of this) In the url call:'https://maps.googleapis./maps/api/js?key=${GM_API_KEY}&callback=initMap'
(use back ticks around this as well instead of quotes) – Jonathan Sexton Commented Oct 5, 2018 at 16:03 - No need to apologize. Just edit your question to include the requested information. – Code-Apprentice Commented Oct 5, 2018 at 16:19
2 Answers
Reset to default 5What I do with sensitive data is not even saving them in .env
I open the terminal inside the root of my project and run export API_KEY=sdf54vvetvf...
and then inside your app you can access it through process.env.API_KEY
note you need to add it manually each time you close the terminal from that session
Try to define the constant that holds the api key in a lifecycle hook like ponentWillMount (though this method is now unsafe) to ensure that the variable is available when the ponent mounts:
ponentWillMount() {
const GM_API_KEY = ${process.env.REACT_APP_GOOGLE_MAPS_API_KEY};
}
Then
https://maps.googleapis./maps/api/js?key=${GM_API_KEY}&callback=initMap
本文标签: javascriptHiding API Key in ReactStack Overflow
版权声明:本文标题:javascript - Hiding API Key in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743946673a2566535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论