【发布时间】:2016-11-19 12:27:12
【问题描述】:
我希望用户在更新密码时插入当前密码和新密码。
我搜索了 Firebase 文档,但没有找到验证用户当前密码的方法。
有谁知道这是否可行?
【问题讨论】:
标签: ios swift firebase firebase-authentication
我希望用户在更新密码时插入当前密码和新密码。
我搜索了 Firebase 文档,但没有找到验证用户当前密码的方法。
有谁知道这是否可行?
【问题讨论】:
标签: ios swift firebase firebase-authentication
您将能够在更改密码之前使用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
}
})
只是为了添加更多信息,here 您可以找到如何为您使用的任何提供者设置凭证对象。
【讨论】:
对于 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)
}
})
}
Firebase 文档可以在 here 找到
【讨论】:
让 Swift 5 在 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)
})
}
})
}
如何使用?
self.changePassword(email: "abcemail@gmail.com", 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.")
}
}
【讨论】: