3

i just want to update authenticate email address of current user. i have tried lot's of solution like updateEmail method of firebase but it not work !! if any one know then please tell me how can i achieved this Thanks in advance !!

enter image description here

@IBAction func btnResetEmailClick(_ sender: UIButton) { let auth = Auth.auth() guard let email = self.txtEmailAddress.text ?? auth.currentUser?.email else { return } // email that i have to update with current user email auth.currentUser?.updateEmail(to: (auth.currentUser?.email)!, completion: { (error) in if error == nil{ }else{ } }) } 
4
  • What's the problem with the code you shared? As in: what line in that code doesn't do what you expect it to do when you run it in a debugger? Commented Mar 2, 2019 at 15:24
  • Yes I debug code ! it going to if loop means it success and now what should I do next !! Commented Mar 4, 2019 at 6:11
  • It appears you're updating the email address to the same already existing email address. i.e. change (to: (auth.currentUser?.email)! to (to: (email)!. For an example, see my answer to this question. Commented Mar 4, 2019 at 18:54
  • Okay Thanks I will check and tell you :) Commented Mar 5, 2019 at 5:04

2 Answers 2

4

To change the email address the user has to be logged in recently i would suggest doing this:

var credential: AuthCredential @IBAction func changeEmail() { if let user = Auth.auth().currentUser { // re authenticate the user user.reauthenticate(with: credential) { error in if let error = error { // An error happened. } else { // User re-authenticated. user.updateEmail(to: "email") { (error) in // email updated } } } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

while credential is not initialed user.reauthenticate(with: credential) will crash.
1

This is effective method to solve it.

let user = Auth.auth().currentUser user?.updateEmail(to: "email") { error in if error != nil { // An error happened } else { // Email updated. } } 

1 Comment

What does that mean enter code here several times? your answer is somehow wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.