- 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.
推薦閱讀
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- 程序是怎樣跑起來(lái)的(第3版)
- Windows內(nèi)核編程
- Python機(jī)器學(xué)習(xí)算法: 原理、實(shí)現(xiàn)與案例
- Java高并發(fā)核心編程(卷1):NIO、Netty、Redis、ZooKeeper
- 運(yùn)維前線:一線運(yùn)維專家的運(yùn)維方法、技巧與實(shí)踐
- Building Business Websites with Squarespace 7(Second Edition)
- C語(yǔ)言程序設(shè)計(jì)
- 征服C指針(第2版)
- Android初級(jí)應(yīng)用開(kāi)發(fā)
- 城市信息模型平臺(tái)頂層設(shè)計(jì)與實(shí)踐
- Java EE互聯(lián)網(wǎng)輕量級(jí)框架整合開(kāi)發(fā):SSM+Redis+Spring微服務(wù)(上下冊(cè))
- Learning QGIS(Second Edition)
- 編程風(fēng)格:程序設(shè)計(jì)與系統(tǒng)構(gòu)建的藝術(shù)(原書第2版)
- SFML Essentials