swift - 如何使用新的 Firebase 快速更新电子邮件和密码

标签 swift firebase firebase-authentication

我正在使用来自谷歌的新 Firebase 开发应用程序。 我在更新用户电子邮件和密码时遇到问题。

这是我尝试过的。

let currentUser = FIRAuth.auth()?.currentUser

currentUser?.updateEmail(email) { error in
    if let error = error {
        print(error)

    } else {
        // Email updated.
        currentUser?.updatePassword(password) { error in
            if let error = error {

            } else {
                // Password updated.
                print("success")

            }
        }
    }
}

但是在更新密码时会出现这样的错误。

"Domain=FIRAuthErrorDomain Code=17014 "This operation is sensitive and requires recent authentication..."

据我所知,我们必须在更新电子邮件后重新配置用户。

我尝试使用此代码从 firebase 重新配置。

let user = FIRAuth.auth()?.currentUser
var credential: FIRAuthCredential

// Prompt the user to re-provide their sign-in credentials

user?.reauthenticateWithCredential(credential) { error in
  if let error = error {
    // An error happened.
  } else {
    // User re-authenticated.
  }
}

但是这样会出现错误

Variable 'credential' used before being initialed

我知道这是因为我没有初始化“凭据”变量,但我不知道如何解决这个问题。

有知道解决办法的吗?

最佳答案

在您的编辑中,您没有初始化您的 FIRAuthCredential ...它应该是 var credential = FIRAuthCredential() 或者像下面的代码一样简单地使用它

let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

user?.reauthenticateWithCredential(credential) { error in
   if let error = error {
       // An error happened.
   } else {
      // User re-authenticated.
   }
 }

关于swift - 如何使用新的 Firebase 快速更新电子邮件和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38907324/

相关文章:

java - 如何确定 setPersistenceEnabled 是否已启用?

ios - Realm - 我可以在删除对象后保存对象吗?

swift - 错误 : `' (@lvalue String) -> Text' is not convertible to '(String) -> Text' ` in SwiftUI

Swift:如何显示屏幕设备的 DFPBannerView 全宽?

ios - Firebase iOS : Second tap is working properly in login page

ios - 将现有 Firebase 身份验证提供商下的用户迁移到另一个提供商

xcode - 将 SKNode 添加到 SpriteKit

android - Firebase 数据库 onDataChange 有时不工作

ios - Swift3/Firebase 更新子项

ios - Firebase 身份验证 :Newly created user can't be saved in Firebase