Reputation:
So I built a simple app using Firebase Authentication (with just email and password) and it was working great, but in order to TestFlight my sign up/login page I needed to delete the accounts of everyone who had signed up with the app previously, only to find that deleting the users on the console doesn't actually deauth them in the app. I would imagine there would be a way to check a user's authentication status in the Firebase console (if they exist or not at least) but I can't find that functionality to save my life. Any help is welcome and appreciated!
Upvotes: 2
Views: 4275
Reputation: 88
The code below works great on android to confirm if the Firebase Auth user still exists (has not been deleted or disabled) and has valid credentials.
Deleting the Auth user from the firebase console does not revoke auth tokens on devices the user is currently logged in as the token is cached locally. Using reload() forces a check with the firebase auth server.
mFirebaseUser.reload().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
//User still exists and credentials are valid
}else {
//User has been disabled, deleted or login credentials are no longer valid,
//so send them to Login screen
}
}
});
Upvotes: 5
Reputation:
I spent all day since posting this trying to do the whitelist solution and couldn't get it to work. However, a friend of mine sent me a solution similar to this and it works like a charm.
func checkUserAgainstDatabase(completion: (success: Bool, error: NSError?) -> Void) {
guard let currentUser = FIRAuth.auth()?.currentUser else { return }
currentUser.getTokenForcingRefresh(true) { (idToken, error) in
if let error = error {
completion(success: false, error: error)
print(error.localizedDescription)
} else {
completion(success: true, error: nil)
}
}
}
Upvotes: 2
Reputation: 525
I ran into this same issue and found a workaround that I've been using ever since. Instead, I just query my database in /users (a category I created for users) and check if my current ID exists. If it does not, I know the account has been deleted. This means you need to create an entry with your userID in /users on sign up and delete this entry when you delete the account. To see if currently authenticated user is deleted then, do something like this:
NSString *currentID=[[FIRAuth auth].currentUser uid];
[[[[[FIRDatabase database]reference]child:@"users"]child:currentID]observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
if (snapshot!=[NSNull Null]) {
//User still exists
} else {
//Account no longer exists (deleted)
}
}];
Upvotes: 3