swift - 在 Firebase 中保存用户名 - Swift

标签 swift firebase firebase-authentication

我的注册表中有三个文本字段。 Firebase SignUp 方法使用用户的电子邮件 ID 和密码来创建新用户。但我也想根据他们输入的内容保存用户名。

我当前的代码是;

@IBAction func registerPressed(_ sender: Any) {

    SVProgressHUD.show(withStatus: "Setting you up")
    dismissUIElements(value: false)

    let currentUserName = userName.text
    if currentUserName?.isEmpty == false {
        FIRAuth.auth()?.createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in

            if error != nil {
                print(error!)

                SVProgressHUD.dismiss()

                self.dismissUIElements(value: true)

            } else {
                print("Registration Successful!")

                SVProgressHUD.dismiss()
                self.dismissUIElements(value: true)

                self.performSegue(withIdentifier: "goToSelectionFromRegister", sender: self)
            }
        })
    }
    else {
        SVProgressHUD.dismiss()

        SVProgressHUD.showError(withStatus: "Please enter your name!")
        SVProgressHUD.dismiss(withDelay: 1)
        self.dismissUIElements(value: true)
    }

}

最佳答案

您需要一个函数来注册用户,然后在 Firebase 中为该用户创建子项。

let databaseRef=//your path to users.     

func registerUser(userUsername userName:String, userEmail email:String, userPassword password: String, userCreationComplete: @escaping (_ status: Bool, _ error: Error?) -> ()) {
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        guard let user = user else {
            userCreationComplete(false, error)
            return
        }

        let userData = ["userName": userName.text] as [String : Any]

        ref.child(user.uid).updateChildValues(userData)

        userCreationComplete(true, nil)

    }
}

然后从 registerPressed() 中调用该函数并将文本字段值传递给它,但要确保它们都不为空。

关于swift - 在 Firebase 中保存用户名 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50993653/

相关文章:

angular - 如何使用 TypeScript 和 Node 正确导入 Firestore?

android - 使用 Firebase 身份验证进行身份验证后检索 Google 访问 token

firebase - 在 Flutter 中使用相同的 Firebase 将同一应用程序中的用户和卖家分开

ios - 没有互联网的icloud同步

ios - 如何将不相关的数组添加到 iOS-charts 折线图中的数据点标签?

ios - swift 将个人的名字从 firebase 读入 UILabel

android - 短信验证码请求失败,未知状态码 17006,同时在 android 上使用 firebase phone-auth api 进行身份验证

objective-c - 尝试使用 Xcode 的新 UI 测试运行多个测试失败

ios - 结合 NSMutableArray 与 [Swift Array]

Swift:正确的 Firebase 错误处理