Firebase -如何在创建新用户前检查电话号码是否存在于手机认证中?

问题描述 投票:0回答:1

在登录新用户或创建新用户时使用电子邮件,有2种不同的方法签名。当创建一个新用户时,如果电子邮件已经存在,将返回一个错误,或者登录用户时,如果电子邮件不存在,将返回一个错误。

// create account
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (authDataResult, error)

    if let error = error {

       // if this email address already exists an error will be returned
       return
    }
})

// login 
Auth.auth().signIn(withEmail: emailTextField.text!, password: self.passwordTextField.text!, completion: { (authDataResult, error) in

    if let error = error {

       // if this email address isn't inside the system then an error will be returned
       return
    }
})

但是当使用用户的电话号码登录或者创建一个新的账户时,我必须在两种情况下使用相同的方法签名。

func loginExistingUserOrCreateNewOne(phoneNumber: String, verificationCode: String) {

    PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber, uiDelegate: nil) { (verificationID, error) in

        if let error = error { return }

        guard let verificationId = verificationID else { return }

        let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationId, verificationCode: verificationCode)

        Auth.auth().signIn(with: credential, completion: { (authDataResult, error) in

            guard let authUser = authDataResult else { return }

            let checkUsersRef = Database.database().reference().child("users").child(authUser.user.uid)
            checkExistingUsersRef.observeSingleEvent(of: .value, with: { (snapshot) in

                if !snapshot.exists() {
                    // this is a new user, now add them to the users ref

                    let newUserDict = ["signupDate": Date().timeIntervalSince1970]
                    checkUsersRef.updateChildValues(newUserDict, withCompletionBlock: { (error, ref) in

                        if let error = error {

                            // because there is an error this ref was never updated so now I have to sign this user out and they have to start over agin
                            do {
                                try Auth.auth().signOut()

                            } catch let err as NSError {

                                // alert user there is a major problem
                            }
                            return
                        }

                        // if no error let them go to HomeVC
                    })

                    return
                }

                // this is a previous user fetch dict data and let them proceed to HomeVC
                guard let previousUserDict = snapshot.value as? [String: Any] else { return }

                // get newUserDict values and let them go to HomeVC
            })
        })
    }
}

如果一个用户已经有一个账户,我需要从用户的ref中获取一些数据,然后让他们进入HomeVC。如果用户以前从未注册过,那么我必须将他们添加到用户参考中,然后让他们继续。这是一个2个步骤的过程。

问题是这些额外的步骤似乎没有必要。例如,使用电子邮件签名或登录会返回一个错误,所以没有必要在另一个ref里面创建和检查是否已经存在电子邮件。

除了使用我上面代码中的流程外,还有没有其他方法可以让我在创建新账户之前确定一个电话号码是否存在,或者在登录时不存在?

ios swift firebase firebase-realtime-database firebase-authentication
1个回答
0
投票

你将需要使用管理员SDK来实现。号码查询:

admin.auth().getUserByPhoneNumber(phoneNumber)
  .then(function(userRecord) {
    // User exists.
  })
  .catch(function(error) {
    if (error.code === 'auth/user-not-found') {
      // User not found.
    }
  });

你可以使用云函数来托管一个HTTP端点。通过电话号码查询用户只能通过服务器端运行的认证API(使用Firebase Admin SDK)。

© www.soinside.com 2019 - 2024. All rights reserved.