Ad

How To Update Email Address In Firebase Authentication In Swift 4

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{
            }
        })
    }
Ad

Answer

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
                }
            }
        }
    }
}
Ad
source: stackoverflow.com
Ad