【发布时间】:2018-10-13 18:19:51
【问题描述】:
我有一个提供简单消息服务的网站。个人可以为服务付费,或者企业可以按月付费,然后免费添加他们的客户/用户。当企业添加客户/用户电子邮件时,会触发以下功能。我正在使用 firebase 函数和 createUser 在我的服务器上创建用户(更少)。但是,有时企业尝试注册用户并且该用户已经存在。在这种情况下,我想向用户发送一封提醒电子邮件。
我的代码运行良好,但在我的捕获/错误中包含一个链感觉很时髦。是否有其他方法可以检测电子邮件是否已在不会引发错误的 Firebase 帐户中注册?
exports.newUserRegisteredByBusiness = functions.database.ref('users/{uid}/users/invited/{shortEmail}').onWrite( (data, context) => {
//don't run function if data is null
if (!data.after.val()){
console.log('SKIP: newUserRegisteredByBusiness null so skipping')
return null
} else {
let businessUID = context.params.uid
let email = data.after.val()
let shortEmail = context.params.shortEmail
let password // = something I randomly generate
return admin.auth().createUser({ email: email, password: password}).then( (user)=> {
//write new user data
let updates = {}
let userData // = stuff I need for service to run
updates['users/' + user.uid ] = userData;
return admin.database().ref().update(updates)
}).then( () =>{
//email new user about their new account
return emailFunctions.newUserRegisteredByBusiness(email, password)
}).catch( (error) =>{
//if user already exist we will get error here.
if (error.code === 'auth/email-already-exists'){
//email and remind user about account
return emailFunctions.remindUsersAccountWasCreated(email).then( ()=> {
//Once email sends, delete the rtbd invite value that triggered this whole function
//THIS IS WHERE MY CODE FEELS FUNKY! Is it ok to have this chain?
return admin.database().ref('users/' + businessUID + '/users/invited/' + shortEmail).set(null)
})
} else {
//delete the rtbd value that triggered this whole function
return admin.database().ref('users/' + businessUID + '/users/invited/' + shortEmail).set(null)
}
});
}
})
【问题讨论】:
标签: node.js promise firebase-authentication firebase-admin