Reputation: 115
I'm implementing facebook login with firebase for my application. The objective is to develop a logic that the user can login with email/password provider or facebook provider. As i started developing, i found two ways to do what i want:
1 - Link facebook to email/password provider:
2 - Enable multiple accounts with the same email on firebase:
User can sign up with facebook and email/password. But firebase won't register the facebook data for the email/password provider. Which means, when i try to get the email from the current user, i have to specify the provider that have the email attribute (facebook provider). Email/password provider returns an empty email
final String email = firebaseUser.getProviderData().get(1).getEmail();
Conclusion
Is there something i'm missing? I tried using updateEmail() when user signs up with facebook but it just updates the email attribute for facebook provider. Do i really have to make the user insert his password to create a link between the two providers?
Upvotes: 0
Views: 1226
Reputation: 24194
To have the convenience of user profile information being provided by methods such as firebase.auth().signInWithRedirect()
and firebase.auth().linkWithRedirect()
, then the Firebase project needs to be configured with One account per email address
(Firebase Console > Authentication > Sign-in method
). This is the simplest (and default) configuration.
The original question stated:
I don't want the user to insert his password for email/password provider just to link to his facebook provider.
If a user consistently signs in using the same provider method (e.g. Facebook, password, email link, Google Sign-in) then he/she will never need to link accounts.
For users that want to sign-in using multiple providers, account linking is a straightforward user experience and only happens one time when first linking providers.
When the Firebase project is configured with One account per email address
, then the first time a user attempts to sign-in with a different provider, you will receive the Firebase error auth/account-exists-with-different-credential
.
Resolve this by calling firebase.auth.Auth.fetchSignInMethodsForEmail
and then asking the user to sign in using one of the returned providers.
Once the user is signed in, the original credential can be linked to the user with firebase.User.linkWithCredential
Note that account linking works for any combination of providers. Hence, it doesn't matter if a user first signs-up using email/password and then later decides to sign-in with Facebook or vice versa.
Upvotes: 1