检查didFinishLaunchingWithOptions中的无效用户令牌firebase

问题描述 投票:0回答:1

根据Google支持,检测已撤销的firebase令牌的唯一方法是向我的Firestore发出读取请求并捕获FIRAuthErrorCodeUserTokenExpired错误。

我在iOS应用程序的didFinishLaunchingWithOptions()中尝试过它。正确读取数据,但是因为我停用了测试用户帐户(令牌应该被撤销),应该会出现错误。不幸的是,Firebase仍会处理请求并在用户中登录而不会产生错误。

对于应该尽可能安全的应用程序,如果您已经撤销了令牌而仍然登录到应用程序,那么这是非常不理想的。

这是我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    window = UIWindow()
    if Auth.auth().currentUser == nil { // check if there is a User logged in
        window?.rootViewController = LoginViewController() // set the MainTabBarController as the root (start) controller
    } else{
        if let uid = Auth.auth().currentUser?.uid{
            let firRef = Firestore.firestore().collection("idols").document(uid)
            firRef.getDocument { (document, error) in
                if let err = error {
                    print(err)
                    self.window?.rootViewController = LoginViewController()
                }else{
                    guard let doc = document, let data = doc.data() else {return}
                    guard let username = data["username"] as? String, let uid = data["uid"] as? String, let biography = data["biography"] as? String else {return}
                    self.activeUser = User(uid: String(uid), username: username, biography: biography)
                    print(self.activeUser)
                    self.window?.rootViewController = MainTabBarController()
                }
            }
        }
        window?.rootViewController = LoginViewController()
    }


    return true
}

这是我的Firebase规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

我知道规则尚未完全调整,但在我看来,Firebase应该抛出错误,因为令牌已过期。我知道我还没有检查过正确的错误,但if if不会被调用。

我检查了多个资源,包括与此主题相对应的Firebase documentation,但文档仅介绍了如何在Web中检测已撤销的令牌,而不是在iOS或Android上。

除此之外,我检查了一个stack overflow条目,但它没有正确的答案,这是有帮助所以我决定在这里发布我自己的一个:

有没有人知道如何在iOS上发现撤销令牌的正确方法?

如果这个问题没有完美表达,我很抱歉。我仍然是堆栈溢出的新手,所以请善待:)

ios firebase firebase-authentication google-cloud-firestore firebase-security-rules
1个回答
0
投票

我找到了以下解决方案:

在didFinishLaunchingWithOptions中添加以下代码:

if Auth.auth?.currentUser == nil {
   // You need to prompt the user login interface
} else {
  Auth.auth().currentUser?.reload(completion: { (error) in
    if error != nil {
        if let err = error as NSError?{
            if let error = AuthErrorCode(rawValue: err.code){
                switch error{
                // You need to prompt the user login interface
                case .invalidCredential: print("Invalid credentials")
                case .invalidUserToken: print("Invalid User Token")
                case .userTokenExpired: print("User Token Expired")
                case .invalidCustomToken: print("Invalid Custom Token")
                case .customTokenMismatch: print("Custom token mismatch")
                case .userDisabled: print("User disabled")
                case .userNotFound: print("User not found")
                default: print("call default error")
                }
            }
        }
    }
    else {
        print("Valid Token")
    }
  })
}

啊,在运行此代码之前不要忘记初始化Firebase(否则您将无法访问Firebase Auth)

© www.soinside.com 2019 - 2024. All rights reserved.