首页 > 解决方案 > 如何在不关闭身份验证处理程序的情况下创建 Firebase 用户

问题描述

当新用户创建帐户时,创建帐户的函数会在用户及其个人资料图片完全写入 Firebase 存储和 Firebase Firestore 之前触发身份验证处理程序。处理程序将应用导航到另一个页面,在该页面中 viewDidLoad 开始根据 Firestore 信息获取提要 - 但此时并未写入用户。

 func didSignUp() {
    // Check the that the fields are not empty
    guard let name = nameField.text, !name.isEmpty else { return }
    guard let email = emailField.text, !email.isEmpty else { return }
    guard let password = passwordField.text, !password.isEmpty else { return }
    guard let image = imageView.image else { return }


    // If successful thus far, create a user
    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in

        //  If there is no error creating a user
        if error == nil {

            // Reset the input fields
            self.nameField.text = nil
            self.emailField.text = nil
            self.passwordField.text = nil
            self.imageView.image = UIImage(named: "New User")


            // Upload the profile image to Firebase Storage
            self.uploadProfileImage(image, completion: { (url) in

                // Check to see if url is returned
                if url == nil {
                    print("Unable to upload profile image")
                }

                // Try to set the user's displayName and photoURL with a changeRequest
                let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
                changeRequest?.displayName = name
                changeRequest?.photoURL = url
                changeRequest?.commitChanges(completion: { (error) in

                    // If there is an error, alert user
                    if error != nil {
                        print("Error commiting change request: \(error!.localizedDescription)")
                    }

                    // Once change request has succeeded, save profile to Firebase Firestore
                    self.saveProfile(name: name, photoURL: url!) { success in

                        if !success {
                            print("Failed to save user to Firestore")
                        } else {
                            self.navigateUserToHomePage()
                        }
                    }
                })

            })
        } else {
            let alertController = UIAlertController(title: "Error Creating Account", message: error?.localizedDescription, preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}

我应该使用 Grand Central Dispatch 异步运行一些代码吗?

编辑:我已经上传了 Firebase 函数

// Upload image data to Firebase Storage and return a URL of image if successful
func uploadProfileImage(_ image: UIImage, completion: @escaping (_ url: URL?) ->()) {

    // Create reference to the profile pictures and save as uid.jpg
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child("profile_pictures/\(uid).jpg")

    // Compress UIImage to jpegData
    guard let imageData = image.jpegData(compressionQuality: 0.5) else { return }

    // Create the file metadata
    let metadata = StorageMetadata()
    metadata.contentType = "image/jpeg"

    // Upload the image and metadata to the object profile_pictures/uid.jpg
    storageRef.putData(imageData, metadata: metadata) { (metadata, error) in

        // Check for errors in uploading data to Firebase Storage
        guard metadata != nil, error == nil else {
            print(error?.localizedDescription ?? "error")
            completion(nil)
            return
        }

        // Get access to the the download URL after upload
        storageRef.downloadURL(completion: { (url, error) in

            // Check for errors in accessing download URL
            guard let downloadURL = url else {
                print("Couln't get profile image url")
                return
            }

            // Return download URL of uploaded image
            completion(downloadURL)
        })
    }
}

// Save user data to Firebase Firestore
func saveProfile(name: String, photoURL: URL, completion: @escaping (Bool) -> ()) {

    guard let uid = Auth.auth().currentUser?.uid else { return }

    // Create data to save as a dictionary of [String: Any]
    let userToSave: [String: Any] =  [Constants.UserKeys.uid: uid,
                                      Constants.UserKeys.name: name,
                                      Constants.UserKeys.photoURL: photoURL.absoluteString]

    // Create a document reference for Firestore
    docRef = Firestore.firestore().collection("users").document(uid)

    // Save data to Firestore reference
    docRef.setData(userToSave) { (error) in
        if let error = error {
            print("Error saving to Firestore: \(error.localizedDescription)")
            completion(false)
        }
        completion(true)
    }
}

标签: swiftfirebasefirebase-authenticationgoogle-cloud-firestore

解决方案


推荐阅读