1

When I delete an account on my Firebase console, the user stills logged in to my app, I use this code to keep the user logged. How can I log out the user when I delete his account?

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)

    //check if user is logged in
    if FIRAuth.auth()?.currentUser != nil {
        //if user if logged in 
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC")
        self.present(vc!, animated: false, completion: nil)
    }

}

But I don't know how to check if the account that is using the user is valid or not (if the account continues at the firebase console or not) before the "automatic log in". Hope someone can help me out!!! Thanks in advance!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
killerwar557
  • 29
  • 1
  • 7
  • See http://stackoverflow.com/questions/37625064/deleted-app-and-reinstalled-and-user-is-still-logged-in – Frank van Puffelen May 05 '17 at 20:46
  • But that`s more focus on log out the user when uninstall the app and I want to log out the user if I delete the user account in firebase. Thank you. – killerwar557 May 05 '17 at 22:39
  • Sorry about that. I was looking for something close enough that was iOS specific. When you delete the user, their token stay valid until the next (hourly) token refresh. If you want to disallow access, you will have to do that on the other side. See http://stackoverflow.com/questions/38195656/how-to-set-firebase-database-rules-how-to-prevent-write-from-deleted-user/38207670#38207670 – Frank van Puffelen May 06 '17 at 00:10

2 Answers2

2

We have similar implementation, so hopefully this solves your problem.

if let currentUser = Auth.auth().currentUser {
    currentUser.getIDTokenForcingRefresh(true) { error in
        if let error = error {
            // log out
        } else {
            // go in
        }
    }
} else {
    // log in
}

This forces Auth to communicate with Firebase to get a new authentication token, which would fail if the user was deleted.

zgosalvez
  • 384
  • 1
  • 5
  • 22
0

The Kotlin implementation would be :

firebaseAuth.currentUser?.getIdToken(true)
firebaseAuth.currentUser == null
Julian Paul
  • 162
  • 1
  • 2