Reputation: 4066
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
Upvotes: 3
Views: 2839
Reputation: 4066
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
}
Upvotes: 4
Reputation:
To delete google and facebook user from firebase, you must have to Re-authenticate user. Deleting an account require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails with the FIRAuthErrorCodeCredentialTooOld
error. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticate
.
let user = Auth.auth().currentUser
var credential: AuthCredential
// Prompt the user to re-provide their sign-in credentials
user?.reauthenticate(with: credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}
Now you can delete a user account with the deleteWithCompletion
method.
let user = Auth.auth().currentUser
user?.delete { error in
if let error = error {
// An error happened.
} else {
// Account deleted.
}
}
Upvotes: 1