- Progressive Web Apps with React
- Scott Domes
- 170字
- 2021-07-08 09:36:23
Hiding our API key
We need to move our API key and messagingSenderId to a separate file, and then ensure that the file is not checked into Git:
- To do so, create a file in public/ called secrets.js. In that file, place the following:
window.apiKey = "YOUR-API-KEY”
messagingSenderId = "YOUR-SENDER-ID"
Again, we're taking advantage of the globally accessible window object to store the key. For those of you new to JavaScript, note that it is not good practice to abuse the window object; only use it when absolutely necessary.
- To use this key in index.html, we can add the following above all the other script tags:
<script src="/secrets.js"></script>
- Then, in our Firebase initialization:
<script>
// Initialize Firebase
var config = {
apiKey: window.apiKey,
// ...rest of config
messagingSenderId: window.messagingSenderId
};
- As the last step, we need to tell Git to ignore the secrets.js file. You can do so by modifying our .gitignore file in the project base, adding the following line:
/public/secrets.js
All done! We can now commit and push up freely.
推薦閱讀
- 軟件項目估算
- Python概率統計
- iOS面試一戰到底
- 微服務與事件驅動架構
- Learning C++ Functional Programming
- Learning Data Mining with Python
- React Native Cookbook
- Silverlight魔幻銀燈
- YARN Essentials
- 用Flutter極速構建原生應用
- 利用Python進行數據分析(原書第3版)
- The Complete Coding Interview Guide in Java
- RabbitMQ Essentials
- 單片機C語言程序設計實訓100例
- 基于Struts、Hibernate、Spring架構的Web應用開發