David Jonker David Jonker - 3 months ago 26
Swift Question

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

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?

Answer

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.
   }
 }
Comments