授权用户使用Firebase无效

时间:2019-06-15 08:43:36

标签: ios swift firebase firebase-authentication

我正在创建一个基本的注册和登录页面应用程序,当用户成功注册或登录后,它会转到主屏幕。这是我的代码:

这是我所谓的按钮按下功能

signupButton.addTarget(self, action: #selector(signupButtonPressed), for: .touchUpInside)

这是用于创建用户并将其存储在数据库中的代码。

@objc func signupButtonPressed() {
    // Check that the text fields aren't empty
    if let username = username.text, let password = password.text, let email = email.text, let country = countryTextField.text {
        // Create a user
        func createUser(withEmail email: String, username: String, password: String, country: String) {

            // Sign up user only if all the text fields are filled
            Auth.auth() .createUser(withEmail: email, password: password) { (result, error) in

                if result != nil {

                    // User found, go to main page
                    let mainPage = MainScreen()
                    self.present(mainPage, animated: true, completion: nil)

                } else {

                    // Error: show error message
                    print("Failed to sign user up with error: ", error!.localizedDescription)
                    return
                }

                guard let userID = result?.user.uid else {return}

                let values = ["email": email, "username": username]

                Database.database().reference().child("Users").child(userID).updateChildValues(values, withCompletionBlock: {(error, ref) in
                    if let error = error {
                        print("Failed to update database values with error: ", error.localizedDescription)
                        return}})
            } 
        }
    }
}

问题是,当我按下按钮时,没有任何效果。希望有人能帮助我,谢谢!

1 个答案:

答案 0 :(得分:3)

@objc func signupButtonPressed() {
    if let username = username.text, let password = password.text, let email = email.text, let country = countryTextField.text {
       createUser(withEmail: email, username: username, password: password, country: country)
    }
}


func createUser(withEmail email: String, username: String, password: String, country: String) {
    ...
}