- 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.
推薦閱讀
- Apache ZooKeeper Essentials
- Python 3.7網(wǎng)絡(luò)爬蟲快速入門
- Rust編程:入門、實(shí)戰(zhàn)與進(jìn)階
- Mastering Entity Framework
- Java從入門到精通(第5版)
- 微信小程序開發(fā)解析
- Python:Master the Art of Design Patterns
- UVM實(shí)戰(zhàn)
- Cybersecurity Attacks:Red Team Strategies
- Arduino可穿戴設(shè)備開發(fā)
- 零基礎(chǔ)學(xué)C語言(升級(jí)版)
- 游戲設(shè)計(jì)的底層邏輯
- R語言數(shù)據(jù)分析從入門到實(shí)戰(zhàn)
- 微信公眾平臺(tái)服務(wù)號(hào)開發(fā):揭秘九大高級(jí)接口
- Neo4j Graph Data Modeling