升级到Firebase 5并出现身份验证问题。无法转换类型为'(User ?, Error?)的值

时间:2018-07-12 15:22:23

标签: ios swift firebase

我刚刚为Firebase升级了吊舱,但出现了很多错误。我在Firebase文档网页上使用了文档,因此能够修复其中的大多数文档。我遇到了这个错误,需要帮助。

这是我的错误:(我将其标记为错误1和错误2。)

  1. 无法将类型'((User ?, Error?)->()'的值转换为预期的参数类型'AuthDataResultCallback?' (aka'可选<(可选,可选)->()>')

  2. “用户”类型的值?没有成员'updateEmail'

这是我的AuthService.Swift代码:(所有相同的Swift代码,只是拆分以显示错误)

import Foundation
import Firebase

class AuthService {

    static func signIn(email: String, password: String, onSuccess: @escaping () -> Void, onError:  @escaping (_ errorMessage: String?) -> Void) {
        Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
            if error != nil {
                onError(error!.localizedDescription)
                return
            }
            onSuccess()
        })

    }

    static func signUp(username: String, email: String, password: String, imageData: Data, onSuccess: @escaping () -> Void, onError:  @escaping (_ errorMessage: String?) -> Void) {

错误1。

  

Auth.auth()。createUser(withEmail:电子邮件,密码:密码,   完成:{(用户:用户?,错误:错误?)

            if error != nil {
                onError(error!.localizedDescription)
                return
            }

            let uid = user?.uid
            let storageRef = FIRStorage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("profile_image").child(uid!)

            storageRef.put(imageData, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    return
                }
                let profileImageUrl = metadata?.downloadURL()?.absoluteString

                self.setUserInfomation(profileImageUrl: profileImageUrl!, username: username, email: email, uid: uid!, onSuccess: onSuccess)
            })
        })

    }

    static func setUserInfomation(profileImageUrl: String, username: String, email: String, uid: String, onSuccess: @escaping () -> Void) {
        let ref = Database.database().reference()
        let usersReference = ref.child("users")
        let newUserReference = usersReference.child(uid)
        newUserReference.setValue(["username": username, "username_lowercase": username.lowercased(), "email": email, "profileImageUrl": profileImageUrl])
        onSuccess()
    }

    static func updateUserInfor(username: String, email: String, imageData: Data, onSuccess: @escaping () -> Void, onError:  @escaping (_ errorMessage: String?) -> Void) {

错误2。

  

Api.User.CURRENT_USER.updateEmail(电子邮件,完成:{(错误)于

            if error != nil {
                onError(error!.localizedDescription)
            }else {
                let uid = Api.User.CURRENT_USER?.uid
                let storageRef = FIRStorage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("profile_image").child(uid!)

                storageRef.put(imageData, metadata: nil, completion: { (metadata, error) in
                    if error != nil {
                        return
                    }
                    let profileImageUrl = metadata?.downloadURL()?.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)
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果您查看release notes,就会发现他们改变了一些东西。从本质上讲,他们现在不再返回用户,而是返回包含用户的另一个对象(AuthDataResult)。

这意味着您需要FIRAuthDataResultCallback

答案 1 :(得分:0)

Database5改变了很多语法,

在error1中,您可以尝试以下操作:

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)
                }
            })
        })
    }
}

在error2中,它与error1相同,您可以尝试以下方法:

        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)
    }
}

您可以再次阅读Firebase的文档以了解https://firebase.google.com/docs/ios/setup有什么区别