- 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.
推薦閱讀
- INSTANT OpenCV Starter
- Moodle Administration Essentials
- C# 2012程序設計實踐教程 (清華電腦學堂)
- HBase從入門到實戰
- OpenCV 3和Qt5計算機視覺應用開發
- SAP BusinessObjects Dashboards 4.1 Cookbook
- 琢石成器:Windows環境下32位匯編語言程序設計
- Nginx實戰:基于Lua語言的配置、開發與架構詳解
- 大話Java:程序設計從入門到精通
- Learning Android Application Testing
- Learning Kotlin by building Android Applications
- 軟件測試分析與實踐
- Mastering Leap Motion
- After Effects CC案例設計與經典插件(視頻教學版)
- SaaS攻略:入門、實戰與進階