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?
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.
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.
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.")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With