【问题标题】:How to verify user's current password when changing password on Firebase 3?在 Firebase 3 上更改密码时如何验证用户的当前密码?
【发布时间】:2016-11-19 12:27:12
【问题描述】:

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

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

有谁知道这是否可行?

【问题讨论】:

    标签: ios swift firebase firebase-authentication


    【解决方案1】:

    您将能够在更改密码之前使用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 您可以找到如何为您使用的任何提供者设置凭证对象。

    【讨论】:

    • @AmadeuAndrade 不错!很高兴你成功了!
    【解决方案2】:

    对于 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 找到

    【讨论】:

      【解决方案3】:

      让 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.")
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2019-05-27
        • 1970-01-01
        • 2023-03-31
        • 2017-04-27
        • 2014-07-27
        • 2018-06-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多