Upgraded to Firebase 5 and having Auth issues. Cannot convert value of type '(User?, Error?)

前端 未结 2 448
旧时难觅i
旧时难觅i 2020-12-11 14:45

I just upgraded my pods for Firebase and got a ton of errors. I used the Docs on the Firebase Docs webpage so I was able to fix most of them. This error I\'m stuck on and ne

2条回答
  •  伪装坚强ぢ
    2020-12-11 15:01

    Database5 is change a lot of syntax,

    In error1, you can try this:

    static func signUp(username:String, email:String, password: String, imageData:Data, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
        Auth.auth().createUser(withEmail: email, password: password) { (user , error) in
            if error != nil {
                onError(error!.localizedDescription)
                return
            }
            //Here you have to change the uid 
            let uid = user?.user.uid
            //It should be Storage.storage().reference()
            let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("profile_image").child(uid!)
            //storageRef.put was removed and it's changed be storage.putData 
            storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    return
                }
                //downloadURL is changed too, you can read at documents of Firebase to know how to use this
                storageRef.downloadURL(completion: { (url, error) in
                    if let error = error { 
                        return
                    }else {
                        let profileImageUrl = url!.absoluteString
                        self.setUserInformation(profileImageUrl: profileImageUrl, username: username, email: email, uid: uid!, onSuccess: onSuccess)
                    }
                })
            })
        }
    }
    

    And in error2, it same error with error1, you can try this:

            Api.User.CURRENT_USER?.updateEmail(to: email, completion: { (error) in
            if error != nil {
                onError(error?.localizedDescription)
            }else {  
                let uid = Api.User.CURRENT_USER?.uid
                //Change here
                let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REF).child("profile_image").child(uid!)
                //Change here
                storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
                    if error != nil {
                        return
                    }
                    storageRef.downloadURL(completion: { (url, error) in
                        if let error = error {
                            return
                        }else {
                            let profileImageUrl = url!.absoluteString
                            self.updateDatabase(profileImageUrl: profileImageUrl, username: username, email: email, onSuccess: onSuccess, onError: onError)
                        }
                    })
                })
            }
        })
    
    
    static func updateDatabase(profileImageUrl: String, username: String, email: String, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
        let dict = ["username": username, "username_lowercase": username.lowercased(), "email": email, "profileImageUrl": profileImageUrl]
        Api.User.REF_CURRENT_USER?.updateChildValues(dict, withCompletionBlock: { (error, ref) in
            if error != nil {
                onError(error?.localizedDescription)
            }else {
                onSuccess()
            }
        })
    }
    
    
    static func logOut(onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
        do {
            try Auth.auth().signOut()
            onSuccess()
        }catch let logoutError {
            onError(logoutError.localizedDescription)
        }
    }
    

    You can read document of Firebase again to see what difference https://firebase.google.com/docs/ios/setup

提交回复
热议问题