问题
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.
回答1:
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.
回答2:
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)
}
})
}
来源:https://stackoverflow.com/questions/40252218/how-can-i-detect-if-the-user-was-deleted-from-firebase-auth