Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 741
Svetoslav Atanasov Avatar asked Aug 11 '16 22:08

Svetoslav Atanasov


People also ask

How do I change my Firebase verification email?

Open your project in the Firebase console. Go to the Email Templates page in the Auth section. In any of the Email Types entries, click the pencil icon to edit the email template. Click customize action URL, and specify the URL to your custom email action handler.

How do I change my Firebase authentication password?

One way to allow your users to change their password is to show a dialog where they enter their current password and the new password they'd like. You then sign in (or re-authenticate) the user with the current passwordand call updatePassword() to update it.

Does Firebase automatically hash passwords?

By default, Firebase uses a modified Firebase version of the scrypt hashing algorithm to store passwords. Importing passwords hashed with modified scrypt is useful for migrating users from another existing Firebase project.


1 Answers

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.
   }
 }
like image 111
EI Captain v2.0 Avatar answered Sep 29 '22 23:09

EI Captain v2.0