0

I'm struggling with problem with re-authentication user with firebase email link. I cannot delete user account.

The process looks like bellow:

  1. User creates an account by adding email address
  2. Link is successfully sent on email
  3. Universal link works
  4. Authentication by clicking on link works
  5. User account is created on Firebase
  6. Saving this link on UserDefaults works
  7. Deleting account when re-authenticate user does not work...

It throws an error:

enter image description here

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() } 

1 Answer 1

1

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") // ... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.