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

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.

主站蜘蛛池模板: 舒城县| 卓资县| 深水埗区| 山西省| 嘉峪关市| 屯门区| 梅河口市| 横山县| 安岳县| 延津县| 石家庄市| 依安县| 芮城县| 静乐县| 高安市| 博爱县| 宜城市| 神木县| 深圳市| 临颍县| 广水市| 资溪县| 抚远县| 潮州市| 桃园县| 鄯善县| 沅江市| 安国市| 崇州市| 滦南县| 上栗县| 宁武县| 枣庄市| 宿松县| 阜新| 阿坝| 巩留县| 保定市| 洛南县| 水富县| 莱芜市|