1
votes

I am new to firebase and I'm trying to learn how to create users. My problem is that when i use the createUserWithEmail completion block via a button, for some reason the unique identifier, the uid, is not generated. This prevents me from storing the associated username and password under the uid in the JSON tree. My code is as follows (I have defined databaseRef in a separate swift file as a global constant using "let databaseRef = FIRDatabase.database().reference()")

@IBAction func createAccount(sender: AnyObject) {

    var username = usernameField.text
    var email = emailField.text
    var password = passwordField.text
    if username != "" && email != "" && password != "" {
        FIRAuth.auth()?.createUserWithEmail(email!, password: password!, completion: { (user, error) in
            databaseRef.child("users/(user.uid)/username").setValue(username)
            databaseRef.child("users/(user.uid)/password").setValue(password)
            databaseRef.child("users/(user.uid)/uid").setValue(user?.uid) 
        })
    } else {
        print("please complete all fields")
    }
}

I know the uid is not generated for two reasons. Firstly, when i run the above code and enter in values for each of my texts fields, the app crashes. Secondly, if i delete the code that deals with setting the values and replace it with print(user.uid), a value of nil is returned. What am i missing? I can understand that the uid is a very important part of creating a user.

***Here is the solution I came up with

 @IBAction func createAccount(sender: AnyObject) {

    var username = usernameField.text
    var email = emailField.text
    var password = passwordField.text

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

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

             if let user = FIRAuth.auth()?.currentUser {

                databaseRef.child("users/\(user.uid)/username").setValue(username)
                databaseRef.child("users/\(user.uid)/password").setValue(password)
                databaseRef.child("users/\(user.uid)/email").setValue(email)

                print(user.uid)

            } else {

                print("no user")

            }

        })


    } else {

        print("please complete all fields")

    }
1
1. Is the user being created in the firebase console? 2. In your completion block, print out the returned user print(user) and make sure it is returning the user info.dylankbuckley
1. Nothing is added to the console 2. I've tried this, unfortunately a value of nil is returnedpho_pho
@pho_pho take a look at the error object returned in the callback. It might have some nice information on whats happening.adolfosrs
@adolfosrs Thanks for the suggestion, unfortunately there are no errors... Would it be sufficient to generate my own user id using a random string generator or will I run in to problems later down the line if I can't access the Firebase provided uid?pho_pho
@adolfors I figured out how to do it... For anyone interested, the way that I did it was by firstly checking that the user was logged in using the currentUser method then setting this equal to a variable from which i could then access the uid.pho_pho

1 Answers

2
votes

Swift 4, 2018 solution for anyone who's interested:

Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
    // guard against errors and optionals
    guard error == nil else { return }
    guard let user = user else { return }
    let userObject = [
        "uid": user.uid,
        "username": username,
        "password": password, // I don't recommend storing passwords like this by the way
        "email": email
        ] as [String:Any]
    databaseRef.child("users").child(user.uid).setValue(userObject)
}