How can I initialize cloud functions and service account together. In my index.ts file I have already initialized functions using
admin.initializeApp(functions.config().firebase);
but now I want to create create custom token with firebase, so I created a service account and added the service-account.json
in my functions folder and added
const serviceAccount = require("./service-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://functionstest-54bd9 .firebaseio.com"
});
above initialize functions. but when I tried to deploy the function, i got this error in the console
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)
I have only one app and I have added only a service account. Do I have to create another app just to create the custom token only? How can I fix this?
Update1
This is how I am creating custom token,
//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() });
Please correct me if I am doing it wrong