首页 > 解决方案 > 当前 Firebase 用户返回 Nil

问题描述

我有一个使用 FirebaseAuth 对用户进行身份验证的应用。我在 AuthService 类中执行此操作;

import Foundation
import FirebaseAuth
class AuthService: NSObject {
    
    private override init() {}
    
    static let shared = AuthService()
    
    let currentUser = Auth.auth().currentUser
    var uid = Auth.auth().currentUser?.uid

    func checkAuthState(completion: @escaping(FirebaseAuth.User?) -> Void) {
//        let listener = Auth.auth().addStateDidChangeListener { (auth, user) in
        Auth.auth().addStateDidChangeListener { (auth, user) in
            switch user {
            case .none:
                print("USER NOT FOUND IN CHECK AUTH STATE")
                completion(nil)
            case .some(let user):
                print("USER FOUND WITH ID: \(user.uid)")
                completion(user)
            }
        }
//        Auth.auth().removeStateDidChangeListener(listener)
    }
}

然后我使用上面的 checkAuthState 函数匿名登录用户。一旦此操作与某个用户完成,我将执行一个操作来切换rootViewController.

func checkAuthState() {
        authService.checkAuthState { (user) in
            switch user {
            case .none:
                SceneDelegate.shared.rootController.showWelcomeController()
            case .some(_):
                SceneDelegate.shared.rootController.showTabBarController()
            }
        }
    }
}

这是我的代码RootViewController

func showWelcomeController() {
        let welcomeController = WelcomeController(authService: AuthService.shared)
        let navigationController = UINavigationController(rootViewController: welcomeController)
        addChild(navigationController)
        navigationController.view.frame = view.frame
        view.addSubview(navigationController.view)
        navigationController.didMove(toParent: self)
        currentController.willMove(toParent: nil)
        currentController.view.removeFromSuperview()
        currentController.removeFromParent()
        currentController = navigationController
    }
    
    func showTabBarController() {
        let tabBarController = TabBarController()
        addChild(tabBarController)
        tabBarController.view.frame = view.frame
        view.addSubview(tabBarController.view)
        tabBarController.didMove(toParent: self)
        currentController.willMove(toParent: nil)
        currentController.view.removeFromSuperview()
        currentController.removeFromParent()
        currentController = tabBarController
    }

以上所有操作都按预期运行并显示了我的TabBarController,但是,然后我在我的 TabBarController 中的一个 ViewController 中运行一个函数,它是;

var uid = Auth.auth().currentUser?.uid

func readComments(query: Query, lastDocument: DocumentSnapshot?, completion: @escaping(Result<[CommentModel], NetworkError>) -> ()) {
        guard let uid = auth.uid else { return completion(.failure(.userNotFound)) }
// Other code

}

但是,我收到 .userNotFound 错误?因为它返回零?我很困惑,因为 checkAuthState 正在返回用户?

标签: iosswiftfirebase-authentication

解决方案


这通常意味着在currentUser代码运行时尚未初始化。由于恢复用户的登录状态需要调用服务器,它是一个异步操作。

任何依赖于当前用户状态被恢复的代码都应该addStateDidChangeListener在你的第一个片段中(或从中调用)。因此,您可能也需要在其他视图控制器中使用类似的片段。


推荐阅读