首页 > 解决方案 > 升级到 Firebase 5 并遇到身份验证问题。无法转换类型“(用户?,错误?)

问题描述

我刚刚为 Firebase 升级了我的 pod,并遇到了很多错误。我使用了 Firebase Docs 网页上的 Docs,所以我能够修复其中的大部分。这个错误我被困住了,需要一些帮助。

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

  1. 无法将类型“(用户?,错误?)->()”的值转换为预期的参数类型“AuthDataResultCallback?” (又名'可选<(可选,可选)->()>')

  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: email, password: password, 完成: { (user: User?, error: Error?) in

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

标签: iosswiftfirebase

解决方案


如果您查看发行说明,您会发现它们更改了一些内容。本质上,它们不是返回用户,而是返回另一个包含用户的对象( AuthDataResult )。

这意味着您需要FIRAuthDataResultCallback


推荐阅读