如何檢查用戶是否需要使用Firebase身份驗證重新進行身份驗證 - How to check if user needs to re-authenticate using Firebase Authentication -开发者知识库

如何檢查用戶是否需要使用Firebase身份驗證重新進行身份驗證 - How to check if user needs to re-authenticate using Firebase Authentication -开发者知识库,第1张

I am using Firebase to log in users into my app, but when I am adding the capability to manage their account like changing their email, password and so on. The documentation says that if the user have not recently signed in they need to re-authenticate, but my question is: How can I check if the user have signed in recently or not? According to the docs the error will return FIRAuthErrorCodeCredentialTooOld, but how can I check this?

我正在使用Firebase將用戶登錄到我的應用中,但是當我添加管理其帳戶的功能時,例如更改其電子郵件,密碼等。文檔說如果用戶最近沒有登錄,則需要重新進行身份驗證,但我的問題是:如何檢查用戶是否最近登錄過?根據文檔,錯誤將返回FIRAuthErrorCodeCredentialTooOld,但我該如何檢查?

如何檢查用戶是否需要使用Firebase身份驗證重新進行身份驗證 - How to check if user needs to re-authenticate using Firebase Authentication -开发者知识库,From the Firebase docs:,第2张

1 个解决方案

#1


1  

Swift 3 I had to do this yesterday when trying to delete a user. One thing to note is FIRAuthErrorCodeCredentialTooOld is now FIRAuthErrorCode.errorCodeRequiresRecentLogin

Swift 3我昨天在嘗試刪除用戶時不得不這樣做。需要注意的一件事是FIRAuthErrorCodeCredentialTooOld現在是FIRAuthErrorCode.errorCodeRequiresRecentLogin

What I did was trigger a UIView to ask for log in details if that error is thrown. Since I was using email and password, that's what I collected from the user in my example.

我所做的是觸發UIView要求登錄詳細信息,如果拋出該錯誤。由於我使用的是電子郵件和密碼,這就是我在我的示例中從用戶那里收集的內容。

private func deleteUser() {
  //get the current user
  guard let currentUser = FIRAuth.auth()?.currentUser else { return }

  currentUser.delete { (error) in
    if error == nil {
      //currentUser is deleted
    } else {
      //this gets the error code
      guard let errorCode = FIRAuthErrorCode(rawValue: error!._code) else { return }
      if errorCode == FIRAuthErrorCode.errorCodeRequiresRecentLogin {
        //create UIView to get user login information
        let loginView = [yourLoginUIViewController]
        self.present(loginView, animated: true, completion: nil)
      }
    }
}

Once I had the login information I ran this function to reauthenticate the user. In my case I ran it the loginView in the above code if the login in was successful:

獲得登錄信息后,我運行此功能以重新驗證用戶。在我的情況下,如果登錄成功,我在上面的代碼中運行了loginView:

func reauthenticateUserWith(email: String, password: String) {
  FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
    if error == nil {
      //display UIView to delete user again
      let deleteUserView = deleteUserView()
      present(deleteUserView, animated: true, completion: nil)
    } else {
      //handle error
      print(error!.localizedDescription)
    }
  }
}

The deleteUserView in my case calls deleteUser() on a button tap from the user. You can also use UIAlertController in place of the custom UIViews, but that's up to you.

在我的情況下,deleteUserView在用戶點擊按鈕時調用deleteUser()。您也可以使用UIAlertController代替自定義UIViews,但這取決於您。

Hope this helps.

希望這可以幫助。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复