我已经创建了一个函数,该函数根据firebase的身份验证选项卡中的电子邮件入口在防火墙数据库中设置用户ID。但是,我需要它成为一个云函数,在第一次登录时触发它。我已经找到了提到此功能的文档,但我不知道如何只在第一次登录时触发它,而不是在用户第二次登录时触发,也无法在用户创建时触发。我还提供了下面的代码,也许它给了我更好的想法,我需要什么。一些行被注释掉了,所以我可以测试其余的代码。我已经找到了关于这个主题的多个线程,但我不知道如何准确地管理这个主题。
https://firebase.google.com/docs/functions/auth-events#trigger_a_function_on_user_creation
//Detect first login from user
//if(firebase.auth.UserCredential.isNewUser()){
if(true){
//User is logged in for the first time
//const userID = firebase.auth().currentUser.UID;
//const userEmail = firebase.auth().currentUser.email;
const userID = '1234567890';
const userEmail = 'example@example.com';
var docFound = false;
//Get email, either personal or work
console.log('Taking a snapshot...');
//Test for work email
const snapshot = db.collectionGroup('people').where('email.work', '==', userEmail).get()
.then(function(querySnapshot){
querySnapshot.forEach(function(doc){
//work email found
console.log('work email found');
console.log(doc.data());
docFound = true;
const organisationID = doc.ref.parent.parent.id;
writeUID(doc.id, userID, organisationID);
});
});
if(!docFound){
//Test for personal email
const snapshot = db.collectionGroup('people').where('email.personal', '==', userEmail).get()
.then(function(querySnapshot){
querySnapshot.forEach(function(doc){
//personal email found
console.log('personal email found');
console.log(doc.data());
const organisationID = doc.ref.parent.parent.id;
writeUID(doc.id, userID, organisationID);
});
});
}
}
async function writeUID(doc, uid, organisationID){
const res = db.collection(`organisations/${organisationID}/people`).doc(doc).set({
userId: uid
}, { merge: true });
}
/*
TODO: Detect first login
TODO: Get correct user values
Rest of the function works
*/提前谢谢你的帮助。
发布于 2020-07-31 16:08:00
--我不知道如何只在第一次登录时触发它,而不是在用户第二次登录时触发,也无法在用户创建时触发
您尝试使用Cloud函数和触发器所做的事情是不可能的。
当创建或删除用户帐户时,Auth只触发只工作。它们不会在用户登录时触发。只有您的应用程序知道用户何时登录或退出-- Firebase或Cloud函数都不了解您对“第一个登录”的具体定义。您需要做的是在应用程序代码中检测您的“第一个登录”,然后可能调用一个函数(HTTP或可调用)来代表用户完成一些工作。
https://stackoverflow.com/questions/63187948
复制相似问题