How can I detect if the user was deleted from Firebase auth?
firebase, firebase-authentication, swift
Solution
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 = Auth.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 in the Firebase Console. So like this it should just show the welcome screen if user has been deleted.
Problem
Two apps are in the same Firebase project. One app deleted a user from Firebase Auth. But another app still get user uid from somewhere, maybe cache, and access the data in Firebase database with Auth's uid too. I mean I can still get "user.uid" by `let user = FIRAuth.auth().currentUser`, if keeping sign-in. The apps both set `FIRDatabase.database().persistenceEnabled = true`. I would like to sing out or refresh cache at all apps if the user was deleted from Auth accordingly.