0
votes

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:

  • User has to be logged in with email/password to make this happen. Which means, if the user is not logged in, i don't have a reference (current user) to link the facebook provider. User cannot sign up with facebook provider because the email is already being used by another provider (email/password).

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

  • The first way won't work for me. User may not be logged in all the time. He may want to login in different ways. I don't want the user to insert his password for email/password provider just to link to his facebook provider.
  • If i choose the second way, i can't find a way to update the email attribute for email/password provider. And it wouldn't be a good practice to always check what providers the user have, just to specify which provider to get the email from.

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?

1

1 Answers

1
votes

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.

Linking Accounts

  • 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.