简体   繁体   中英

SwiftUI & Firebase: How to delete a user?

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.

The recommended way for doing this is:

  1. Write a Cloud Function that triggers when the user account is deleted in Firebase Auth (see user.onDelete() )
  2. In this function, delete all the user's data in Cloud Firestore, Cloud Storage
  3. Trigger the function by deleting the user by calling user.delete()

You might want to check out the Delete User Data Extension , which covers step 1 and 2 for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM