Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify user's current password when changing password on Firebase 3?

I want the user to insert the current password and the new one when updating his password.

I've searched Firebase documentation and didn't find a way to verify the user's current password.

Does anyone know if this is possible?

like image 223
Amadeu Andrade Avatar asked Jul 16 '16 11:07

Amadeu Andrade


People also ask

How do I find my Firebase authentication password?

Finding the Password Hash Parameters To access these parameters, navigate to the 'Users' tab of the 'Authentication' section in the Firebase Console and select 'Password Hash Parameters' from the drop down in the upper-right hand corner of the users table.


2 Answers

You will be able to achieve it using reauthenticate before changing the password.

let user = FIRAuth.auth()?.currentUser
let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: currentPassword)    

user?.reauthenticateWithCredential(credential, completion: { (error) in
    if error != nil{
        self.displayAlertMessage("Error reauthenticating user")
    }else{
        //change to new password
    }
})

Just to add more information, here you can find how to set the credential object for whatever provider you are using.

like image 96
adolfosrs Avatar answered Oct 23 '22 12:10

adolfosrs


For Swift 5 to change password in firebase

import Firebase

func changePassword(email: String, currentPassword: String, newPassword: String, completion: @escaping (Error?) -> Void) {
        let credential = EmailAuthProvider.credential(withEmail: email, password: currentPassword)
        Auth.auth().currentUser?.reauthenticate(with: credential, completion: { (result, error) in
            if let error = error {
                completion(error)
            }
            else {
                Auth.auth().currentUser?.updatePassword(to: newPassword, completion: { (error) in
                    completion(error)
                })
            }
        })
    }

How to use ?

self.changePassword(email: "[email protected]", currentPassword: "123456", newPassword: "1234567") { (error) in
        if error != nil {
            self.showAlert(title: "Error" , message: error?.localizedDescription)
        }
        else {
            self.showAlert(title: "Success" , message: "Password change successfully.")
        }
    }
like image 30
Hardik Thakkar Avatar answered Oct 23 '22 14:10

Hardik Thakkar