如何在Firebase 3上更改密码时验证用户的当前密码?

Ama*_*ade 12 ios firebase swift firebase-authentication

我希望用户在更新密码时插入当前密码和新密码.

我搜索过Firebase文档,但没有找到验证用户当前密码的方法.

有谁知道这是否可能?

ado*_*srs 15

在更改密码之前,您将能够使用reauthenticate实现它.

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
    }
})
Run Code Online (Sandbox Code Playgroud)

只需添加更多信息,您可以在此处找到如何为您使用的任何提供程序设置凭据对象.


小智 9

对于Swift 4:

typealias Completion = (Error?) -> Void 

func changePassword(email: String, currentPassword: String, newPassword: String, completion: @escaping Completion) {
    let credential = EmailAuthProvider.credential(withEmail: email, password: currentPassword)
    Auth.auth().currentUser?.reauthenticate(with: credential, completion: { (error) in
        if error == nil {
            currentUser.updatePassword(to: newPassword) { (errror) in
                completion(errror)
            }
        } else {
            completion(error)
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

Firebase文档可在此处找到