我正在尝试从iOS上的Swift中的用户取消链接电子邮件/密码身份验证。我已经阅读the documentation并成功地链接和取消链接了Facebook身份验证。但是,成功链接电子邮件/密码凭证后,providerData对象为nil。 providerID是“Firebase”,但是当我将其传递给取消链接代码时,将引发以下错误:

Error Domain=FIRAuthErrorDomain Code=17016 "User was not linked to an account with the given provider." UserInfo={NSLocalizedDescription=User was not linked to an account with the given provider., error_name=ERROR_NO_SUCH_PROVIDER}

我正在使用的取消链接代码是:
let providerId = (FIRAuth.auth()?.currentUser?.providerID)!
print("Trying to unlink:",providerId)     // providerId = "Firebase"
FIRAuth.auth()?.currentUser?.unlinkFromProvider(providerId) { user, error in
    if let error = error {
        print("Unlink error:", error)
    } else {
        // Provider unlinked from account successfully
       print("Unlinked...user.uid:", user!.uid, "Anonymous?:", user!.anonymous)
        }
    }

阅读文档并使其在Facebook上正常运行后,我希望在通过电子邮件身份验证后将providerData数组填充一些内容。那么我的链接代码是否错误(它不会引发错误并且看上去工作正常)?

我的链接代码:
let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

FIRAuth.auth()?.currentUser!.linkWithCredential(credential) { (user, error) in
    if user != nil && error == nil {
        // Success
        self.success?(user: user!)
        dispatch_async(dispatch_get_main_queue(), {
            self.dismissViewControllerAnimated(true, completion: nil)
            if type == "new" {
                print("New user logged in...")
            }
            if type == "existing" {
                print("Existing user logged in...")
            }
        })
    } else {
        print("Login error:",error)
        self.showOKAlertWithTitle("Login Error", message: error!.localizedDescription)
    }
}

关于如何修改我的方法的任何指示都很好。

最佳答案

要获取从链接到用户的登录提供者检索到的配置文件信息,请使用providerData属性。

if let user = FIRAuth.auth()?.currentUser {
  for profile in user.providerData {
    // Id of the provider (ex: facebook.com)
    let providerID = profile.providerID
  }
} else {
  // No user is signed in.
}

调用FIRAuth.auth()?. currentUser?.providerID将导致“firebase”。

07-28 03:12
查看更多