Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Google or Facebook User from Firebase

To delete a user from the authentication tab I am using this code:

    if let user = FIRAuth.auth()?.currentUser{
            user.delete(completion: nil)
    }

And this works just fine when the user signed-up with an e-mail/password combination but when they signed up with a social account they are NOT deleted and I can still see their email, UID etc in the Authentication tab.

How can I delete those users from Firebase as well?

Thanks in advance

like image 686
JP Aquino Avatar asked Apr 19 '26 13:04

JP Aquino


1 Answers

Thank you Jogendra, reauthentication was the key to solving my problem and I accepted the answer. This is a complete answer using Swift 3 that includes code to retrieve the user credentials.

 func deleteAllUserData(){
        if let user = FIRAuth.auth()?.currentUser{
            user.delete(completion: { (err) in
                if(err != nil){
                print(err!.localizedDescription)
                //Try to re-authenticate if there is an error
                self.getProvider()
                }else{
                self.deleteData()
                }
            })
        }
    }

//There are different methods to retrieve the credentials based on the auth type so you first need to know which type of authentication is being used

     func getProvider(){
        if let providerData = FIRAuth.auth()?.currentUser?.providerData {
            for userInfo in providerData {
                switch userInfo.providerID {
                case "facebook.com":
                    if let credential = facebookCredential(){
                        self.reauthenticate(credential: credential)
                    }
                case "google.com":
                    if let credential = googleCredential(){
                        self.reauthenticate(credential: credential)
                    }
                    print("user is signed in with google")
              case "password":
                    let alert = UIAlertController(title: "Sign In", message: "Please Sign In again to your Locket account to confirm that you want to delete all your data", preferredStyle: .alert)
                    alert.addTextField { (textField: UITextField) in
                        textField.placeholder = "Email"
                    }
                    alert.addTextField { (textField: UITextField) in
                        textField.placeholder = "Password"
                        textField.isSecureTextEntry = true
                    }
                    let noAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

                    let yesAction = UIAlertAction(title: "OK", style: .destructive, handler: { (action:UIAlertAction) in
                         let emailTextField = alert.textFields![0]
                         let passwordTextField = alert.textFields![1]

                        if let credential = self.emailCredential(email: emailTextField.text!, password: passwordTextField.text!){
                            self.reauthenticate(credential: credential)
                        }else{
                            print("error")
                        }
                    })
                    alert.addAction(yesAction)
                    alert.addAction(noAction)

                self.present(alert, animated: true, completion: nil)
                default:
                print("unknown auth provider")


                }
            }
            }
        }

   func reauthenticate(credential:FIRAuthCredential){
        FIRAuth.auth()?.currentUser?.reauthenticate(with: credential) { error in
            if let error = error {
                print("reauth error \(error.localizedDescription)")
            } else {
                print("no reauth error")
                self.deleteData()
            }
        }
    }

    func facebookCredential() -> FIRAuthCredential? {
        let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
        return credential
    }

    func emailCredential(email:String,password:String) -> FIRAuthCredential? {
       let credential = FIREmailPasswordAuthProvider.credential(withEmail: email, password: password)
       return credential
    }

    func googleCredential() -> FIRAuthCredential? {
        guard let user = GIDSignIn.sharedInstance().currentUser else {return nil}
        guard let authentication = user.authentication else {return nil}
        let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
        return credential
    }


     func deleteData(){
            // Delete all other data related to user
        }
like image 113
JP Aquino Avatar answered Apr 21 '26 01:04

JP Aquino