I'm struggling with problem with re-authentication user with firebase email link. I cannot delete user account.
The process looks like bellow:
- User creates an account by adding email address
- Link is successfully sent on email
- Universal link works
- Authentication by clicking on link works
- User account is created on Firebase
- Saving this link on
UserDefaultsworks - Deleting account when re-authenticate user does not work...
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() } 