Reputation: 284
I'm struggling with problem with re-authentication user with firebase email link. I cannot delete user account.
The process looks like bellow:
UserDefaults
works It throws an error:
This is the code for delete user account:
func deleteAccount() {
// Create progres HUD
let hud = JGProgressHUD(style: .dark)
hud.textLabel.text = NSLocalizedString("Deleting Account", comment: "")
hud.show(in: self.view)
// #1 Create firebase credential to re-authenticate user
let currentUser = Auth.auth().currentUser
guard let email = Auth.auth().currentUser?.email else {return}
let link = UserDefaults.standard.string(forKey: "Link")
let credential = EmailAuthProvider.credential(withEmail: email, link: link!)
// #2 Re-authenticate user
currentUser?.reauthenticate(with: credential, completion: { (result, error) in
// #3 if there is no error, remove user from database
if error == nil {
currentUser?.delete(completion: { (error) in
guard let userID = currentUser?.uid else {return }
let ref = Database.database().reference()
ref.child("User").child(userID).removeValue()
if error == nil{
hud.dismiss()
self.delegate?.deletePinAnnotation(email: email)
self.navigationController?.popViewController(animated: true)
} else {
print(error.debugDescription)
}
} else { // #4 If error when re-authenticate user occurs
print(error.debugDescription)
}
hud.dismiss()
}
Upvotes: 0
Views: 408
Reputation: 284
It turned out that I should not use the same link that I used when creating the account. During reauthentication I should send another link.
using the same method:
Auth.auth().sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings) { (error) in
// ...
if let error = error {
self.createAlert(withDescription: error.localizedDescription)
print(error)
return
}
// The link was successfully sent. Inform the user.
// Save the email locally so you don't need to ask the user for it again
// if they open the link on the same device.
UserDefaults.standard.set(email, forKey: "Email")
self.createAlert(withDescription: "Open email to check the link")
// ...
}
Upvotes: 1