简体   繁体   English

构建后如何在 Vue.js 项目中存储 Firebase 凭据 (env_var)

[英]How to store Firebase credentials in Vue.js projects after building (env_var)

I have been trying to deploy my firebase&vue app.我一直在尝试部署我的 firebase&vue 应用程序。 However I couldn't add firebase credentials to the env variable.但是我无法将 firebase 凭据添加到 env 变量中。

Basically this is the structure on vue.js基本上这是 vue.js 上的结构

  • config配置

    ---- key.js ---- key.js

    ---- keys_dev.js ----keys_dev.js

    ---- keys_prod.js ----keys_prod.js

key.js关键.js

if(process.env.NODE_ENV == 'production'){
    module.exports = require('./keys_prod');
} else {
    module.exports = require('./keys_dev');
}

keys_dev.js键_dev.js

module.exports = {
firebase: {
    apiKey: "*********************************",
    authDomain: "*****************************",
    databaseURL: "****************************",
    projectId: "******************************",
    storageBucket: "**************************",
    messagingSenderId: "**********************",
  }    
}

keys_prod.js keys_prod.js

module.exports = {
  firebase: {
    apiKey: process.env['FIREBASE_apiKey'],
    authDomain: process.env['FIREBASE_authDomain'],
    databaseURL: process.env['FIREBASE_databaseURL'],
    projectId: process.env['FIREBASE_projectId'],
    storageBucket: process.env['FIREBASE_storageBucket'],
    messagingSenderId: process.env['FIREBASE_messagingSenderId'],
  }
}

Also I tried like this in keys_prod.js but I guess we can not use object in the env_var, so I changed it to the code above.我也在keys_prod.js 中尝试过这样,但我想我们不能在env_var 中使用对象,所以我将其更改为上面的代码。

module.exports = {
  firebase: process.env.FIREBASE_CONFIG
}

After npm run build, I created express app, define process.env's however when I run server, I am getting error because of env_vars are missing.在 npm run build 之后,我创建了 express 应用程序,定义了 process.env,但是当我运行服务器时,由于缺少 env_vars,我收到错误消息。 I tried to see logs on both front and backend console.我试图在前端和后端控制台上查看日志。 I can see envs on node console but not in browser.我可以在节点控制台上看到 envs,但在浏览器中看不到。 I also tried to deploy this app to heroku and firebase hosting.我还尝试将此应用程序部署到 heroku 和 firebase 托管。 In both cases I added envs externally, but nothing change.在这两种情况下,我都在外部添加了 envs,但没有任何变化。 (I guess there is no difference to use env_vars in localhost or remote server right ?) (我想在本地主机或远程服务器中使用 env_vars 没有区别吧?)

Finally I tried to change keys.js (keys_prod > keys_dev)最后我尝试更改keys.js (keys_prod > keys_dev)

if(process.env.NODE_ENV == 'production'){
    module.exports = require('./keys_dev');
} else {
    module.exports = require('./keys_dev');
}

then it works, but I can find the credentials in the source code this time.然后它就可以工作了,但是这次我可以在源代码中找到凭据。

There's a better way that the other answer.有一个更好的方法,另一个答案。 You don't need to import - that would bring the settings into the deployed JS, which isn't a good idea.您不需要导入 - 这会将设置带入部署的 JS,这不是一个好主意。

Create a file like .env.local in the root of the Vue app, like this:在 Vue 应用程序的根目录中创建一个类似.env.local的文件,如下所示:

$ cat .env.local
# REMEMBER:
# ----------------------------------------------------------
# In development, *restart* "yarn serve" to pick up changes.
# ----------------------------------------------------------
VUE_APP_FIREBASE_API_KEY='1234-F1xH53UCnk'
VUE_APP_FIREBASE_AUTH_DOMAIN='your-web-1234.firebaseapp.com'
VUE_APP_FIREBASE_DATABASE_URL='https://your-web-1234.firebaseio.com'
VUE_APP_FIREBASE_PROJECT_ID='your-web-1234'
VUE_APP_FIREBASE_STORAGE_BUCKET='your-web-1234.appspot.com'
VUE_APP_FIREBASE_MESSAGING_SENDER_ID='12345678'
VUE_APP_FIREBASE_APP_ID='1:1234:web:0398509235'

Wherever you want firebase, do something like:无论您想要使用 firebase,都可以执行以下操作:

function getFirebaseConfig() {
  const firebaseConfig = {
    // see .env file for instructions
    apiKey: process.env.VUE_APP_FIREBASE_API_KEY || 'api-key-not-set',
    authDomain: process.env.VUE_APP_FIREBASE_AUTH_DOMAIN || 'env-not-set',
    databaseURL: process.env.VUE_APP_FIREBASE_DATABASE_URL || 'env-not-set',
    projectId: process.env.VUE_APP_FIREBASE_PROJECT_ID || 'env-not-set',
    storageBucket: process.env.VUE_APP_FIREBASE_STORAGE_BUCKET || 'env-not-set',
    messagingSenderId: process.env.VUE_APP_FIREBASE_MESSAGING_SENDER_ID || 'env-not-set',
    appId: process.env.VUE_APP_FIREBASE_APP_ID || 'env-not-set',
  }
  return firebaseConfig
}

// Initialize Firebase
firebase.initializeApp(getFirebaseConfig())

I'm using TypeScript, so I put that in a file called cloud-functions.ts` and after the above, I have:我正在使用 TypeScript,所以我把它放在一个名为 cloud-functions.ts` 的文件中,在上面之后,我有:

export const postContactFunc = firebase.functions().httpsCallable('postContact')

Then, where I want to consume that function, I have:然后,在我想使用该功能的地方,我有:

// somewhere-else.ts
import {postContactFunc} from '@/services/cloud-functions'

Note that .env*.local is never checked in.请注意, .env*.local永远不会被签入。

To remind myself what the file should be, I have this content in .env which is checked in:为了提醒自己文件应该是什么,我在.env有这个内容签入:

$ cat .env
# REMEMBER:
# ----------------------------------------------------------
# DO NOT EDIT THIS FILE
#
# Edit .env.local or similar.
# ----------------------------------------------------------

# Get these from console - look on the _app_ area not the _project_ area.
VUE_APP_FIREBASE_API_KEY='not-set-yet'
VUE_APP_FIREBASE_AUTH_DOMAIN='not-set-yet'
VUE_APP_FIREBASE_DATABASE_URL='not-set-yet'
VUE_APP_FIREBASE_PROJECT_ID='not-set-yet'
VUE_APP_FIREBASE_STORAGE_BUCKET='not-set-yet'
VUE_APP_FIREBASE_MESSAGING_SENDER_ID='not-set-yet'
VUE_APP_FIREBASE_APP_ID='not-set-yet'

Here how you can do it.在这里你可以怎么做。

Firstly, add dev.env to your project and add firebase variables.首先,将dev.env添加到您的项目并添加dev.env变量。

FIREBASE_CONFIG: {
    apiKey: "*********************************",
    authDomain: "*****************************",
    databaseURL: "****************************",
    projectId: "******************************",
    storageBucket: "**************************",
    messagingSenderId: "**********************",
} 

Import the file in dev.env.js在 dev.env.js 中导入文件

import merge from 'webpack-merge';
import env from './dev.env';

module.exports = merge(env, {
  NODE_ENV: 'development'
}

Now, Initialize your firebase app with现在,初始化您的 firebase 应用程序

import * as firebase from 'firebase';
firebase.initializeApp({VUE_APP_FIREBASE_CONFIG})

There you go!你去吧! Firebase is set up for you Firebase 已为您设置好

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM