如何在Firebase IOS中正确链接不同的Auth帐户 [英] How to correctly link different Auth accounts in Firebase IOS

查看:158
本文介绍了如何在Firebase IOS中正确链接不同的Auth帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我正在开发一个使用firebase作为后端的IOS应用程序。
有3个认证:
1:密码和电子邮件
2:FaceBook
3:Google



选项每个帐户一个电子邮件选项。



情况是:

如果我先登录与身份验证提供商之一,然后注销,并希望与任何其他两个身份验证提供商注册。如果当前提供者的关联电子邮件与以前相同,则会收到电子邮件地址已被使用错误。在这种情况下,我想将当前的Auth帐户与以前的帐户关联。



我明白我需要调用 linkWithCredential:completion:方法链接帐户。但我首先需要登录以前的帐户,但我怎么能告诉哪个帐户登录?例如,如果我通过Facebook登录并获得正在使用的电子邮件错误,那么我现在应该如何知道是否应该通过Google或电子邮件/密码登录?



有趣的是,如果我使用Facebook或电子邮件/密码先登录,然后用Google登录,firebase将自动处理链接,但默认行为是用Google覆盖以前的Auth提供商,并保留UID ...



我找到了一个有用的帖子



但是它只处理更简单的当你得到的证书已经存在的错误,你已经有了电子邮件在那一点上,然后用该电子邮件地址 fetchProvidersForEmail 来查找p与该电子邮件相关联的rovider ID。然后您使用其中一个提供者登录用户。完成用现有帐户登录后,使用导致错误发生的原始凭证调用 linkWithCredential:completion:。这导致帐户链接。用户下一次尝试登录时,他们将能够使用任一提供者登录到同一用户。



检查FirebaseUI-iOS已经处理整个流程为你。您还可以查看源代码,了解他们如何处理这种情况: https://github.com/firebase / FirebaseUI-iOS

Background:

I am developing an IOS app using firebase as backend. There are 3 authentication: 1:password and email 2:FaceBook 3:Google

I have checked the option "one email per account" option.

The situation is:

Say if I first sign in with one of the Auth provider and later, log out, and want to sign up with any other two Auth providers. I will get an "the email address has been used" error if the associated Email of the current provider is the same as previous. In this case I want to link the current Auth account with the previous account.

I understand that I need to call the linkWithCredential:completion: method to link the accounts. But I first need to sign In the previous account but how can I tell which account to sign in? For example, if I log in via Facebook and get the "same email being used" error, how do I know at this point whether should I sign in via Google or the email/password?

One interesting thing is If I use Facebook or email/password to sign in first and later sign in with Google, firebase will automatically handle the linking but the default behaviour is to overwrite the previous Auth provider with Google and keep the UID...

I have found an useful post How to manage users' different authentication in firebase

But it only deal with a simpler situation where authentication are only two.

解决方案

When you get the credential already exists error, you already have the email at that point, you then call fetchProvidersForEmail with that email which will lookup the provider IDs associated with that email. You then sign in the user with one of those providers. After you finish sign-in with the existing account, you call linkWithCredential:completion: with the original credential that caused the error to occur. This causes the accounts to link. The next time the user tries to sign in, they will be able to sign in to the same user with either provider.

Check FirebaseUI-iOS which already takes care of the whole flow for you. You can also check there source code to see how they handle such situations: https://github.com/firebase/FirebaseUI-iOS

这篇关于如何在Firebase IOS中正确链接不同的Auth帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆