经过多次测试,我决定创建一个新的xCode项目,以更好地了解具有多个提供程序的Firebase身份验证。
我在Firebase中设置->登录方法->每个电子邮件地址的帐户
每个电子邮件地址的帐户
防止用户创建多个 使用相同身份验证地址的电子邮件地址的帐户 提供者
在这一点上,我已经认真执行了Firebase指南,使用 Facebook 和 Google 登录。我无法管理的相同错误:
当我的用户通过 Google 创建Firebase帐户时,如果决定使用 Facebook ,他将不再能够登录。
Facebook 在使用Firebase完成身份验证流程时返回错误:
Facebook提供程序的Firebase错误:已经存在一个具有相同电子邮件地址但登录凭据不同的帐户。使用与此电子邮件地址关联的提供商登录。
继续按照文档逐步操作,我停止了here(firebase解释了如何处理此错误)
我还实现了错误处理,但是在致电Auth.auth().fetchSignInMethods
之后,Firebase说我应该使用现有提供程序对用户进行身份验证,这时我如何获取现有提供程序进行身份验证的凭据?
我不想重新打开现有的提供者控制器以获取新的凭据
我是否有义务要求用户使用现有提供商登录并再次显示另一个访问控制器(在本例中为 Google )?
我应该如何处理这种情况?
override func viewDidLoad() {
super.viewDidLoad()
facebookSetup()
}
func facebookSetup() {
let loginButton = FBLoginButton(permissions: [ .publicProfile, .email ])
loginButton.center = view.center
loginButton.delegate = self
view.addSubview(loginButton)
}
//MARK: - FACEBOOK Delegate
func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
if let error = error {
print(error.localizedDescription)
return
}
let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
Auth.auth().signIn(with: credential) { (authResult, error) in
if let error = error {
print("\n FIREBASE: ",error.localizedDescription)
// An account with the same email already exists.
if (error as NSError?)?.code == AuthErrorCode.accountExistsWithDifferentCredential.rawValue {
// Get pending credential and email of existing account.
let existingAcctEmail = (error as NSError).userInfo[AuthErrorUserInfoEmailKey] as! String
let pendingCred = (error as NSError).userInfo[AuthErrorUserInfoUpdatedCredentialKey] as! AuthCredential
// Lookup existing account identifier by the email.
Auth.auth().fetchSignInMethods(forEmail: existingAcctEmail) { providers, error in
if (providers?.contains(GoogleAuthProviderID))! {
// Sign in with existing account.
Auth.auth().signIn(with: "? ? ? ?") { user, error in
// Successfully signed in.
if user != nil {
// Link pending credential to account.
Auth.auth().currentUser?.link(with: pendingCred) { result, error in
// Link Facebook to Google Account
}
}
}
}
}
}
}
}