【问题标题】:How does Firebase Auth UI deal with reauthentication?Firebase Auth UI 如何处理重新身份验证?
【发布时间】:2017-06-02 09:58:15
【问题描述】:

我正在为我的 Swift iOS 应用程序使用 Firebase 身份验证。 Google 建议使用 Firebase Auth UI 作为“插入式”身份验证系统,但它只处理初始登录。我现在正致力于允许用户更改个人资料,例如电子邮件和密码。

进行这些更改的文档在多个地方提到某些更改需要用户最近登录(请参阅https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user):

某些安全敏感操作(例如删除帐户、设置主电子邮件地址和更改密码)要求用户最近登录。如果您执行这些操作之一,而用户登录时间过长之前,操作失败并出现 FIRAuthErrorCodeCredentialTooOld 错误。

首先,API 中的任何地方似乎都没有出现FIRAuthErrorCodeCredentialTooOld 错误。

其次,文档建议使用reauthenticate(with:) 来解决这个问题,代码示例如下:

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

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

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

问题是,因为我使用了 Firebase Auth UI,所以我没有用于获取用户凭据的自定义 UI。

我目前的想法是,我可以通过在发生此错误时显示用于登录的相同 Firebase 身份验证 UI 来重新进行身份验证。但是,我不知道这是否是认可的方式,或者它是否会起作用,或者它是否会在未来继续起作用。我检查了 Firebase Auth UI 代码库,并没有在任何地方调用 reauthenticate()。文档在此错误的情况下专门调用此方法,所以我很困惑。

如果我需要构建整个 UI 来执行重新身份验证,包括多个提供程序,那么使用 Firebase Auth UI 有什么意义?

【问题讨论】:

  • 这个问题是 2 年前提出的。我们是否已经在 2019 年为此制定了有效的解决方案?

标签: ios swift firebase firebase-authentication firebaseui


【解决方案1】:

关于错误代码,文档只需要更新。错误代码现在称为FIRAuthErrorCode.errorCodeRequiresRecentLogin

现在,就您所面临的 UI 问题而言,为什么不直接提供带有文本字段的 UIAlertController,用户可以使用该文本字段输入密码以进行重新身份验证?它肯定比创建一个完整的视图控制器要简单得多(并且对用户更友好)。

下面是一个非常简单的示例,说明如何在不经历太多麻烦的情况下重新验证​​用户身份:

// initialize the UIAlertController for password confirmation
let alert = UIAlertController(title: "", message: "Please, enter your password:", preferredStyle: UIAlertControllerStyle.alert)
// add text field to the alert controller
alert.addTextField(configurationHandler: { (textField) in
    textField.placeholder = "Password"
    textField.autocapitalizationType = .none
    textField.autocorrectionType = .no
    textField.isSecureTextEntry = true
})
// delete button action
alert.addAction(UIAlertAction(title: "Delete account", style: UIAlertActionStyle.destructive, handler: {action in
    // retrieve textfield
    let txtFld = alert.textFields![0]
    // init the credentials (assuming you're using email/password authentication
    let credential = FIREmailPasswordAuthProvider.credential(withEmail: (FIRAuth.auth()?.currentUser?.email)!, password: txtFld.text!)
    FIRAuth.auth()?.currentUser?.reauthenticate(with: credential, completion: { (error) in
        if error != nil {
            // handle error - incorrect password entered is a possibility
            return
        }

        // reauthentication succeeded!
    })
}))
// cancel reauthentication
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {action in }))

// finally, present the alert controller
self.present(alert, animated: true, completion: nil)

当您需要更改用户的电子邮件或删除他们的帐户(密码重置根本不需要登录)时,使用上面的 sn-p 来弹出一个警报控制器,他们可以在其中重新输入密码。

编辑:请注意,上面提供的代码会强制解包当前用户的电子邮件,因此请确保您在该阶段有用户登录,否则您的应用将崩溃。

【讨论】:

  • 当然可以,但是如果用户使用 Facebook 或 Google 等提供商登录会怎样?没有迹象表明此错误仅在使用电子邮件登录时发生。
  • 在这种情况下,您需要先提示用户输入登录方法。你可能需要一个合适的视图控制器,而不仅仅是一个警报控制器
  • 谢谢,这正是我所期望的。所以,我最初的问题仍然存在:我可以再次使用 Firebase Auth UI 进行重新身份验证吗?它不会在任何地方调用 reauthenticate。但是,如果再次登录就可以了,那也应该可以吗? Firebase 团队的一些澄清会有所帮助。
  • 我的错!我似乎错过了你问题的那一部分。完全使用 Firebase Auth UI 重新登录应该按您的预期工作,因为它会生成一个新的访问令牌。不过我同意,他们确实应该进一步记录身份验证/重新身份验证过程!
  • 我有一个类似的问题,我正在使用电话身份验证 UI。在不使用 UI 时,可以获取验证 ID 并将其持久化到磁盘,以后是否可以重复使用此验证 ID,从而无需使用 SMS 令牌进行完整的重新验证?我的问题在这里:stackoverflow.com/questions/47311502/…
猜你喜欢
  • 2017-05-07
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 2021-07-11
  • 2020-10-13
  • 2018-03-28
  • 2015-10-10
相关资源
最近更新 更多