Firebase:将Facebook帐户与现有用户关联 [英] Firebase: Link facebook account with existing user

查看:187
本文介绍了Firebase:将Facebook帐户与现有用户关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中有一个具有活动用户的当前数据库,该数据库可以使用user/pwd登录,但是现在我正在实现facebook登录,并且我意识到将facebook帐户与现有用户关联的唯一方法是仅当该用户处于已经用用户名/pwd登录,但没有登录.

I have a current database with active users in Firebase that can login with user/pwd but now I'm implementing the facebook login and I realised the only way to link a facebook account with an existing user is only when the user is already logged with the user/pwd but not before the login.

我的应用程序中有两个按钮(使用fb和电子邮件登录),但是如果我尝试使用现有用户的同一电子邮件使用fb登录,则会收到以下错误消息: auth/account-exists- with-different-credential ,并且该文档说,要解决此问题,用户需要先登录然后再链接.

I have two buttons in my app (login with fb and with email) but if I try to login with fb using the same email of an existing user, I will receive the following error auth/account-exists-with-different-credential and the documentation says that in order to fix this the user needs to login first then link.

您知道是否有一种方法可以链接两个帐户,但无需先执行登录,即从登录视图开始?

Do you know if there is a way to link both accounts but without perform a login first, I mean, from the login view?

推荐答案

在链接之前,您需要先登录用户.如果要确保它是同一用户,这一点很重要.否则,您可以在Firebase控制台中为每个电子邮件切换到多个帐户. 解决此问题的方法是,当您收到错误auth/account-exists-with-different-credential时,使用Facebook登录后该错误将包含error.emailerror.credential,并且该帐户已作为密码帐户存在. 然后,您呼叫firebase.auth().fetchProvidersForEmail(error.email),并使用该电子邮件的提供商ID列表进行解析.在这种情况下,它将包含['password'].然后,您要求用户提供密码.您调用signInWithEmailAndPassword(error.email, password)登录原始用户.然后,您调用firebase.auth().currentUser.linkWithCredential(error.credential)将Facebook凭据链接到密码帐户.现在,两个帐户都已合并,用户可以使用任一帐户登录.

You need to sign in the user first before linking. This is important if you want to ensure it is the same user. Otherwise you can switch to multiple accounts per email in the Firebase console. The way to solve this, when you get the error auth/account-exists-with-different-credential, the error will contain error.email and error.credential after you sign in with Facebook and the account already exists as a password account. You then call firebase.auth().fetchProvidersForEmail(error.email) to which resolves with the list of provider IDs for that email. In this case, it will contain ['password']. You then ask the user to provide their password. You call signInWithEmailAndPassword(error.email, password) to sign-in the original user. You then call firebase.auth().currentUser.linkWithCredential(error.credential) to link the Facebook credential to the password account. Now both accounts are merged and the user can sign in with either.

这篇关于Firebase:将Facebook帐户与现有用户关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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