尝试使用 Firebase 重新验证用户时出错

Getting an error trying to reauthenticate user with Firebase

所以我的目标是让用户重新进行身份验证,然后能够更新他们的密码。所以现在我有一个警报控制器,它会在点击一个单元格时显示,并且在该警报中 textFields 用于重新验证。

        var reauthEmailTextField = UITextField()
        var reauthPasswordTextField = UITextField()
        let credential: AuthCredential = EmailAuthProvider.credential(withEmail: reauthEmailTextField.text ?? "", password: reauthPasswordTextField.text ?? "")
        let reauthAlert = UIAlertController(title: "Reauthenticate", message: "In order for you to change your password, you must provide your sign-in credentials again.", preferredStyle: .alert)
        let submit = UIAlertAction(title: "Submit", style: .default) { (sub) in
            user.reauthenticate(with: credential) { (result, error) in
                guard error == nil else {
                    print("The user couldn't be reauthenticated. Email: \(reauthEmailTextField.text ?? "") Password:  \(reauthPasswordTextField.text ?? "")")
                    return
                } ...



     reauthAlert.addTextField { (reAuthEmail) in
            reauthEmailTextField = reAuthEmail
            reAuthEmail.placeholder = "Your Email"
            reAuthEmail.clearButtonMode = .whileEditing
        }
        
        reauthAlert.addTextField { (reAuthPass) in
            reauthPasswordTextField = reAuthPass
            reAuthPass.clearButtonMode = .whileEditing
            reAuthPass.placeholder = "Your Password"
            reAuthPass.isSecureTextEntry = true
        }
        
        reauthAlert.addAction(UIAlertAction(title: "Dismiss", style: .cancel))
        reauthAlert.addAction(submit)
        
        present(reauthAlert, animated: true, completion: nil)

现在,当用户成功重新验证后,它会关闭当前警报并显示新警报以实际更新用户密码。问题是,重新身份验证过程总是失败,并且永远不会结束显示第二个警报。打印语句确实打印出 textFields 中的输入,所以我知道它与 textFields 无关。我正在阅读一些关于重新验证的帖子,并将我的代码与其他代码进行比较,我相信我的语法是正确的,所以我真的无法理解这个重新验证不起作用的问题。有什么建议吗?

您正在创建 credential 对象 之前 用户有机会在文本字段中输入任何内容:

var reauthEmailTextField = UITextField()
var reauthPasswordTextField = UITextField()
let credential: AuthCredential = EmailAuthProvider.credential(withEmail: reauthEmailTextField.text ?? "", password: reauthPasswordTextField.text ?? "") //<-- Here

在这种情况下,您将始终获得两个值的""

相反,您需要在 您的 submit 闭包中捕获这些值:

let submit = UIAlertAction(title: "Submit", style: .default) { (sub) in

   //get text field values and build `credential` object here
   let credential: AuthCredential = EmailAuthProvider.credential(withEmail: reauthEmailTextField.text ?? "", password: reauthPasswordTextField.text ?? "")

   user.reauthenticate(with: credential) { (result, error) in
                guard error == nil else {
                    print("The user couldn't be reauthenticated. Email: \(reauthEmailTextField.text ?? "") Password:  \(reauthPasswordTextField.text ?? "")")
                    return
                } ...

由于某种原因,声明的定位导致了问题。我决定像这样修改方法:

  user.reauthenticate(with: EmailAuthProvider.credential(withEmail: reauthEmailTextField.text ?? "", password: reauthPasswordTextField.text ?? "")) { (result, error) in
                guard error == nil else {
                    print("The user couldn't be reauthenticated. Email: \(reauthEmailTextField.text ?? "") Password: \(reauthPasswordTextField.text ?? "")")
                    print("\(String(describing: error))")
                    return
                }

这可能不是最干净的方法,但如果鞋子合脚,那就试试吧。整个过程现在工作正常。