-3

After leaving Parse and moving to Firebase I faced a problem that it's only possible to signup with email and password, how can I make Firebase store the username as well?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Abdullah
  • 147
  • 2
  • 12
  • See http://stackoverflow.com/q/32151178 – Frank van Puffelen Apr 01 '17 at 16:26
  • I think the linked question talks about Android, is it the same thing to do? @FrankvanPuffelen – Abdullah Apr 01 '17 at 16:30
  • The Firebase SDKs work the same across platforms. The approach in the linked answer will work for iOS too. If you're having problems making it work, share the [minimal, complete code that reproduces where you are stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Apr 01 '17 at 16:37

2 Answers2

2

Try This...

FIRAuth.auth()?.createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user: FIRUser?, error) in
            if error != nil {
                print(error)
                return

            } else { 
            print("User Created...") }

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

            // You can set these values to whatever you want, plus add more!
            let values = ["email": emailField.text!, "username": usernameField.text!] as [String : Any]

         // Then pass your values into another function called 'RegisterUserIntoDatabase' 
         // which creates the user in your Firebase Database...   

            self.registerUserIntoDatabase(uid, values: values as [String : AnyObject])
        })


private func registerUserIntoDatabase(_ uid: String, values: [String: AnyObject]) {
    // Adding User Info
    let ref = FIRDatabase.database().reference()
    let usersReference = ref.child("users").child(uid)

    usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
        if err != nil {
            print(err)
            return
        }
        print("Successfully Added a New User to the Database")
    })
}
Tyler Rutt
  • 567
  • 2
  • 8
  • 24
0
func handleRegister(email:String, password:String, username:String) {

    //Create User
    FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, err) in

        if err != nil {
            print(err!.localizedDescription)
            return
        }

        //Success! User Created!

        if let userID = user?.uid {

            //Create User Profile
            let databaseRef = FIRDatabase.database().reference()
            let usersRef = databaseRef.child("Users").child(userID)

            let usernameValue = ["username":username]

            usersRef.updateChildValues(usernameValue, withCompletionBlock: { (err, ref) in

                if err != nil {
                    print(err!.localizedDescription)
                    return
                }

                //Profile created and updated!
                //You can use perform segue or present controller to go to another controller...
            })

        }

    })

}