How can I detect if the user was deleted from Firebase auth?

自作多情 提交于 2019-12-05 08:38:35

You can achieve it by doing this inside appDelegate:

//Check if user does exists
    func checkUserAgainstDatabase(completion: @escaping (_ success: Bool, _ error: NSError?) -> Void) {
        guard let currentUser = FIRAuth.auth()?.currentUser else { return }
        currentUser.getTokenForcingRefresh(true) { (idToken, error) in
            if let error = error {
                completion(false, error as NSError?)
                print(error.localizedDescription)
            } else {
                completion(true, nil)
            }
        }
    }

And you can do something like this after checking with the above function in didFinishLaunchingWithOptions:

If user does exist:

self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "CustomTabBarViewController")

else:

self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "WelcomeViewController")

And to test if it did work simply remove user manually from the Auth manager from firebase. So like this it should just show the welcoome screen if user has been deleted.

with Firebase 4.0 it would look like this:

func checkUserAgainstDatabase(completion: @escaping (_ success: Bool, _ error: NSError?) -> Void) {
    guard let currentUser = Auth.auth().currentUser else { return }
    currentUser.getIDTokenForcingRefresh(true, completion:  { (idToken, error) in
      if let error = error {
        completion(false, error as NSError?)
        print(error.localizedDescription)
      } else {
        completion(true, nil)
      }
    })
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!