一起初始化服务账号和云函数-Firebase

Initialize service account and clound functions together-Firebase

如何一起初始化云功能和服务帐户。在我的 index.ts 文件中,我已经使用

初始化了函数

admin.initializeApp(functions.config().firebase);

但现在我想用 firebase 创建自定义令牌,所以我创建了一个服务帐户并在我的函数文件夹中添加了 service-account.json 并添加了

const serviceAccount = require("./service-account.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://functionstest-54bd9 .firebaseio.com"
});

以上初始化函数。但是当我尝试部署该功能时,我在控制台中收到此错误

Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.
    at FirebaseAppError.FirebaseError [as constructor] (/home/me/Documents/TfmFirebase/functions/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/home/me/Documents/TfmFirebase/functions/node_modules/firebase-admin/lib/utils/error.js:85:28)

我只有一个应用程序,而且我只添加了一个服务帐户。我是否必须创建另一个应用程序才能创建自定义令牌?我怎样才能解决这个问题?

更新1

这就是我创建自定义令牌的方式,

//create user
let writeResult = await db.collection('users').add({
                    user: user,
                    password: password,
                    isBlocked: false,
                    joiningDate: Date.now(),
                    phoneVerified: false,
                    deleted: false,
                    contacts:{
                        phone: phone
                    }
                })

//create token
let tokenSnapShot = await admin.auth().createCustomToken(writeResult.id);
const userRef = db.collection('users').doc(writeResult.id);
let updateResult = await userRef.get();
t.update(userRef,{ "token": updateResult.data() });

如有错误请指正

我刚刚在研究同一个主题。当您在本地主机上工作时。您将需要额外的安全性来访问您的数据库。因此,您将需要管理 service key while working at localhost, and you may emulate only HTTPS 功能。在 firebase 托管工作时,您不需要此帐户密钥。但也可以正常工作。然后你需要删除上面的代码

1.admin.initializeApp(functions.config().firebase); 

const serviceAccount = require("./service-account.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://functionstest-54bd9 .firebaseio.com"
});