简体   繁体   中英

Swift Firebase - User Registration with Profile Photo

I'm trying to figure out how to register user information and add profile photo. The problem is Firebase documentation only provide way to create user with email and password, it works, but how do i follow to register more information ?

func createNewUser(name: String, email: String, phone: String, photo: UIImage, password: String, onCompletion: @escaping (Bool, RequestErrors?) -> Void) {
    Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
        if let error = error {
            print("Login error: \(error.localizedDescription)")
            onCompletion(false, .authError)
            return
        }
        onCompletion(true, nil)
    }
}

I'm passing the informations to this function by parameters but i don't know how to proceed. Any help ?

To store more information for the user, you need to use either Firebase Database or Firestore. You'd have to add one to your pod file.

For example, here's how setting the email for the user in the database would work

var ref: DatabaseReference!

ref = Database.database().reference()

self.ref.child("users").child(user.uid).setValue(["email": user's email])

For image storage you need to use Storage, and it would work similarly. You would also need to update your pod file. You would create a reference to store the images. So something like users/profilepics/uid.jpg Then you'd have an upload task to upload the image to the reference. Check out the Firebase Storage documentation on this for more.

That is the final code.

func createNewUser(name: String, email: String, phone: String, photo: UIImage, password: String, onCompletion: @escaping (Bool, RequestErrors?) -> Void) {
    Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
        if let error = error {
            print("Login error: \(error.localizedDescription)")
            onCompletion(false, .authError)
            return
        }

        let imageName = UUID().uuidString
        let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).jpg")

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

        if let uploadData = photo.jpegData(compressionQuality: 0.1) {
            storageRef.putData(uploadData, metadata: nil, completion: { (_, error) in
                if let error = error {
                    print(error)
                    return
                }
                storageRef.downloadURL(completion: { (url, error) in
                    if let error = error {
                        print(error)
                        return
                    }
                    guard let photoUrl = url else { return }
                    let values = ["displayName": name, "email": email, "phoneNumber": phone, "photoURL": photoUrl.absoluteString]
                    let ref = Database.database().reference()
                    let usersReference = ref.child("users").child(uid)
                    usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
                        if err != nil {
                            print(err!)
                            return
                        }
                        User.shared = User(uid: uid, displayName: name, email: email, phoneNumber: phone, photoURL: photoUrl)
                        onCompletion(true, nil)
                    })
                })
            })
        }
    }

When i get all the data that i need i fill my User singleton.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM