Firebase authentication with multple providers - firebase

I'm trying to create a user authentication screen, so I decided to use Firebase.
However, I am facing a problem. Nowadays I have enabled 2 providers, which are:
1- E-mail/Password
2- Google
Enabled providers
I can create users perfectly.
When I create a user with E-mail/password option and use the Gmail address guilherme.nunes#fover.com.br, Firebase creates the user normally.
After using email, I am trying to use the second option of Google provider, using the same email. Now the old user is replaced with the new provider in Firebase.
account created
After this, if I try to login with the first option (E-mail/Password) again, it says that the user does not exist.
I tried disabling the option to have 1 user per account, but then, Firebase creates 2 distinct users.
What I would like, is to link 1 account, with several providers.
Can someone help me, please?

When you enable the option to allow only a single account per email address, Firebase has certain providers that are preferred for certain email domains. Most notably: the Google provider is the preferred provider for #gmail.com addresses.
So in your initial process, you:
Register a user something#gmail.com with a email+password account.
Register a user something#gmail.com with a google account.
In this case, the second registration replaces/overrides the initial account.
What you want to do is link the two accounts/providers as described in the documentation on account linking. With this approach you don't fully register an account in step 2, but instead link the credentials for that second account to the email+password account that was already created for the user.
The documentation has a step-by-step explanation on how to do this. If you're having trouble making this work, post a new question with the minimal, complete/standalone code that reproduces where you got stuck.

Related

Prevent Firebase Auth from creating multiple users with the same email from different providers

I have Email/Password & Google enabled as providers. I'm able to create a user with both providers and as a result I have two users with the same identifier (email) in Firebase. I need the identifier to be unique.
An answer in a rather old but same question on Stack Overflow says to:
Step 1 : Go to Firebase Console > Authentication > Sign in method. Check the option preventing multiple account creation with single email id.
But this option doesn't exist for me. The only thing I see under advanced is SMS Multi-factor Authentication. Is this maybe available on the Blaze plan (I have Spark) or was this removed over the years?
If there's no setting, should this be set up with rules or do I need to do additional checks before calling either signInWithPopup or createUserWithEmailAndPassword?
That option is now on the authentication settings page (Authentication > Settings) and is called User account linking.

Detect whether authetication credientials are already linked or not in Firebase

In Firebase, I want the users to be able to sign in with providers like Facebook, Twitter, and Google but not to sign up with them. So, when the user tries to login with them, how can I detect whether his/her credentials are linked to an account or not before trying to sign in with those credentials to Firebase?!
I don't think this is going to be possible. To be able to link accounts with those providers, you'll need to enable the providers in the Firebase console. And once you do that, users can call the API themselves to create an account with that provider.
If you don't care about this out-of-bounds abuse, and just want to make it work in your application code, have a look at the fetchSignInMethodsForEmail method.

Firebase linking multiple auth providers without matching email

I've been trying to find a way to figure out if a user is already created with the same email in Firebase, and it doesn't seem like it is possible.
This basically means I will need to save the email for every user and check in the Firebase database if the email is there already.
Is there really no other way?
I see all these posts with... how to link a user with another auth provider, but there is no way to know if the user with the specific mail exists already...
You cannot link an existing account to another account. You can only link a new account to an existing one. If you wish to check if an email of a new account already exists before either creating it or linking it to the existing account.
You can call https://firebase.google.com/docs/reference/js/firebase.auth.Auth#fetchProvidersForEmail
If the email provided already exists, it will return an array of the provider ids. You then sign in the user to the existing account and link the new account to it.
fetchProvidersForEmail will work as expected when multiple accounts per email is disabled in Firebase Console(default behavior unless you are migrating from Firebase v2).

Firebase : Authentication providers different email address

If I register with Facebook (x#x.com) and later log in with Google (y#y.com), but I do not have the same email address on both providers, there are 2 users created. How can I handle this situation?
Linking is typically used in three cases:
Automatically requested by the backend for security reasons: when a user signs in to google for example with email x#x and then logs out and tries to sign in with a new facebook account x#x. In this case the backend will not complete the second sign in without verifying that the second user is the same as the first user (since both use the same email). So in this case, the user has to sign to the google account and then link the second facebook account to the initial one.
Manually triggered by the developer: One common case here is that the user signs in to google with email x#x and remains signed in. The developer wants access to the user's facebook friends. So the developer will ask the user to link their facebook account to the already logged in google user.
Upgrading an anonymous user: Developer could automatically sign in users initially as anonymous and then prompt them to upgrade to a registered user. In this case you can call link on the anonymous user.
So auth.currentUser.link can be made on all kinds of users as long as the account you are linking is new and not already linked.
You'll want to use the Account Linking APIs to authenticate multiple providers for the same account. Docs for Web, Android, and iOS are available.

Facebook API, Using FB Connect on email address

ASP.Net C# and FaceBook Connect.
I'm using Facebook connect on my site. If a new user connects through FB i create an account for them and all is fine. What i would also like to do is check to see if they already have a registered account.
So if someone connects that has not logged in but has an account i would like to be able to locate the account in my application a link it. I hoping this could be done via the email address?
Any ideas
Thanks
Richard
UPDATE: You need Connect.registerUsers:
This method is used to create an
association between an existing user
account on your site and that user's
Facebook account, provided the user
has not connected accounts before.
This method takes an array of account
data, including a required email_hash
and optional account data. For each
connected account, if the user exists,
the information is added to the set of
the user's connected accounts. If the
user has already authorized the site,
the connected account is added in the
confirmed state. If the user has not
yet authorized the site, the connected
account is added in the pending state.
If the user deactivates their external
user account, then call
connect.unregisterUsers. To get the
number of friends a user has in the
pending state, use
connect.getUnconnectedFriendsCount.
If that doesn't resolve your question, this question is not a duplicate of yours, but the answers might give you a way forward:
How do we register users — Facebook Connect users logging in the first time — when we cannot get an email address from facebook?

Resources