在Firebase中保存用户名-Swift

时间:2018-06-22 18:20:26

标签: 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)
    }

}

2 个答案:

答案 0 :(得分:1)

您需要一个功能,该功能将注册用户,然后在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()内部调用该函数,然后将文本字段值传递给该函数,但请确保它们都不为空。

答案 1 :(得分:0)

您将创建一个存储该信息的新数据表。在创建用户功能中不会完成此操作。

// create a reference to  the DB
ref = Database.database().reference(fromURL: "https://your-project-name.firebaseio.com/")
//variables to store data
var myID : String?
var myName : String?
var myNumber : String?
var myEmail : String?

// creating the save user function
func saveUser(_ completion: @escaping(_ error: Error?) -> Void) {
    if PageDataSource.sharedInstance.crudIsAvailable == true {
        let usersRef = ref.child("users")
        let myUserRef = usersRef.child(id)
        myUserRef.updateChildValues(["User ID": id,
                                     "Name": myName,
                                     "Email": myEmail,
                                     "Phone": .myNumber], withCompletionBlock: { (error, ref) in
                                        if error != nil {
                                            completion(error!)
                                        } else {
                                            completion(nil)
                                        }
        })
    } else {
        completion(NSError(domain: "Unavailable", code: 0, userInfo: nil))
    }
}

// call the method like this to perform the save
func storeUser(completion: @escaping(_ completed: Bool, _ error: NSError?)-> Void) { 
     if let user = Auth.auth().currentUser { 
         myID = user.uid 
         myName = user.displayName 
         myEmail = user.email 
           // etc.,
         completion(true,nil) 
     } else { 
         completion(false,NSError(domain: "No Current User", code: 1, userInfo: nil)) 
     } 
 }