Reputation: 2017
In my SettingsViewModel
I have the following:
class SettingsViewModel: ObservableObject {
func deleteUser(){
let userId = Auth.auth().currentUser!.uid
Firestore.firestore().collection("users").document(userId).delete() { err in
if let err = err {
print("error: \(err)")
} else {
print("Deleted user in db users")
Storage.storage().reference(forURL: "gs://myapp.appspot.com").child("avatar").child(userId).delete() { err in
if let err = err {
print("error: \(err)")
} else {
print("Deleted User image")
Auth.auth().currentUser!.delete { error in
if let error = error {
print("error deleting user - \(error)")
} else {
print("Account deleted")
}
}
}
}
}
}
}
}
In my setting view im calling the function in a button like so:
@ObservedObject var settingsViewModel = SettingsViewModel()
func logout() {
session.logout()
}
Button(action: {
self.showActionSheet = true
}) {
Text("Delete Account").foregroundColor(.white).padding()
}.background(Color.red)
.cornerRadius(10)
.padding(.top, 35)
.actionSheet(isPresented: self.$showActionSheet) {
ActionSheet(title: Text("Delete"), message: Text("Are you sure you want to delete your account?"),
buttons: [
.default(Text("Yes, delete my account."), action: {
self.deleteUser()
self.session.logout()
self.showActionSheet.toggle()
}),.cancel()
])
}
This doesn't work correctly as it deletes the account:
Auth.auth().currentUser!.delete { error in
if let error = error {
print("error deleting user - \(error)")
} else {
print("Account deleted")
}
}
And then signs out, leaving the other data, but if i remove:
Auth.auth().currentUser!.delete { error in
if let error = error {
print("error deleting user - \(error)")
} else {
print("Account deleted")
}
}
then it deletes the user data, but then doesn't delete the storage:
Storage.storage().reference(forURL: "gs://myapp.appspot.com").child("avatar").child(userId).delete()
Im trying to get it to flow so it deletes the user data, then the image, then the auth data and then logout out the app. all the functions work, but putting them together is causing an issue.
Upvotes: 1
Views: 2015
Reputation: 7254
The recommended way for doing this is:
user.delete()
You might want to check out the Delete User Data Extension, which covers step 1 and 2 for you.
Upvotes: 2