【问题标题】:Check If Firebase User Exist Without Throwing Error检查 Firebase 用户是否存在且未引发错误
【发布时间】: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


    【解决方案1】:

    为避免在 catch 块中进一步实现,您可以将此 Firebase 函数包装到此代码中:

    async function checkUserInFirebase(email) {
        return new Promise((resolve) => {
            admin.auth().getUserByEmail(email)
                .then((user) => {
                    resolve({ isError: false, doesExist: true, user });
                })
                .catch((err) => {
                    resolve({ isError: true, err });
                });
        });
    }
    
    ...
    
    const rFirebase = await checkUserInFirebase('abc@gmail.com');
    

    【讨论】:

      【解决方案2】:

      要查看是否已为给定电子邮件地址创建了用户帐户,请致电 admin.auth().getUserByEmail

      admin.auth().getUserByEmail(email).then(user => { 
        // User already exists
      }).catch(err => { 
        if (err.code === 'auth/user-not-found') {
          // User doesn't exist yet, create it...
        }
      })
      

      虽然您仍在使用 catch(),但它感觉像是一次失败的操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-26
        • 1970-01-01
        • 2017-11-20
        • 2018-11-27
        相关资源
        最近更新 更多