IOS SWIFT: Unable to remove user from Firebase database

I want to remove the current user from Firebase. However, the authenticated user is deleted, but I cannot delete the data for this user in the database. What am I doing wrong?

This is my method of deleting a user ....

FIRAuth.auth()?.signIn(withEmail: (emailTextField?.text)! , password: (passwordTextField?.text)!, completion: { (user, error) in if error == nil { print("User Authenticate!!!") let user = FIRAuth.auth()?.currentUser user?.delete(completion: { (error) in if error != nil { print("Error unable to delete user") } else { DataService.ds.deleteCurrentFirebaseDBUser() KeychainWrapper.standard.removeObject(forKey: KEY_UID) self.performSegue(withIdentifier: "goToLogin", sender: nil) } }) } else { //Password was wrong, unable to authenicate user. Data is not updated print("!!!ALERT!!! Unable to authenticate user") let alert = UIAlertController(title: "Incorrect Password", message: "Please re-enter your password", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil) } }) 

Firebase Rules:

 { "rules": { "users": { "$uid": { ".read": "$uid === auth.uid", ".write": "$uid === auth.uid" } } } } 

Database:

 App -> users -> 4erkjkl543jfe46 ->name ->email 

ERRORS:

2017-01-21 21: 33: 10.321704 APP [11582: 4102711] [FirebaseDatabase] setValue: or removeValue: at / users / 4erkjkl543jfe46 failed: permission_denied

Optional (Error Domain = com.firebase Code = 1 "Permission denied" UserInfo = {NSLocalizedDescription = Permission denied})

+5
source share
2 answers

I have the same problem. You cannot use your deleteCurrentFirebaseDBUser() function because the Firebase delete function (if successful) deletes the user's auth object.

As a result, the user is no longer authenticated while you want to delete the user data in the database using deleteCurrentFirebaseDBUser() .

I am currently deleting user data in the database before the Firebase delete function, which is not an ideal solution.

+3
source

We can remove the user from both sides of the authentication and database. But before that, we need to re-authenticate the user, and then we will receive the last token to delete the user.

Here is a nice code:

  let user = Auth.auth().currentUser user?.reauthenticate(with:credential) { error in if let error = error { // An error happened. showAlertWithErrorMessage(message: error.localizedDescription) } else { // User re-authenticated. user?.delete { error in if let error = error { // An error happened. showAlertWithErrorMessage(message: error.localizedDescription) } else { // Account deleted. let userID = HelperFunction.helper.FetchFromUserDefault(name: kUID) Database.database().reference(fromURL: kFirebaseLink).child(kUser).child(userID).removeValue() try! Auth.auth().signOut() showAlertWithErrorMessage(message: "Your account deleted successfully...") return } } } } 

100% working in my project and well tested

0
source

Source: https://habr.com/ru/post/1263156/


All Articles