Can I delete FIRAuth users using their User UID?

后端 未结 2 1673
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 15:06

I have create user used as Admin in firebase auth. Now I want to login and use Admin account to delete other account by using their FIRAuth User UID. How to implement in swift?<

相关标签:
2条回答
  • 2021-01-29 15:31

    The client-side SDKs for Firebase can only delete the currently authenticated user. They do not allow deleting any other user, or deleting a user identified by their UID.

    This means that you cannot implement administrative functionality like deleting an arbitrary user in Swift.

    Instead you will have to use the Firebase Admin SDK to delete an arbitrary user or allow your iOS users to delete their own account. Given the actions the Admin SDK allows its user to perform, it should only be used in trusted environments, such as a server you control or Cloud Functions for Firebase.

    0 讨论(0)
  • 2021-01-29 15:45

    You can remove/delete user like this:

    Note: Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in.

     let credential: AuthCredential
    
                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.
                                Database.database().reference(fromURL: kFirebaseLink).child(kUser).child(userID).removeValue()
    
                                try!  Auth.auth().signOut()
                                self.navigationController?.popToRootViewController(animated: true)
                            }
                        }
    
                    }
                }
            }else{
                showAlertWithErrorMessage(message: "Try again later")
            }
    

    This way achieve delete user functionality in my ios app.(remove user from firebase authentication and database both side)

    100% working and tested

    0 讨论(0)
提交回复
热议问题