Tarvo Mäesepp Tarvo Mäesepp - 1 year ago 152
Swift Question

Does deleting account from Firebase automatically logs user out?

I would like to know, does deleting account from Firebase automatically logs user out?
I mean, if I want to show him after deleting the login screen, then I just have to present that VC?
I am asking this because if I do like this, it crashes and I think it is because the user doesn't exists anymore at this points.

Am I right?

The code with some explenations :

let user = FIRAuth.auth()?.currentUser

user?.deleteWithCompletion { error in
if let error = error {
// An error happened.
} else {
try! FIRAuth.auth()!.signOut()//This is unnecessary?

let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:nil)

let WelcomeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "WelcomeViewController")

//Send the user to the WelcomeViewController
self.present(WelcomeViewController, animated: true, completion: nil)
// Account deleted.

Answer Source

If you are deleting your currentUser you need to take care of two things:-

  • Delete the user's data from the Firebase Database (If there is any)
  • Delete the auth Credentials (e.g :- email-password, facebook login, twitter etc)

    To delete your current user use the below function, which also first sign's out the user

     FIRAuth.auth()?.currentUser?.delete(completion: { (err) in

    If you CMD+CLICK on the delete function it will take you to its documentation :-

Deletes the user account (also signs out the user, if this was the current user).

completion Optionally; the block invoked when the request to delete the account is complete, or fails. Invoked asynchronously on the main thread in the future.

Possible error codes: - @c FIRAuthErrorCodeRequiresRecentLogin - Updating email is a security sensitive operation that requires a recent login from the user. This error indicates the user has not signed in recently enough. To resolve, reauthenticate the user by invoking reauthenticateWithCredential:completion: on FIRUser.

See @c FIRAuthErrors for a list of error codes that are common to all FIRUser operations. */

So long story short if the err received is nil your current users's account has not only been deleted but also signed out automatically, But you will need to handle other FIRAuthErrors as stated in the documentation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download