gpt4 book ai didi

swift - Firebase Auth 与新的 Apple 从社交登录中选择退出

转载 作者:行者123 更新时间:2023-11-28 07:44:28 26 4
gpt4 key购买 nike

新的 Apple 指南规定,用户应该能够选择退出社交登录并切换到电子邮件登录。

对于 Facebook 和 Twitter 登录,我添加了电子邮件登录。现在,在设置 View 中,我想为社交登录选择退出添加这个选项,但我真的不明白 Firebase 在这种情况下是如何工作的。

我想创建一个新帐户,我想在成功创建帐户后我只需要删除(取消链接)旧提供商。

到目前为止,这是我的代码:

Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
guard let user = user else {
if let error = error as NSError? {
Alertift.alert(title: "", message: error.localizedDescription)
.action(.default("Okay"))
.show(on: self, completion: nil)
}
return
}

if self.isInOptOut {
// Opt-Out flow
print("OPT-OUT FLOW")
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
user.user.linkAndRetrieveData(with: credential, completion: { (result, error) in
if let error = error {
print(error)
Alertift.alert(title: "", message: error.localizedDescription)
.action(.default("Okay"))
.show(on: self, completion: nil)
}

guard let result = result else {
return
}

print(result)
})
}

// Regular flow, go through form
let userProfileFormViewController = UserProfileFormViewController()
userProfileFormViewController.email = user.user.email
userProfileFormViewController.isEmailDisabled = true
self.navigationController?.pushViewController(userProfileFormViewController, animated: true)
}

我收到用户已使用此提供商的错误消息。有人可以指出我正确的方向吗?

更新:

我设法以不同的顺序完成这项工作。

if self.isInOptOut {
guard let currentUser = Auth.auth().currentUser else { print("No user"); return }

// Link with email provider

let credential = EmailAuthProvider.credential(withEmail: email, password: password)
currentUser.linkAndRetrieveData(with: credential, completion: { (authResult, error) in
if let error = error {
Alertift.alert(title: "", message: error.localizedDescription)
.action(.default("Okay"))
.show(on: self, completion: nil)
}

// Unlink from Social provider
if currentUser.providerData.count > 0 {
for provider in currentUser.providerData {
print(provider.providerID)
if provider.providerID == "twitter.com" || provider.providerID == "facebook.com" {
currentUser.unlink(fromProvider: provider.providerID) { (user, error) in
Alertift.alert(title: "", message: "Success. From now on, use only email login.")
.action(.default("I understand"), handler: { _, _, _ in
self.navigationController?.popViewController(animated: true)
})
.show(on: self, completion: nil)
}
}
}
}
})
}

最佳答案

发生错误是因为您为用户创建了一个具有电子邮件身份验证的新帐户,然后您尝试从文档中合并:

The call to linkWithCredential:completion: will fail if the credentials are already linked to another user account.

即您正尝试在两个不同的帐户上使用相同的凭据。

删除.createUser.linkAndRetrieveData 成功后,使用电子邮件提供商登录用户并取消链接社交提供商。

关于swift - Firebase Auth 与新的 Apple 从社交登录中选择退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51449343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com