How to update both Email and Password with new Firebase in swift

最后都变了- 提交于 2019-12-19 06:22:34

问题


I'm developing app with new firebase from google. And I'm having problem with updating user email and password.

Here's what I've tried.

let currentUser = FIRAuth.auth()?.currentUser

currentUser?.updateEmail(email) { error in
    if let error = error {
        print(error)

    } else {
        // Email updated.
        currentUser?.updatePassword(password) { error in
            if let error = error {

            } else {
                // Password updated.
                print("success")

            }
        }
    }
}

But when updating password this occurs error like this.

"Domain=FIRAuthErrorDomain Code=17014 "This operation is sensitive and requires recent authentication..."

As I know we have to re-configure user after updating email.

I tried with this code for re-cofiguring from the firebase.

let user = FIRAuth.auth()?.currentUser
var credential: FIRAuthCredential

// Prompt the user to re-provide their sign-in credentials

user?.reauthenticateWithCredential(credential) { error in
  if let error = error {
    // An error happened.
  } else {
    // User re-authenticated.
  }
}

But this occurs error

Variable 'credential' used before being initialed

I know this is because I don't initialize 'credential' variable but I don't know how to fix this to work.

Is there anybody who knows solution?


回答1:


In your edit you didn't initialize your FIRAuthCredential ... it should be var credential = FIRAuthCredential() or simply use it like below code

let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

user?.reauthenticateWithCredential(credential) { error in
   if let error = error {
       // An error happened.
   } else {
      // User re-authenticated.
   }
 }


来源:https://stackoverflow.com/questions/38907324/how-to-update-both-email-and-password-with-new-firebase-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!