Моя функция Firebase импортировала 2 проекта, один для производства, а другой для OTE:
Ниже показано, как я его инициализирую (здесь нет проблем):
const functions = require('firebase-functions');
var firebaseAdmin = require('firebase-admin');
var productionServiceAccount = require('./keys/production-key.json');
var oteServiceAccount = require("./keys/ote-key.json");
var prodServer = firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(productionServiceAccount),
databaseURL: 'https://production-panel.firebaseio.com'
}, "prod");
var oteServer = firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(oteServiceAccount),
databaseURL: "https://ote-panel.firebaseio.com"
}, "ote");
console.log("prodServer: ", prodServer.name, "oteServer: ", oteServer.name)
Ниже показано, какя реализую его с помощью Express
(также работает нормально):
//import express, router, authentication middlewre, etc... i will skip it...
router.post('/createUser', function (req, res) {
let admin = req.headers.env == 'prod' ? prodServer : oteServer
let newUser = {
emailVerified: false
}
if (req.body.email && req.body.password) {
newUser["email"] = req.body.email
newUser["password"] = req.body.password
}
else {
if (!req.body.email) {
return res.status(400).send({
code: -1,
msg: "Email is missing"
})
}
if (!req.body.password) {
return res.status(400).send({
code: -1,
msg: "Password is missing"
})
}
return res.status(400).send({
code: -1,
msg: "Email/Password is missing"
})
}
return admin.auth().createUser(newUser).then(userRecord => {
console.log("successfully created new User: ", userRecord.uid, ", by: ", req.headers.uid)
console.log(userRecord)
return res.status(201).send(userRecord)
}).catch(error => {
console.log("Failed to create user: ", error)
return res.status(400).send({
code: -1,
msg: "Error has occur during creating user",
error: error
})
})
})
app.use(cors)
app.use(router)
exports.api = functions.https.onRequest(app)
Вы можете видеть, что я добавил обработчик для маршрутизации HTTP-запроса в проект prod / ote на основе заголовка
Ниже приведена проблема, с которой я сталкиваюсь, я не знаю, как идентифицировать проект с событием onCall
/ onCreate
:
exports.newUserNotification = functions.auth.user().onCreate((user) => {
//How to identify this function is trigger at which project?
//Then I want to write the data to different DB, etc
});
Примечание:
1) Когда я выполняю развертывание, яЯ использую firebase use <projectId>
+ firebase deploy
, поэтому функция будет развернута только в соответствующем проекте.
Заранее спасибо