官术网_书友最值得收藏!

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:

  1. 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.

  1. To use this key in index.html, we can add the following above all the other script tags:
<script src="/secrets.js"></script>
  1. Then, in our Firebase initialization:
 <script>  
// Initialize Firebase
var config = {
apiKey: window.apiKey,
// ...rest of config
messagingSenderId: window.messagingSenderId
};
  1. 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.

主站蜘蛛池模板: 北川| 焉耆| 临西县| 公主岭市| 松桃| 鹿邑县| 巴中市| 县级市| 洪江市| 京山县| 乡宁县| 通化市| 花莲市| 永胜县| 鄱阳县| 阳西县| 南丰县| 九龙城区| 白银市| 天长市| 长武县| 乐平市| 青铜峡市| 嘉鱼县| 彰化县| 望城县| 印江| 浮梁县| 乡城县| 皋兰县| 鹤峰县| 鄂伦春自治旗| 思南县| 万荣县| 来宾市| 防城港市| 乌什县| 乐陵市| 郴州市| 佛冈县| 桃江县|