Firebase 注册 Swift

Firebase sign up with Swift

所以,出于某种原因,我遇到了代码问题。我有一个 Swift 应用程序和一个注册视图控制器,它应该在 Firebase 中使用电子邮件注册用户作为后端(我已经将该应用程序升级到最新的 Firebase 版本)。我创建了一些断点以查看它停止工作的位置,结果是:

FIRAuth.auth()?.createUserWithEmail(email!, password: password!, completion: {
            user, error in

它不会执行整个错误或成功代码检查,它只是移出 createUserWithEmail 方法的范围。电子邮件和密码有值,我看到了它们,但它只是在方法的范围之外继续。我已将 Firebase 作为模块导入,它在构建时或编译时不会抛出任何警告或错误

我不知道为什么会这样。这是注册按钮的代码:

@IBAction func signUpTapped(sender: ButtonWhite) {

    let email = emailTextField.text
    let password = passwordTextField.text
    let repeatPassword = repeatPasswordTextField.text

    if email != "" && password != "" && repeatPassword != "" {

        if password == repeatPassword {

            FIRAuth.auth()?.createUserWithEmail(email!, password: password!, completion: {
                user, error in

                print(user)
                // Error Case 
                if error != nil {

                    if let errorCode = FIRAuthErrorCode(rawValue: error!.code) {
                        switch errorCode {
                        case .ErrorCodeInvalidCredential:
                            showAlert(title: "Invalid Credentials!", msg: "Please, try again.", actionButton: "OK", viewController: self)
                        case .ErrorCodeNetworkError:
                            showAlert(title: "Network Error!", msg: "An error occurred while attempting to contact the authentication server. Try again", actionButton: "OK", viewController: self)
                        case .ErrorCodeOperationNotAllowed:
                            showAlert(title: "Bummer!", msg: "The administrator disabled sign in with the specified identity provider", actionButton: "OK", viewController: self)
                        case .ErrorCodeEmailAlreadyInUse:
                            showAlert(title: "Oops!", msg: "The email used to attempt a sign up already exists. Please, try again", actionButton: "OK", viewController: self)
                        case .ErrorCodeInvalidEmail:
                            showAlert(title: "Error", msg: "The email is invalid. Try again.", actionButton: "OK", viewController: self)
                        case .ErrorCodeTooManyRequests:
                            showAlert(title: "Error", msg: "Too many requests were made to a serve method", actionButton: "OK", viewController: self)
                        case .ErrrorCodeAccountExistsWithDifferentCredential:
                            showAlert(title: "Error", msg: "User account exists with different credentials than currently submitted. Try again.", actionButton: "OK", viewController: self)

                        default:
                            showAlert(title: "Ups!", msg: "An error occur. Please, try again.", actionButton: "OK", viewController: self)
                        }
                    }

                    SVProgressHUD.dismiss()


                } else {

                // Success Case

                    // Create Firebase User
                    let userData = ["provider" : "email", "password": password!]
                    DataService.ds.createFirebaseUser(user!.uid, user: userData)
                    print("firebase user created!")
                    self.loginUser()
                }
            })

        } else {
            showAlert(title: "Password Mismatch", msg: "The passwords you've entered do not match. Please, try again", actionButton: "OK", viewController: self)
        }

只要没有错误,FIRAuth.auth()?.createUserWithEmail会立即创建用户,不需要

// Create Firebase User
let userData = ["provider" : "email", "password": password!]
DataService.ds.createFirebaseUser(user!.uid, user: userData)
print("firebase user created!")
self.loginUser()

检查您的 firebase 控制台以查看是否已创建用户